-
Proper syntax to retrieve field from SQL 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
-
First of all, it looks like @BName is the name of a parameter, not a field; I don't think SQL Server allows field names that begin with '@'. Second, you're trying to execute a query (a SQL statement that returns one or more records), but you're calling ExecuteNonQuery to do it. ExecuteNonQuery is intended for SQL statements (such as INSERT, UPDATE and DELETE commands) which do not return records.
See if this helps: http://www.csharp-station.com/Tutori.../Lesson06.aspx
Phil Weber
http://www.philweber.com
Please post questions to the forums, where others may benefit.
I do not offer free assistance by e-mail. Thank you!
-
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'.
-
Did you read the tutorial to which I linked in my previous post? What do you notice about the relationship between the parameter names and the SQL query?
Phil Weber
http://www.philweber.com
Please post questions to the forums, where others may benefit.
I do not offer free assistance by e-mail. Thank you!
-
I wish I knew...I am doing this in VB and there is a difference between C# and VB. Thanks for trying to help me
-
The SQL syntax has nothing to do with C# or VB; it's the same for both.
Phil Weber
http://www.philweber.com
Please post questions to the forums, where others may benefit.
I do not offer free assistance by e-mail. Thank you!
Similar Threads
-
Replies: 17
Last Post: 04-04-2003, 04:55 PM
-
Replies: 2
Last Post: 08-07-2002, 02:20 PM
-
By Roy in forum VB Classic
Replies: 3
Last Post: 12-04-2001, 02:39 AM
-
By bogus in forum Database
Replies: 1
Last Post: 03-22-2001, 08:26 AM
-
By Julian Pickard in forum VB Classic
Replies: 1
Last Post: 02-09-2001, 08:42 PM
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Development Centers
-- Android Development Center
-- Cloud Development Project Center
-- HTML5 Development Center
-- Windows Mobile Development Center
|