Reputation: 65
I've been struggling to find a solution to this, but still nothing good.
I create a GridView from code behind (as well as the SqlDataSource). In my page there is a DropDownList filled with usernames (populated in code behind).
When I select an username in the DDL, the GridView is supposed to rebind and show only the data belonging to that specific user. For some reason I can't make it work and I don't understand why.
This is my code:
Creation of GW and SqlDS:
SqlDataSource SqlDataSourceFormulariDaAppr = new SqlDataSource();
SqlDataSourceFormulariDaAppr.ID = "SqlDataSourceFormulariDaAppr";
this.Page.Controls.Add(SqlDataSourceFormulariDaAppr);
SqlDataSourceFormulariDaAppr.ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["MyConnectionString"].ConnectionString;
SqlDataSourceFormulariDaAppr.SelectCommand = "Query";
SqlDataSourceFormulariDaAppr.SelectParameters.Add("userID", DropDownListUtenti.SelectedValue);
SqlDataSourceFormulariDaAppr.DataBind();
GridViewFormulariDaAppr.DataSource = SqlDataSourceFormulariDaAppr;
GridViewFormulariDaAppr.DataBind();
SelectedIndex method:
protected void DropDownListUtenti_SelectedIndexChanged(object sender, EventArgs e)
{
GridViewFormulariDaAppr.DataBind();
}
Any help would be greatly appreciated.
Upvotes: 1
Views: 168
Reputation: 698
Good to know you found a workaround. Based on code in your question, making following changes will work for you:
SqlDataSourceFormulariDaAppr.SelectParameters.Clear();
will clear the parameters you previously added
Creation of GW and SqlDS:
SqlDataSource SqlDataSourceFormulariDaAppr = new SqlDataSource();
SqlDataSourceFormulariDaAppr.ID = "SqlDataSourceFormulariDaAppr";
this.Page.Controls.Add(SqlDataSourceFormulariDaAppr);
SqlDataSourceFormulariDaAppr.ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["MyConnectionString"].ConnectionString;
SqlDataSourceFormulariDaAppr.SelectCommand = "Query";
SqlDataSourceFormulariDaAppr.SelectParameters.Add("userID", DropDownListUtenti.SelectedValue);
SqlDataSourceFormulariDaAppr.DataBind();
GridViewFormulariDaAppr.DataSource = SqlDataSourceFormulariDaAppr;
GridViewFormulariDaAppr.DataBind();
SqlDataSourceFormulariDaAppr.SelectParameters.Clear();
SelectedIndex method:
protected void DropDownListUtenti_SelectedIndexChanged(object sender, EventArgs e)
{
populateGrid();
}
Additionally you would like to add :
SqlDataSourceFormulariDaAppr.SelectCommandType = SqlDataSourceCommandType.StoredProcedure;
before
SqlDataSourceFormulariDaAppr.SelectCommand = "Query";
Upvotes: 1
Reputation: 811
I think you'll need something like this:
Parameter param = new Parameter("userID", DbType.String, DropDownListUtenti.SelectedValue);
if (SqlDataSourceFormulariDaAppr.SelectParameters.Contains(param))
{
SqlDataSourceFormulariDaAppr.SelectParameters["userID"] = DropDownListUtenti.SelectedValue;
}
else
{
SqlDataSourceFormulariDaAppr.SelectParameters.Add(param);
}
Upvotes: 0
Reputation: 65
I found a workaround, maybe not the safest or best, but it works. I removed the parameters from the SqlDS and inserted the DropDownList selection directly in the query (I wrote a simple example now), as follows:
SqlDataSource SqlDataSourceFormulariDaAppr = new SqlDataSource();
SqlDataSourceFormulariDaAppr.ID = "SqlDataSourceFormulariDaAppr";
this.Page.Controls.Add(SqlDataSourceFormulariDaAppr);
SqlDataSourceFormulariDaAppr.ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["MyConnectionString"].ConnectionString;
SqlDataSourceFormulariDaAppr.SelectCommand = "SELECT * FROM table WHERE userID = " + DropDownListUtenti.SelectedValue + "";
SqlDataSourceFormulariDaAppr.DataBind();
GridViewFormulariDaAppr.DataSource = SqlDataSourceFormulariDaAppr;
GridViewFormulariDaAppr.DataBind();
Upvotes: 0