Click here to Skip to main content
16,018,264 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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);
           }
Posted
Comments
Richard MacCutchan 27-Jan-12 7:36am    
Which line gives the error? This message indicates that you have not initialised some object that you are trying to use.
[no name] 27-Jan-12 7:41am    
ya u r right ...min i want to blank cell if user left it blank
[no name] 27-Jan-12 7:42am    
sry i want to save blank cell if user left it blank

Hi,

Instead of ToString(), use Convert.tostring() as it handles null values.

like,
SQL
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.
 
Share this answer
 
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+"')";
 
Share this answer
 
Comments
[no name] 27-Jan-12 8:27am    
thanks digimanus ...this exactly which i want ...again thanks a lot

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