问题描述
后台代码:namespaceWebApplication1{publicpartialclassAltertable:System.Web.UI.Page{protectedvoidPage_Load(objectsender,EventArgse){if(!IsPostBack){GridView1.DataSource=GetData();GridView1.DataBind();}}publicDataViewGetData(){SqlConnectioncon=newSqlConnection(System.Configuration.ConfigurationManager.AppSettings["DBConnection"]);con.Open();SqlDataAdapterda=newSqlDataAdapter("select*fromdbo.AD_Contenta,dbo.ADdayClickCountbwherea.ADGuid=b.ADGuid",con);DataSetds=newDataSet();da.Fill(ds);da.Dispose();con.Close();con.Dispose();returnds.Tables[0].DefaultView;}publicstringTypeConvert(stringtypeID){if(typeID=="1")return"浮动广告";elseif(typeID=="2")return"弹出广告";elsereturn"";}protectedvoidButton1_Click(objectsender,EventArgse){Response.Redirect("insertAD.aspx");}protectedvoidGridView1_RowCommand(objectsender,GridViewCommandEventArgse){if(e.CommandName=="Select"){stringstr="Youwantedtochangetheline"+e.CommandArgument.ToString()+"!";Response.Write("<script>window.alert('"+str+"')</script>");intindex=Convert.ToInt32(e.CommandArgument);stringstr2=GridView1.DataKeys[index].Value.ToString();/*ADGuid*//*Session["AdId"]=str2;*/Response.Redirect("Altertable.aspx?ID="+str2);}/*elseif(e.CommandName=="Delete1"){intindex=Convert.ToInt32(e.CommandArgument);GridViewRowrow=GridView1.Rows[index];ListItemitem=newListItem();item.Text=Server.HtmlDecode(row.Cells[2].Text)+""+Server.HtmlDecode(row.Cells[3].Text);stringstr1=row.Cells[1].Text.ToString();stringstr=GridView1.DataKeys[index].ToString();stringstr2="Youwantedtochangetheline"+e.CommandArgument.ToString()+"!";Response.Write("<script>window.alert('"+str1+"')</script>");}*/}protectedvoidGridView1_RowDeleting(objectsender,GridViewDeleteEventArgse){/*intstrid=(int)GridView1.DataKeys[e.RowIndex].Value;*/intindex=e.RowIndex;GridViewRowrow=GridView1.Rows[index];stringstr1=row.Cells[1].Text.ToString();str1=row.Cells[1].Text.ToString();stringstr2=GridView1.DataKeys[index].Value.ToString();/*取出的广告ID*//*stringstr="Youwantedtodeletetheline"+"str1;"+str1+"str2;"+str2;Response.Write("<script>window.alert('"+str+"')</script>");*测试数据用*//*根据ADGuid删除数据库里的内容*/stringconstr=System.Configuration.ConfigurationManager.AppSettings["DBConnection"];SqlConnectioncon=newSqlConnection(constr);con.Open();stringsqlStr1="declare@myidvarchar(50)set@myid='"+str2+"'"+";deletefromAD_CountwhereADGuid=@myid;"+"deletefromAD_ContentwhereADGuid=@myid;";stringsqlStr2="deletefromAD_CountwhereADGuid=@myid1;"+"deletefromAD_ContentwhereADGuid=@myid2;";SqlCommandsqlCmd1=newSqlCommand(sqlStr1,con);/*SqlParameter[]arr=newSqlParameter[]{newSqlParameter("@myid1",str2),newSqlParameter("@myid2",str2)};*/sqlCmd1.ExecuteNonQuery();DataSetds=newDataSet();sqlCmd1.Dispose();con.Close();con.Dispose();Response.Write("<script>window.alert('删除成功!')</script>");Response.Redirect("Default.aspx");}protectedvoidButton2_Click(objectsender,EventArgse){}protectedvoidButton3_Click(objectsender,EventArgse){}protectedvoidGridView1_SelectedIndexChanged(objectsender,EventArgse){}protectedvoidlinkBtnUpdate_Click(objectsender,EventArgse){}}}然后调试时一直出现该怎么解决呀。。
解决方案
解决方案二:
参考:问题出现在:没有设置DATAKEYFIELD设为数据库中的ID值。因为在引用this.DataGrid1.DataKeys[e.Item.ItemIndex].ToString();之前要为DATAKEYS填充数据源,既数据库中的ID字段。解决方法:不管是DataGrid还是GridView在控件的属性窗口(其实就是aspx文件中控件属性)中直接给DataKeyField(DataKeyName)赋值为数据库的ID值,问题即可解决。
解决方案三:
DataKey是为空吧,从哪里抄的代码
解决方案四:
引用2楼duanzi_peng的回复:
DataKey是为空吧,从哪里抄的代码
是别人的代码,我拿过来改了下发现有问题,但别人同样代码却没问题
解决方案五:
引用1楼stubble的回复:
参考:问题出现在:没有设置DATAKEYFIELD设为数据库中的ID值。因为在引用this.DataGrid1.DataKeys[e.Item.ItemIndex].ToString();之前要为DATAKEYS填充数据源,既数据库中的ID字段。解决方法:不管是DataGrid还是GridView在控件的属性窗口(其实就是aspx文件中控件属性)中直接给DataKeyField(DataKeyName)赋值为数据库的ID值,问题即可解决。
那我该怎么改呢。。。我数据库中的id是ADGuid,新手求教。。