Click here to Skip to main content
15,889,931 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more: , +
how to stop loop for blank rows in datgridview


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; string eee; string fff; string cno; string cdate; string bank; string branch;
                       ddd = dataGridView1[2, i].Value == null ? string.Empty : dataGridView1[2, i].Value.ToString();
                       eee = dataGridView1[1, i].Value == null ? string.Empty : dataGridView1[1, i].Value.ToString();
                       fff = dataGridView1[3, i].Value == null ? string.Empty : dataGridView1[3, i].Value.ToString();
                       cno = dataGridView1[5, i].Value == null ? string.Empty : dataGridView1[5, i].Value.ToString();
                       cdate = dataGridView1[6, i].Value == null ? string.Empty : dataGridView1[6, i].Value.ToString();
                       bank = dataGridView1[7, i].Value == null ? string.Empty : dataGridView1[7, i].Value.ToString();
                       branch = dataGridView1[8, i].Value == null ? string.Empty : 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();

                       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);
           }



thanks in advanced
Posted

Don't execute insert query if all values are null or string.Empty
 
Share this answer
 
Not sure if this is the best way, but you can put condition before insert code.

C#
if(!string.IsNullOrEmpty(ddd) && !string.IsNullOrEmpty(eee)) // check for all the variables
                        {

                        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);                                                                                                                                
                        cmd1.ExecuteNonQuery();
                        }
 
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