Click here to Skip to main content
15,916,693 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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()

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
Posted

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.
 
Share this answer
 
Comments
[no name] 21-Jan-12 12:18pm    
ok thanks for advice
Espen Harlinn 22-Jan-12 7:14am    
Good points :)
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
 
Share this answer
 
v2
Comments
[no name] 21-Jan-12 13:16pm    
thanks for advise HA ha ha
Sergey Alexandrovich Kryukov 21-Jan-12 14:11pm    
You are very welcome!
--SA
[no name] 21-Jan-12 14:12pm    
i m very happy ..becoz.. at least u know them????
Sergey Alexandrovich Kryukov 22-Jan-12 13:05pm    
Ha-ha, great. Give them my best regards and my great respect!

Thank you.
--SA
Espen Harlinn 22-Jan-12 7:16am    
A bit of humour is always welcome - 5'ed :)
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[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900