Forum Moderators: open

Message Too Old, No Replies

SqlDataAdapter and Parameters

Can't add parameters to sqldataadapter for use in SQL Select statement

         

evilracecarlive

10:39 pm on Jul 15, 2010 (gmt 0)

10+ Year Member



Hi Everyone,

I am trying to add a Parameter to a SqlDataAdapter (from a variable in my appsettings). I am pretty sure the code is all correct, but VS2008 keeps telling me
"'Parameters' is not a member of 'System.Data.SqlClient.SqlDataAdapter'" and won't publish.

I am wondering if anyone can help me out and tell me how to correct my code... Basically I need to add the "TesID" appsettings key as a Parameter to my SQL as "@TesID". Simplified my code as much as possible but this should still be enough to identify the problem.


Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

Dim myconnection As SqlConnection
Dim myda As SqlDataAdapter
Dim ds As New DataSet

myconnection = New SqlConnection(System.Configuration.ConfigurationManager.AppSettings("Connection"))

myda = New SqlDataAdapter("SELECT ID, dbo.FormatDateTime(DayDate, 'LONGDATE') AS FormatedDayDate, DayDate FROM dbo.udfDaysWithDates(@TesID) AS udfDaysWithDates_1", myconnection)
myda.Parameters.add("@TesID", SqlDbType.Int).Value = System.Configuration.ConfigurationManager.AppSettings("TesID")
myda.Fill(ds, "AllTables")

End Sub


It seems a little odd since this MSDN article seems to suggest it's possible to add parameters to DA and even gives examples...
([msdn.microsoft.com ])
But maybe I am missing something.

Any help is appreciated. Thanks in advance!

Ocean10000

1:55 pm on Jul 16, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



If you noticed it states the examples are for dot.net 4, and is very new.

In previous dot.net versions you had to access the underling command object to update the parameters for this to work.

try

myda.SelectCommand.Parameters.add("@TesID", SqlDbType.Int).Value = System.Configuration.ConfigurationManager.AppSettings("TesID")

evilracecarlive

3:39 pm on Jul 16, 2010 (gmt 0)

10+ Year Member



Thanks for the clarification on dot.net 4, Ocean. I figured I had to be missing something.

Your solution seems to have done the trick. This is a handy one to know for the future.

Thanks again!