Provide Parameters

  • Thread starter Thread starter MXC
  • Start date Start date
M

MXC

I am new to Visual Web Developer and ASP.NET

If a GridView configured a SQLDataSource and displaying data with stored
procedure with parameters
Can anyone tell me how to put code to change parameter values runtime in
Visual Studio 2005 or Web Developer.

Thanks
 
If you are going to use the drag and drop, you have to create a control that
feeds the values and then link the value of that control to the parameters
in the wizard.

If you want to hand code it, the pattern is very simple:

1. Pull values for the parameters from the controls in the button click
event (or hard code the values, if you wish)
2. Create a SqlConnection object with a connection string to the database
3. Create a SqlCommand object with the stored procedure name as its SQL
4. Set the SqlCommand object to a CommandType of StoredProcedure
5. Add a parameter object for each parameter and feed it the proper value
6. Create a SqlDataAdapter object with the SqlCommand object as its select
command
7. Ceate a DataSet to receive the data
8. Open the connection
9. Fill the DataSet with the SqlDataAdapter
10. Dispose the connection
11. Bind the DataSet to the GridView

In code it looks something like this:

string connString =
ConfigurationManager.ConnectionStrings["connStringName"].ConnectionString;
string sql = "sprocName';

SqlConnection conn = new SqlConnection(connString);
SqlCommand cmd = new SqlCommand(sql, conn);
cmd.CommandType = CommandType.StoredProcedure;

cmd.Parameters.AddWithValue("@Param1Name", param1Value);

SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();

try
{
conn.Open();
da.Fill(ds);
}
finally
{
conn.Dispose();
}

GridView1.DataSource = ds.Tables[0].DefaultView;

I am not that fond of the drag and drop method, as you can probably tell.

--
Gregory A. Beamer
MVP: MCP: +I, SE, SD, DBA

Blog:
http://feeds.feedburner.com/GregoryBeamer

********************************************
| Think Outside the Box! |
********************************************
 
Back
Top