can i use dynamic variable inside a dataadapter and bind to datagrid

J

jijis

Hi

I want to use a sql designed in the dataadapter and I want to pass a
variable so it can retrieve related data by using "WHERE" clause in the sql
statement.
 
E

Elton Wang

You can use command object to pass parameter to sql query.

SqlConnection conn = new SqlConnection(CONNECTIN_STRING);
SqlCommand command = conn.CreateCommand();
command.CommandType = CommandType.Text;
command.CommandText = "SELECT * FROM Table_Name WHERE
field_Name = @parameter";
command.Parameters.Add("@parameter",value);
SqlDataAdapter dap = new SqlDataAdapter(command);

HTH

Elton Wang
(e-mail address removed)
 
B

Brock Allen

So add a parameter to the DataAdapter.SelectCommand.Parameters collection:

DataAdapter1.SelectCommand.CommandText = "select * from foo where bar = @bar";
DataAdapter1.SelectCommand.Parameters.Add("@bar", "some value");
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,731
Messages
2,569,432
Members
44,835
Latest member
KetoRushACVBuy

Latest Threads

Top