Friday, March 16, 2012

Parameter Query to Datagrid

I have a Web form that collects a text box value as a query parameter and returns the results to the datagrid. The parameter collects the company name. Sometimes, we have several versions of the company name to look through. I would now like to include 3 or 4 text boxes to feed the same parameter as if I'm putting OR clauses between the company names. Not sure how to do this. Help appreciated. Here is my current code:

Dim DS As DataSet
Dim MyConnection As SqlConnection
Dim MyCommand As SqlDataAdapter

Dim SelectCommand As String = "select * from ClientName where CompanyName = @dotnet.itags.org.Company"

MyConnection = New SqlConnection("server=(local)\NetSDK;database=production;Trusted_Connection=yes")
MyCommand = New SqlDataAdapter(SelectCommand, MyConnection)

MyCommand.SelectCommand.Parameters.Add(New SqlParameter("@dotnet.itags.org.Company", SqlDbType.VarChar, 50))
MyCommand.SelectCommand.Parameters("@dotnet.itags.org.Company").Value = Textbox1.Value

Thanks,

Frank

It's possible I missunderstood your question (I blame it's late). But you could always use multiple parameters, as long as you have a fixed number of textboxes. Something like the following:

Dim SelectCommand As String = "select * from ClientName where CompanyName = @.Company1 OR CompanyName = @.Company2"

...

MyCommand.SelectCommand.Parameters.Add(New SqlParameter("@.Company1", SqlDbType.VarChar, 50))
MyCommand.SelectCommand.Parameters("@.Company1").Value = Textbox1.Value

MyCommand.SelectCommand.Parameters.Add(New SqlParameter("@.Company2", SqlDbType.VarChar, 50))
MyCommand.SelectCommand.Parameters("@.Company2").Value = Textbox2.Value

On a side note. It is possible to do "the same thing" using just one parameter. But it would require using stored procedures, and some SQL/T-SQL coding. Though, in your case, I would strongly recommend using multiple parameters instead.


Thanks David. Your suggestion is exactly what I needed.

Frank

0 comments:

Post a Comment