Click here to Skip to main content
15,902,915 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
private void toolStripButton3_Click(object sender, EventArgs e)
        {
            if (textBox14.Text.Equals(textBox15.Text))
            {
                try
                {
                    SqlCommand sqlcmd = sqlconn.CreateCommand();
                    foreach (DataGridViewRow dr in dataGridView1.Rows)
                    {
                        sqlcmd.CommandText = "set identity_insert gl_Transaction On";
                        sqlcmd.CommandText = "insert into Gl_Transaction(Compcode,Branchcode,Accountno,Acct_Name,SerialNo,Acct_Nirration,Value_Date,Voucher_No,Vchrtype,Dr_Amount,Cr_Amount,AddDate,UserId,pflag,InstrumentNo) values('" + dr.Cells[0].Value.ToString() + "','" + dr.Cells[1].Value.ToString() + "','" + dr.Cells[2].Value.ToString() + "','" + dr.Cells[3].Value.ToString() + "','" + dr.Cells[4].Value.ToString() + "','" + dr.Cells[5].Value.ToString() + "','" + dr.Cells[6].Value.ToString() + "','" + dr.Cells[7].Value.ToString() + "','" + dr.Cells[8].Value.ToString() + "','" + dr.Cells[9].Value.ToString() + "','" + dr.Cells[10].Value.ToString() + "','" + dr.Cells[11].Value.ToString() + "','" + dr.Cells[12].Value.ToString() + "','" + dr.Cells[13].Value.ToString() + "','" + dr.Cells[14].Value.ToString() + "')";
                        sqlcmd.CommandText = "set identity_insert gl_Transaction Off";
                        try
                        {
                            sqlcmd.ExecuteNonQuery();
                        }
                        catch (SqlException err)
                        {
                            MessageBox.Show(err.Message);
                        }
                        MessageBox.Show("Record Entered Successfull");


                    }

                }
                catch (Exception)
                {
                    throw;


                }
            }
            else
            {
                MessageBox.Show("Debit and credit are not equal");


            }


There is no error in this code... but data are not going in table... wats the error...

i am sending deata through gridview two rows at a single click...
Posted
Comments
[no name] 19-Apr-14 11:08am    
You should not be doing this at all. Use a parameterized query.

1 solution

Try the following :
SQL
...
sqlcmd.CommandText = "set identity_insert gl_Transaction On;";
sqlcmd.CommandText += "insert into Gl_Transaction(Compcode,Branchcode,Accountno,Acct_Name,SerialNo,Acct_Nirration,Value_Date,Voucher_No,Vchrtype,Dr_Amount,Cr_Amount,AddDate,UserId,pflag,InstrumentNo) values('" + dr.Cells[0].Value.ToString() + "','" + dr.Cells[1].Value.ToString() + "','" + dr.Cells[2].Value.ToString() + "','" + dr.Cells[3].Value.ToString() + "','" + dr.Cells[4].Value.ToString() + "','" + dr.Cells[5].Value.ToString() + "','" + dr.Cells[6].Value.ToString() + "','" + dr.Cells[7].Value.ToString() + "','" + dr.Cells[8].Value.ToString() + "','" + dr.Cells[9].Value.ToString() + "','" + dr.Cells[10].Value.ToString() + "','" + dr.Cells[11].Value.ToString() + "','" + dr.Cells[12].Value.ToString() + "','" + dr.Cells[13].Value.ToString() + "','" + dr.Cells[14].Value.ToString() + "')";
sqlcmd.CommandText += ";set identity_insert gl_Transaction Off";
...


[Edit]
If you need to use compound sql statements i.e. more than 1 then each one should end with a ';' semicolon.

Also in the case above you need to concat strings for the CommandText with += otherwise they get overwritten.
 
Share this answer
 
v2
Comments
Member 10690757 19-Apr-14 10:59am    
whats the difference in it???
Mehdi Gholam 19-Apr-14 11:02am    
2 semicolons and 2 plus signs
Member 10690757 19-Apr-14 11:06am    
thanx a lot :) it is working??? but wat is the function of plus sign and semicolons?
Mehdi Gholam 19-Apr-14 11:17am    
See the updated solution.
Member 10690757 19-Apr-14 12:10pm    
thanx 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