In Code Behind, What is proper select statement syntax to retrieve the @BName field from a table?
-
In Code Behind, What is proper select statement syntax to retrieve the @BName field from a table? Using Visual Studio 2003 SQL Server DB I created the following parameter: Dim strName As String Dim parameterBName As SqlParameter = New SqlParameter("@BName", SqlDbType.VarChar, 50) parameterBName.Value = strName myCommand.Parameters.Add(parameterBName) I tried the following but get error: Dim strSql As String = "select @BName from Borrower where BName= DOROTHY V FOWLER " error is: Line 1: Incorrect syntax near 'V'. Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. Exception Details: System.Data.SqlClient.SqlException: Line 1: Incorrect syntax near 'V'. Source Error: Line 59: Line 60: Line 61: myCommand.ExecuteNonQuery() 'Execute the query
-
In Code Behind, What is proper select statement syntax to retrieve the @BName field from a table? Using Visual Studio 2003 SQL Server DB I created the following parameter: Dim strName As String Dim parameterBName As SqlParameter = New SqlParameter("@BName", SqlDbType.VarChar, 50) parameterBName.Value = strName myCommand.Parameters.Add(parameterBName) I tried the following but get error: Dim strSql As String = "select @BName from Borrower where BName= DOROTHY V FOWLER " error is: Line 1: Incorrect syntax near 'V'. Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. Exception Details: System.Data.SqlClient.SqlException: Line 1: Incorrect syntax near 'V'. Source Error: Line 59: Line 60: Line 61: myCommand.ExecuteNonQuery() 'Execute the query
Sorry: To further clarify My database table name is borrower and two of the fields are BName and TaxID, I am trying to select a particular borrower (BName) with a taxid OF 767, lets say in this case THE BORROWER IS Bill Jones I declared the following variables: Dim strName As String Dim parameterBName As SqlParameter = New SqlParameter("@BName", SqlDbType.VarChar, 50) parameterBName.Value = strName myCommand.Parameters.Add(parameterBName) Dim strTaxID As String Dim ParameterTaxID As SqlParameter = New SqlParameter("@TaxID", SqlDbType.VarChar, 15) ParameterTaxID.Value = strTaxID myCommand.Parameters.Add(ParameterTaxID) I inserted the folowing command: Dim strSql As String = "select BName from Borrower where TaxID= @333-00-1492 " I get error: Must declare the variable '@333'.
-
Sorry: To further clarify My database table name is borrower and two of the fields are BName and TaxID, I am trying to select a particular borrower (BName) with a taxid OF 767, lets say in this case THE BORROWER IS Bill Jones I declared the following variables: Dim strName As String Dim parameterBName As SqlParameter = New SqlParameter("@BName", SqlDbType.VarChar, 50) parameterBName.Value = strName myCommand.Parameters.Add(parameterBName) Dim strTaxID As String Dim ParameterTaxID As SqlParameter = New SqlParameter("@TaxID", SqlDbType.VarChar, 15) ParameterTaxID.Value = strTaxID myCommand.Parameters.Add(ParameterTaxID) I inserted the folowing command: Dim strSql As String = "select BName from Borrower where TaxID= @333-00-1492 " I get error: Must declare the variable '@333'.
You pass in two parameters but you don't use them in the SQL statement. You then use a parameter (
@333
) that does not exist. Maybe somthing like this would work:SELECT BName from Borrower WHERE TaxID = @TaxID
You can drop the parameter
@BName
as it is not being used. To get theBName
back into your application you can useExecuteScalar()
.
"On two occasions, I have been asked [by members of Parliament], 'Pray, Mr. Babbage, if you put into the machine wrong figures, will the right answers come out?' I am not able to rightly apprehend the kind of confusion of ideas that could provoke such a question." --Charles Babbage (1791-1871) My: Website | Blog