Reputation: 7532
I am trying to set a SQL parameter via the default value of a DropDownMenu:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
SqlDataSource2.SelectParameters["userParam"].DefaultValue = nameDropDownList.SelectedItem.Value.ToString();
}
}
I am getting the following error: Object reference not set to an instance of an object.
I presume this is because there is no selected value yet. I tried:
SqlDataSource2.SelectParameters["userParam"].DefaultValue = nameDropDownList.Items[1].Value;
and
nameDropDownList.SelectedIndex = 0;
SqlDataSource2.SelectParameters["userParam"].DefaultValue = nameDropDownList.SelectedItem.Value.ToString();
Is there a way to get the value of a drop down list on page load?
Upvotes: 0
Views: 234
Reputation: 9414
Try this:
SqlDataSource1.SelectCommand = "select * from ta where name like '%'+@userParam+'%'";
if (SqlDataSource1.SelectParameters.Count == 0)
{
SqlDataSource1.SelectParameters.Add("userParam", DbType.String, nameDropDownList.SelectedItem.Value);
}
SqlDataSource1.SelectParameters["userParam"].DefaultValue = nameDropDownList.SelectedItem.Value ;
Upvotes: 1