Hi,
Before this, everything is fine when running the project After I've put "SelectedValue" below to the field,
<asp:TemplateField HeaderText="Interest">
<ItemTemplate>
<asp:DropDownList ID="ddl" OnSelectedIndexChanged="ddl_IndexChanged" SelectedValue='<%# bind("int_id") %>' AutoPostBack="true" runat="server">
</asp:DropDownList>
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList ID="ddl" OnSelectedIndexChanged="ddl_IndexChanged" SelectedValue='<%# bind("int_id") %>' AutoPostBack="true" runat="server">
</asp:DropDownList>
</EditItemTemplate>
<FooterTemplate>
<asp:DropDownList ID="ddl" OnSelectedIndexChanged="ddl_IndexChanged" SelectedValue='<%# bind("int_id") %>' AutoPostBack="true" runat="server">
</asp:DropDownList>
</FooterTemplate>
</asp:TemplateField>
I then have got this exception message
'ddl' has a SelectedValue which is invalid because it does not exist in the list of items. Parameter name: valueSystem.Web
when it is to call this event
private void BindData()
{
int userid;
bool b = int.TryParse(lb_userid.Text, out userid);
DataTable table = new DataTable();
using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["Mssqlconn2"].ConnectionString))
{
string sql = "Select * from student where teacher_id=@teacher_id";
//string sql = "Select * from student";
using (SqlCommand cmd = new SqlCommand(sql, conn))
{
using (SqlDataAdapter ad = new SqlDataAdapter(cmd))
{
cmd.Parameters.Add("@teacher_id", SqlDbType.Int).Value = userid;
ad.Fill(table);
}
}
}
GridView1.DataSource = table;
GridView1.DataBind();
}
Here are other relevant events.
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
if (e.Row.RowState == DataControlRowState.Edit)
{
DropDownList ddl = (DropDownList)e.Row.FindControl("ddl");
this.BindDropDown(ddl);
}
else
{
DropDownList ddl = (DropDownList)e.Row.FindControl("ddl");
this.BindDropDown(ddl);
}
}
if (e.Row.RowType == DataControlRowType.Footer)
{
DropDownList ddl = (DropDownList)e.Row.FindControl("ddl");
this.BindDropDown(ddl);
}
}
private void BindDropDown(DropDownList para_ddl)
{
SqlCommand cmd = new SqlCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = "select stu_id,rtrim(ltrim(cast(stu_id as varchar)))+' '+stu_name stu_name from student where stu_id>=12 order by 1";
para_ddl.DataSource = GetData(cmd);
para_ddl.DataTextField = "stu_name";
para_ddl.DataValueField = "stu_id";
para_ddl.DataBind();
para_ddl.Items.Insert(0, new ListItem("Please select"));
}