Solved

Are SQL Parameters compatible with .Net SQLDataAdapters?

Posted on 2012-12-26
5
277 Views
Last Modified: 2012-12-28
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
0
Comment
Question by:mpdillon
5 Comments
 
LVL 1

Expert Comment

by:igordevelop
ID: 38722699
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
0
 
LVL 75

Assisted Solution

by:käµfm³d 👽
käµfm³d   👽 earned 250 total points
ID: 38722801
@igordevelop

You must first declare the SqlCommand and later pass the parameter.
I don't think that is accurate. It shouldn't matter until you actually send the query to the server.

@mpdillon

The problem as I see it--from the code you posted above--is that you have not associated the command with the DataAdapter. (Honestly, I don't think you even need the DataAdapter. See this article as to why.) Try assigning the command to the DataDapter:

...

dataadapter.SelectCommand = MacCmd
dataadapter.Fill(dt)

...

Open in new window

0
 
LVL 83

Accepted Solution

by:
CodeCruiser earned 250 total points
ID: 38724478
As above OR change your code to following


            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"

            Dim dataadapter As New SqlDataAdapter(ParmString, MacConn)
            dataadapter.SelectCommand.Parameters.AddWithValue("@CusNo", CusNoString)
            Dim dt As New DataTable
            dataadapter.Fill(dt)  
            dataadapter.Dispose

Open in new window

0
 
LVL 1

Expert Comment

by:igordevelop
ID: 38726201
Hi,

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

It is not necessary to do it again.
0
 

Author Closing Comment

by:mpdillon
ID: 38727081
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
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question