why i can't write my data to SQLServer?
-
:confused:In a DataGrid, thare are A、B、C、D TemplateColumn,all bind with CheckBox, ID A B C D 1 口 口 口 口 updatecommand 2 口 口 口 口 updatecommand "口" means CheckBox My updatecommand code like: I can't find where I make a mistake! there are any mistake hints! public void DataGrid_UpdateCommand(object sender, DataGridCommandEventArgs e) { SqlConnection MyCn = new SqlConnection(strConn); CheckBox a = (CheckBox)e.Item.FindControl("CheckBox1"); CheckBox b = (CheckBox)e.Item.FindControl("CheckBox2"); CheckBox c = (CheckBox)e.Item.FindControl("CheckBox3"); CheckBox d = (CheckBox)e.Item.FindControl("CheckBox4"); string createnew = "0"; string deletelist = "0"; string modify = "0"; string readlist = "1"; string strSQL = "update table1 set createnew = '"+ createnew +"',deletelist = '"+ deletelist +"',modify = '"+ modify +"',readlist = '"+ readlist +"' where id= " + DataGrid1.DataKeys[(int)e.Item.ItemIndex].ToString(); SqlCommand MyCmd = new SqlCommand(strSQL,MyCn); if(a.Checked == true) createnew = "1"; if(b.Checked ==true) deletelist = "1"; if(c.Checked ==true) modify = "1"; if(d.Checked ==true) readlist = "1"; MyCn.Open(); MyCmd.ExecuteNonQuery(); MyCn.Close(); DataGrid1.EditItemIndex = -1; BindGrid(); } ---------------------------------------------------------------- One is never too old to learn
-
:confused:In a DataGrid, thare are A、B、C、D TemplateColumn,all bind with CheckBox, ID A B C D 1 口 口 口 口 updatecommand 2 口 口 口 口 updatecommand "口" means CheckBox My updatecommand code like: I can't find where I make a mistake! there are any mistake hints! public void DataGrid_UpdateCommand(object sender, DataGridCommandEventArgs e) { SqlConnection MyCn = new SqlConnection(strConn); CheckBox a = (CheckBox)e.Item.FindControl("CheckBox1"); CheckBox b = (CheckBox)e.Item.FindControl("CheckBox2"); CheckBox c = (CheckBox)e.Item.FindControl("CheckBox3"); CheckBox d = (CheckBox)e.Item.FindControl("CheckBox4"); string createnew = "0"; string deletelist = "0"; string modify = "0"; string readlist = "1"; string strSQL = "update table1 set createnew = '"+ createnew +"',deletelist = '"+ deletelist +"',modify = '"+ modify +"',readlist = '"+ readlist +"' where id= " + DataGrid1.DataKeys[(int)e.Item.ItemIndex].ToString(); SqlCommand MyCmd = new SqlCommand(strSQL,MyCn); if(a.Checked == true) createnew = "1"; if(b.Checked ==true) deletelist = "1"; if(c.Checked ==true) modify = "1"; if(d.Checked ==true) readlist = "1"; MyCn.Open(); MyCmd.ExecuteNonQuery(); MyCn.Close(); DataGrid1.EditItemIndex = -1; BindGrid(); } ---------------------------------------------------------------- One is never too old to learn
What does your strSql string have in it when you create your command object? Is it what you want? Does it update your db when you run it in QA? It looks odd to me that you create the initialize the string with the value createnew, deletelist, modify, and readlist but change them after you have created the string. Is strconn a global variable? Do you receive any error msgs when running the execute nonQuery method? Some quick advice... Wrap you db calls in a try catch finally block and call dispose on these objects to release your sources also look into using parameterized queries. HTH, Daniel
-
:confused:In a DataGrid, thare are A、B、C、D TemplateColumn,all bind with CheckBox, ID A B C D 1 口 口 口 口 updatecommand 2 口 口 口 口 updatecommand "口" means CheckBox My updatecommand code like: I can't find where I make a mistake! there are any mistake hints! public void DataGrid_UpdateCommand(object sender, DataGridCommandEventArgs e) { SqlConnection MyCn = new SqlConnection(strConn); CheckBox a = (CheckBox)e.Item.FindControl("CheckBox1"); CheckBox b = (CheckBox)e.Item.FindControl("CheckBox2"); CheckBox c = (CheckBox)e.Item.FindControl("CheckBox3"); CheckBox d = (CheckBox)e.Item.FindControl("CheckBox4"); string createnew = "0"; string deletelist = "0"; string modify = "0"; string readlist = "1"; string strSQL = "update table1 set createnew = '"+ createnew +"',deletelist = '"+ deletelist +"',modify = '"+ modify +"',readlist = '"+ readlist +"' where id= " + DataGrid1.DataKeys[(int)e.Item.ItemIndex].ToString(); SqlCommand MyCmd = new SqlCommand(strSQL,MyCn); if(a.Checked == true) createnew = "1"; if(b.Checked ==true) deletelist = "1"; if(c.Checked ==true) modify = "1"; if(d.Checked ==true) readlist = "1"; MyCn.Open(); MyCmd.ExecuteNonQuery(); MyCn.Close(); DataGrid1.EditItemIndex = -1; BindGrid(); } ---------------------------------------------------------------- One is never too old to learn
You have not told us what the actual error is, post it and we can help you more. However have you checked to make sure you are skipping your DataBind in the Load event if it is a postback? You use
IsPostBack
to check for this. regards, Paul Watson Bluegrass South Africa Marc Clifton wrote: "organically fed captivity free regurgitated bee nectar" (honey) on dieting. Crikey! ain't life grand? -
You have not told us what the actual error is, post it and we can help you more. However have you checked to make sure you are skipping your DataBind in the Load event if it is a postback? You use
IsPostBack
to check for this. regards, Paul Watson Bluegrass South Africa Marc Clifton wrote: "organically fed captivity free regurgitated bee nectar" (honey) on dieting. Crikey! ain't life grand?