I am trying to dynamically change a query in a SQLDataAdapter based off the querystring. I tried passing it in as a parameter, but that doesn't work. Any other suggestions?
Right now I have:
Dim conn As Data.SqlClient.SqlConnection = New Data.SqlClient.SqlConnection(WebConfigurationManager.ConnectionStrings("MyConnection").ToString())
Dim ad As New SqlDataAdapter
Dim selectSQL As String = "SELECT BookTitle, Publisher, Location, CallNumber, [Extra_Description] AS Description, [ISBN] FROM [New_Book_Table] WHERE [Export_to_list]='True' AND [@Category]='True'"
Dim selectCMD As SqlCommand = New SqlCommand(selectSQL, conn)
ad.SelectCommand = selectCMD
selectCMD.Parameters.Add("@Category", SqlDbType.NVarChar, 30).Value = getCategoryString()
Dim ds As New DataSet
ad.Fill(ds)
ListView1.DataSource = ds
ListView1.DataBind()
But get the error: System.Data.SqlClient.SqlException: Invalid column name '@Category'.
I guess because this isn't the correct way to do this. How can I do this? I can get the column name via query string. I want to pass in that column name into the query and have it be recognized as a column.
Well I figured this out, and it's a much easier method than trying to dynamically add parameters, since the initial SelectSQL is just a string, I dynamically constructed that string wit the following function:
Protected Function getSQLQuery() As String
Dim baseQuery As String = "SELECT BookTitle, Publisher, Location, CallNumber, [Extra_Description] AS Description, [ISBN] FROM [New_Book_Table] WHERE [Export_to_list]='True'"
Dim category As String = getCategoryString()
Return baseQuery & "AND [" & category & "]='True'"
End Function