user3779566
user3779566

Reputation: 31

set the value of sql data source parameter

I am filtering the data of an SQL data source by setting a WHERE clause parameter in this formatting [@a] and in the event of a combo box, I would to set the value to this parameter, the previous sql data source for a gridview

SqlDataSource2.SelectParameters.Clear();
SqlDataSource2.SelectParameters["@a"].DefaultValue = cid.ToString();
SqlDataSource2.DataBind();

I need to pass value to the WHERE parameter.

I choose the filtering parsed on non choice ( not form or control or profile or session ...)

This way gives me an error. Is there any way to give a value for this parameter [@a]?

Upvotes: 3

Views: 2287

Answers (1)

lucidgold
lucidgold

Reputation: 4542

Try the following:

ASPX:

<asp:SqlDataSource ID="SqlDataSource1" runat="server" DataSourceMode="DataSet" 
                   ConnectionString="<%$ConnectionStrings:MyConnectionString %>" 
                   SelectCommandType="Text"
                   SelectCommand = "SELECT * FROM TABLE WHERE a=@a"  
                   CancelSelectOnNullParameter="false"> 
        <SelectParameters>
            <asp:QueryStringParameter Name="a" QueryStringField="a" 
                           DbType="String" ConvertEmptyStringToNull="true" />
              </SelectParameters>
</asp:SqlDataSource> 

C# Code-Behind:

change @a to a:

SqlDataSource2.SelectParameters.Clear();
SqlDataSource2.SelectParameters["a"].DefaultValue = cid.ToString();
SqlDataSource2.DataBind();

If @a is the FIRST parameter you can also set it using INDEX 0:

SqlDataSource2.SelectParameters.Clear();
SqlDataSource2.SelectParameters[0].DefaultValue = cid.ToString();
SqlDataSource2.DataBind();

Upvotes: 2

Related Questions