gridview摸板列添加dropdownlist的问题
代码如下,如果是datagrid好象我都是这样做没有问题
就是点击编辑列后 就 未将对象引用设置到对象的实例,找不到dropdownlist的值
int id = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Value);
string connstr = ConfigurationSettings.AppSettings["connection"] + Server.MapPath(ConfigurationSettings.AppSettings["data"]);
OleDbConnection cn = new OleDbConnection(connstr);
cn.Open();
int type=1;
for (int i = 0; i < GridView1.Rows.Count; i++)
{
DropDownList ddlType = (DropDownList)GridView1.Rows[i].FindControl("DropDownList2");
type = Convert.ToInt32(ddlType.SelectedValue);
}
string sql = "update admin set Flag = " + type + " where AdminID="+id+"";
OleDbCommand cmd = new OleDbCommand(sql, cn);
try
{
cmd.ExecuteNonQuery();
}
catch (Exception ex)
{
Response.Write(ex.ToString());
}
BindData();
报错
未将对象引用设置到对象的实例。
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。
异常详细信息: System.NullReferenceException: 未将对象引用设置到对象的实例。
源错误:
行 108: {
行 109: DropDownList ddlType = (DropDownList)GridView1.Rows[i].FindControl("DropDownList2");
行 110: type = Convert.ToInt32(ddlType.SelectedValue);
行 111: }
行 112: string sql = "update admin set Flag = " + type + " where AdminID="+id+"";
问题点数:20、回复次数:0Top




