I'm having problems trying to fill in the DataSet using SQL Server 2005. Basically I have a table in SQL Server and I want to retrieve those information and be able to display it in a web browser using DataSet. Whenever I run my code I always get error message:

Line 1: Incorrect syntax near '?'.
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 '?'.

Source Error:


Line 16: ProductDA = New SqlDataAdapter("Select * From Surfzone Where SBID = ?", objConn)
Line 17:
Line 18: ProductDA.Fill(ProductDS)
Line 19:
Line 20: Return ProductDS.Tables(0)

-------------------------------------------------------------------------------

Here's my code:

Imports System.Data
Imports System.Data.SqlClient

Namespace SurfZone

    Public Class Products

        Public Function RetrieveProductData(ByVal IDProduct As Integer) As DataTable

            Dim objConn As SqlConnection
            Dim ProductDA As SqlDataAdapter
            Dim ProductDS As New DataSet

            objConn = New SqlConnection("Data Source=0.0.0.0;Initial Catalog=DBNAME;Persist Security Info=TRUE;User ID=USERID;Password=PASSWORD")

            ProductDA = New SqlDataAdapter("Select * From TABLENAME Where ID = ?", objConn)

            ProductDA.Fill(ProductDS)

            Return ProductDS.Tables(0)

        End Function

    End Class

End Namespace

---------------------------------------------------------------------------

Any ideas??

Thanks!

You need to pass parameter value.

....
Dim Cmd as new SqlCommand("Select * From TABLENAME Where ID = @id", objConn)
Cmd.Parameters.Add(new SqlParameter("@id",SqlDbType.Int)).Value=10
ProductDA = New SqlDataAdapter(Cmd)
....

Hi adatapost,

Thank you so much for your reply! That worked like a charm! I adjusted some parts of the code because I'm using a query string being passed from the asp server page. The part of the code that I changed was:

Dim Cmd As New SqlCommand("Select * From TABLENAME Where ID = @id", objConn)
Cmd.Parameters.Add(New SqlParameter("@id", SqlDbType.Int)).Value = IDProduct

Thanks again for your quick response! I greatly appreciate it! :)

Regards

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.