如何在datgrid视图中处理空值 [英] how to Handel null value in datgrid view

查看:67
本文介绍了如何在datgrid视图中处理空值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的代码

here is my code

for (int i = 0; i < dataGridView1.Rows.Count; i++)
                {
                SqlCommand cmd3 = new SqlCommand("select AC_CODE from accountd where compcode=''" + Compcls.Gcomp_cd + "''and Name=''" + dataGridView1[0, i].Value.ToString() + "''", con, trans);
                 AC_cod = cmd3.ExecuteScalar().ToString();

                 string dgdat = "INSERT INTO VCHAMT (compcode,VOU_TYPE,VOU_NO,AMOUNT,VOU_DT,DR_CR,NARRATION,AC_CODE)values(''" + Compcls.Gcomp_cd + "'',''" + vctype + "'',''" + txtvoucherno.Text + "'',''" + dataGridView1[2, i].Value.ToString() + "'',''" + (Convert.ToDateTime(dateTimePicker1.Text)).ToString("yyyy/MM/dd") + "'',''" + dataGridView1[1, i].Value.ToString() + "'',''"+dataGridView1[3,i].Value.ToString()+"'',''" + AC_cod + "'')";
                SqlCommand cmd1 = new SqlCommand(dgdat, con, trans);
                cmd1.ExecuteNonQuery();//,CHEQUE_NO,CHEQUE_DT,BANK_NAME,BRANCH
              }




如果用户未在




if user not enter value in

dataGridView1[1, i].Value.ToString()


中输入值 它给出了error =


it gives error=

Object reference not set to an instance of an object.

我想要处理错误,任何伙伴都可以告诉我有关如何处理此错误的任何想法.

i want handle error can any buddy tell me any idea that how handle this error

推荐答案

停止使用字符串连接来形成SQL语句,并且 从不 使用未经验证的用户输入EVER.您应该至少使用参数化查询或存储过程.
Stop using string concatenation to form your SQL statements and NEVER use unvalidated user input, EVER. You should be using paramterized queries at the least or stored procedures.


您确定要处理此值,而不是Mozart吗? :-)

参见:
http://en.wikipedia.org/wiki/Handel [ http://en.wikipedia.org/wiki/Mozart [ http://en.wikipedia.org/wiki/SeaMonkey [ http://www.seamonkey-project.org/ [ http://en.wikipedia.org/wiki/Firefox [ http://www.mozilla.org/en-US/firefox/fx/ [ ^ ].

这样,您可以大大提高获得有用建议的机会.也许比这更有用. :-)

—SA
Are you sure you want to Handel this value, not Mozart it? :-)

See:
http://en.wikipedia.org/wiki/Handel[^],
http://en.wikipedia.org/wiki/Mozart[^].

Please use the spellchecker which you can get free of charge; just for for example: excellent Mozilla Web browsers spell-check all input fields:
http://en.wikipedia.org/wiki/SeaMonkey[^],
http://www.seamonkey-project.org/[^],
http://en.wikipedia.org/wiki/Firefox[^],
http://www.mozilla.org/en-US/firefox/fx/[^].

This way, you can greatly improve your chance to get useful advice. Perhaps even more useful than this one. :-)

—SA


我想对Mark的关于学习使用参数的评论表示赞同.他们总是一个好主意.至于空值的问题,您可以在运行需要爆炸的代码之前专门检查它.在此MSDN链接中查看有关如何使用dbnull.value的示例:
http://msdn.microsoft.com/en-us/library/system.dbnull. value.aspx [ ^ ]
I''d like to second Mark''s comment about learning to use parameters. They are always a good idea. As for your problem with the null value, you can specifically check for it before running the code that blows up if you need to. See an example of how to use dbnull.value in this MSDN link:
http://msdn.microsoft.com/en-us/library/system.dbnull.value.aspx[^]


这篇关于如何在datgrid视图中处理空值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆