SQLDatasource parameters

hhh3112 picture hhh3112 · Feb 9, 2011 · Viewed 17.3k times · Source

How can i set sql parameters for an sqlDatasource in the code behind? I am trying like this:

int id=1;
SqlDataSource1.SelectCommand = "SELECT * FROM categ WHERE id=@id";
SqlDataSourceArticole.SelectParameters.Add("@id", id);
// and also like this:
SqlDataSourceArticole.SelectParameters.Add("id", id);

but it doesn't work? What am i doing wrong?

Answer

darth_phoenixx picture darth_phoenixx · Feb 9, 2011

Make sure you add the select parameters before trying to set their default value.

i.e.

SqlDataSourceArticole.SelectParameters.Add("@id", id);
SqlDataSourceArticole.SelectParameters["id"].DefaultValue = id.ToString();