Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free.

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?

share|improve this question

2 Answers 2

up vote 5 down vote accepted

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();
share|improve this answer

You can update the value by:

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

Using the default value can work in this case.

HTH.

share|improve this answer
    
i get 'System.NullReferenceException: Object reference not set to an instance of an object.' –  hhh3112 Feb 9 '11 at 19:40
    
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. –  Brian Mains Feb 10 '11 at 17:04

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.