Link to home
Start Free TrialLog in
Avatar of mpdillon
mpdillon

asked on

Are SQL Parameters compatible with .Net SQLDataAdapters?

I cannot get the following code to execute without this error. "Must declare the Scalar variable @CusNo".
           With MacCmd
                .Parameters.Clear()
                .Parameters.AddWithValue("@CusNo", CusNoString)
            End With
            ParmString = "Select M.Cus_No, M.Cus_Name, A.IDNo From " &     CompanyNameString & ".dbo.ARCusFil_SQL M " & _
            "Inner Join Amatex.dbo.AMTransferHdr A " & _
            "ON M.Cus_no = A.CusNo where CusNo = @CusNo " & _
            "Order by A.IDNo Desc"
            '
             MacCmd.CommandText = ParmString
            '
            Dim dataadapter As New SqlDataAdapter(ParmString, MacConn)
            Dim ds As New DataSet()
            Dim dt As New DataTable
            '
            Application.DoEvents()
            dataadapter.Fill(dt)  '<== Fails here
            CoConn.Close()

Open in new window


When I remove the Parameter, @CusNo and directly substitute the value, the SQLDataAdapter does not fail. See below.
            ParmString = "Select M.Cus_No, M.Cus_Name, A.IDNo From " &   CompanyNameString & ".dbo.ARCusFil_SQL M " & _
            "Inner Join Amatex.dbo.AMTransferHdr A " & _
            "ON M.Cus_no = A.CusNo where CusNo = '000000106800' " & _
            "Order by A.IDNo Desc"
            '
            MacCmd.CommandText = ParmString

            '
            Dim dataadapter As New SqlDataAdapter(ParmString, MacConn)
            Dim ds As New DataSet()
            Dim dt As New DataTable
            '
            Application.DoEvents()
            dataadapter.Fill(dt)  '<== Does not fail when I use the actual variable value
            CoConn.Close()

Open in new window


Is there a way to use Parameters with a SQLDataAdapter?

Thanks,
pat
Avatar of igordevelop
igordevelop
Flag of North Macedonia image

Hi,

You must first declare the SqlCommand and later pass the parameter. You were doing the reverse.

Try this, should work:

ParmString = "Select M.Cus_No, M.Cus_Name, A.IDNo From " &     CompanyNameString & ".dbo.ARCusFil_SQL M " & _
            "Inner Join Amatex.dbo.AMTransferHdr A " & _
            "ON M.Cus_no = A.CusNo where CusNo = @CusNo " & _
            "Order by A.IDNo Desc"
            '
                   MacCmd.Parameters.Clear()
             MacCmd.Parameters.AddWithValue("@CusNo", CusNoString)
             MacCmd.CommandText = ParmString
            '
            Dim dataadapter As New SqlDataAdapter(ParmString, MacConn)
            Dim ds As New DataSet()
            Dim dt As New DataTable
            '
            Application.DoEvents()
            dataadapter.Fill(dt)
            CoConn.Close()


Let me know if anything.

Regards,
Igor
SOLUTION
Avatar of kaufmed
kaufmed
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hi,

@kaufmed
The command is assigned in this line:
Dim dataadapter As New SqlDataAdapter(ParmString, MacConn)

It is not necessary to do it again.
Avatar of mpdillon
mpdillon

ASKER

Assigning the paramaters to the dataadapter works great.I thought that the Parameters were associated with the SQLCommand, MacCmd, but apparently the parameters must be assigned to the dataadapter.
Here is my final code.

MacCmd.CommandText = ParmString
            '
            Dim dataadapter As New SqlDataAdapter(ParmString, MacConn)
            Dim dt As New DataTable
            With dataadapter
                .SelectCommand.Parameters.Clear()
                .SelectCommand.Parameters.AddWithValue("@CusNo", CusNoString)
            End With
            '
            Application.DoEvents()
            dataadapter.Fill(dt)  '<== Code no longer fails here

Thank you for your assistance.
pat