如何在数据库中保存datagridview的空单元格 [英] how to save empty cell of datagridview in database
问题描述
你好朋友,我的项目中有datagridview,其中有8列.
在许多情况下,用户不输入.银行名称,分行等,但是当我单击保存"按钮时
它得到错误=对象引用未设置为对象的实例
这是我的代码
for (int i = 0; i < dataGridView1.Rows.Count; ) { AC_code = dataGridView1[0, i].Value.ToString(); SqlCommand cmd3 = new SqlCommand("select AC_CODE from accountd where compcode=''" + Compcls.Gcomp_cd + "''and Name=''" + AC_code + "''", con, trans); AC_cod = cmd3.ExecuteScalar().ToString(); string ddd = dataGridView1[2, i].Value.ToString(); string eee = dataGridView1[1, i].Value.ToString(); string fff = dataGridView1[3, i].Value.ToString();// string cno = dataGridView1[5, i].Value.ToString(); string cdate= dataGridView1[6, i].Value.ToString(); string bank = dataGridView1[7, i].Value.ToString(); string branch = dataGridView1[8, i].Value.ToString(); string dgdat = "INSERT INTO VCHAMT (compcode,VOU_TYPE,VOU_NO,AMOUNT,VOU_DT,DR_CR,NARRATION,AC_CODE,CHEQUE_NO,CHEQUE_DT,BANK_NAME,BRANCH)values(''" + Compcls.Gcomp_cd + "'',''" + vctype + "'',''" + txtvoucherno.Text + "'',''" + ddd + "'',''" + (Convert.ToDateTime(dateTimePicker1.Text)).ToString("yyyy/MM/dd") + "'',''" + eee + "'',''" + fff + "'',''" + AC_cod + "'',''"+cno+"'',''"+cdate+"'',''"+bank+"'',''"+branch+"'')"; SqlCommand cmd1 = new SqlCommand(dgdat, con, trans); //cash_c_d= sum > 0 ? cash_c_d = "D" : cash_c_d = "C"; cmd1.ExecuteNonQuery();//,CHEQUE_NO,CHEQUE_DT,BANK_NAME,BRANCH i++; } trans.Commit(); toolTip1.IsBalloon = true; toolTip1.ToolTipIcon = ToolTipIcon.Info; toolTip1.ToolTipTitle = "SAVED"; toolTip1.Show("SAVED", mainpanell, 0, 0, 2000); } catch (Exception Ex) { trans.Rollback(); MessageBox.Show(Ex.Message); }
代替ToString(),使用Convert.tostring()来处理空值.
喜欢,
字符串ddd = Convert.Tostring(dataGridView1 [ 2 ,i] .Value);
或检查gridview单元格值是否为null,然后将该值分配给该变量.否则分配空值.
意见建议:
请勿将内联插入值使用过多.最佳做法是调用存储过程并将这些值作为参数传递.
希望对您有所帮助.
示例:
字符串ddd = dataGridView1 [2,i] .Value.ToString();
必须是:
字符串ddd = dataGridView1 [2,i] .Value == null? string.Empty:dataGridView1 [2,i] .Value.ToString();
在您的数据库中:
插入VCHAMT(compcode,VOU_TYPE,VOU_NO,AMOUNT,VOU_DT,DR_CR,NARRATION,AC_CODE,CHEQUE_NO,CHEQUE_DT,BANK_NAME,BRANCH)值(''"+ Compcls.Gcomp_cd +"'',''"+ 字符串IsNullOrEmpty(vctype)?string.Empty:vcType +'',''" + txtvoucherno.Text +"," + ddd +'',""+(Convert.ToDateTime (dateTimePicker1.Text)).ToString("yyyy/MM/dd")+'',""+ eee +"''," + fff +'',""+ AC_cod +"' ',''"+ cno +"'',''"+ cdate +"'',''"+ bank +"'',''"+ branch +"'');
>
hello friend i have datagridview in my project which have 8 column.
at many cases user not Enter. bank name ,branch,and etc but when i click on save button
it get error=object reference not set to an instance of an object
here is my code
for (int i = 0; i < dataGridView1.Rows.Count; ) { AC_code = dataGridView1[0, i].Value.ToString(); SqlCommand cmd3 = new SqlCommand("select AC_CODE from accountd where compcode=''" + Compcls.Gcomp_cd + "''and Name=''" + AC_code + "''", con, trans); AC_cod = cmd3.ExecuteScalar().ToString(); string ddd = dataGridView1[2, i].Value.ToString(); string eee = dataGridView1[1, i].Value.ToString(); string fff = dataGridView1[3, i].Value.ToString();// string cno = dataGridView1[5, i].Value.ToString(); string cdate= dataGridView1[6, i].Value.ToString(); string bank = dataGridView1[7, i].Value.ToString(); string branch = dataGridView1[8, i].Value.ToString(); string dgdat = "INSERT INTO VCHAMT (compcode,VOU_TYPE,VOU_NO,AMOUNT,VOU_DT,DR_CR,NARRATION,AC_CODE,CHEQUE_NO,CHEQUE_DT,BANK_NAME,BRANCH)values(''" + Compcls.Gcomp_cd + "'',''" + vctype + "'',''" + txtvoucherno.Text + "'',''" + ddd + "'',''" + (Convert.ToDateTime(dateTimePicker1.Text)).ToString("yyyy/MM/dd") + "'',''" + eee + "'',''" + fff + "'',''" + AC_cod + "'',''"+cno+"'',''"+cdate+"'',''"+bank+"'',''"+branch+"'')"; SqlCommand cmd1 = new SqlCommand(dgdat, con, trans); //cash_c_d= sum > 0 ? cash_c_d = "D" : cash_c_d = "C"; cmd1.ExecuteNonQuery();//,CHEQUE_NO,CHEQUE_DT,BANK_NAME,BRANCH i++; } trans.Commit(); toolTip1.IsBalloon = true; toolTip1.ToolTipIcon = ToolTipIcon.Info; toolTip1.ToolTipTitle = "SAVED"; toolTip1.Show("SAVED", mainpanell, 0, 0, 2000); } catch (Exception Ex) { trans.Rollback(); MessageBox.Show(Ex.Message); }
Hi,
Instead of ToString(), use Convert.tostring() as it handles null values.
like,
string ddd =Convert.Tostring(dataGridView1[2,i].Value);
or check the gridview cellvalue isnull then assign the value to the variable.else assign null value.
Suggestion:
Do not use the inline insert with too many values.Best practice is to call a stored procedure and pass the values as parameters.
Hope this helps.
example:
string ddd = dataGridView1[2, i].Value.ToString();
must be:
string ddd = dataGridView1[2, i].Value == null ? string.Empty: dataGridView1[2, i].Value.ToString();
In your db:
INSERT INTO VCHAMT (compcode,VOU_TYPE,VOU_NO,AMOUNT,VOU_DT,DR_CR,NARRATION,AC_CODE,CHEQUE_NO,CHEQUE_DT,BANK_NAME,BRANCH)values(''" + Compcls.Gcomp_cd + "'',''" + string.IsNullOrEmpty(vctype) ? string.Empty : vcType + "'',''" + txtvoucherno.Text + "'',''" + ddd + "'',''" + (Convert.ToDateTime(dateTimePicker1.Text)).ToString("yyyy/MM/dd") + "'',''" + eee + "'',''" + fff + "'',''" + AC_cod + "'',''"+cno+"'',''"+cdate+"'',''"+bank+"'',''"+branch+"'')";
这篇关于如何在数据库中保存datagridview的空单元格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!