Proper syntax to retrieve field from SQL table?


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 6 of 6

Thread: Proper syntax to retrieve field from SQL table?

  1. #1
    Join Date
    Nov 2005
    Posts
    46

    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

  2. #2
    Join Date
    Nov 2003
    Location
    Portland, OR
    Posts
    8,387
    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!

  3. #3
    Join Date
    Nov 2005
    Posts
    46
    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'.

  4. #4
    Join Date
    Nov 2003
    Location
    Portland, OR
    Posts
    8,387
    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!

  5. #5
    Join Date
    Nov 2005
    Posts
    46
    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

  6. #6
    Join Date
    Nov 2003
    Location
    Portland, OR
    Posts
    8,387
    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

  1. How would u do it (design question) ?
    By Joe in forum Database
    Replies: 17
    Last Post: 04-04-2003, 05:55 PM
  2. SQL Many Table Relation
    By sa in forum .NET
    Replies: 2
    Last Post: 08-07-2002, 03:20 PM
  3. Help with SQL syntax
    By Roy in forum VB Classic
    Replies: 3
    Last Post: 12-04-2001, 03:39 AM
  4. Temporary table in SQL 7.0
    By bogus in forum Database
    Replies: 1
    Last Post: 03-22-2001, 09:26 AM
  5. Replies: 1
    Last Post: 02-09-2001, 09: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
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center