4

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?

2 Answers 2

5

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();
Sign up to request clarification or add additional context in comments.

1 Comment

Should be SqlDataSourceArticole.SelectParameters.Add("id", id); with no @
2

You can update the value by:

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

Using the default value can work in this case.

HTH.

2 Comments

i get 'System.NullReferenceException: Object reference not set to an instance of an object.'
The ID parameter has to exist in markup; if you are programmably create the collection of parameters, then when you create the parameter, also set the default value.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.