Click here to Skip to main content
Sign Up to vote bad
good
See more: C#
want to insert data of gridview to database, got an error ->"Transaction no has already been decleared" Plz help me with this. Thanx
String query = "insert into Account_Credit(Transaction_No,Account_No,Amount_Credit,Credit_Date,Total_Amount_Credit)values(@Transaction_No,@Account_No,@Amount_Credit,@Credit_Date,@Total_Amount_Credit)";
                cmd = new SqlCommand(query, cn);
               cmd.Prepare();
 
               int i = dataGridView1.CurrentRow.Index;
               cmd.Parameters.AddWithValue("@Account_No", dataGridView1.Rows[i].Cells[0].Value);
               for (i = 0; i <= dataGridView1.Rows.Count - 1; i++)
               {
 

                   cmd.Parameters.AddWithValue("@Transaction_No", dataGridView1.Rows[i].Cells[1].Value);
                   cmd.Parameters.AddWithValue("@Amount_Credit", dataGridView1.Rows[i].Cells[2].Value);
                   cmd.Parameters.AddWithValue("@Credit_Date", dataGridView1.Rows[i].Cells[3].Value);
                   cmd.Parameters.AddWithValue("@Total_Amount_Credit", dataGridView1.Rows[i].Cells[4].Value);
                   cn.Open();
 

                   cmd.ExecuteNonQuery();
                   cn.Close();
               }
Posted 30 Dec '12 - 0:22
Edited 30 Dec '12 - 0:34

Comments
jibesh - 30 Dec '12 - 6:43
double check the order of the parameters added to the SQLCmd and the order in query are matched. The First Parameter Passed to the SqlCmd is AcctionNo but inside the insert query its Transaction_No. Check that
zeshanazam - 30 Dec '12 - 6:47
u are wrong
jibesh - 30 Dec '12 - 6:50
can you post the exception stack what you are getting for better understanding of the issue
zeshanazam - 30 Dec '12 - 6:52
loop works only for one time and then error occurs Transaction no has already been decleared
jibesh - 30 Dec '12 - 7:10
moment... what are you doing with query execution inside the loop? that means you are adding the parameters to the sqlCmd for each row. you need to reconsider the logic here. i.e starting from Transaction_No the parameters are added again and again to the sql Cmd.

2 solutions

AddWithValue adds a parameter with its value to the current command. Since you are executing it in a loop, you need to remove existing copies of the parameters before you add new ones.
There are two things you can do:
1) Put this line at teh top of your loop:
cmd.Parameters.Clear();
2) Add the parameters without values outside the loop, and then set the parameter values only inside the loop.
  Permalink  
Change the query like below
String query = "insert into Account_Credit(Transaction_No,Account_No,Amount_Credit,Credit_Date,Total_Amount_Credit)values(@Transaction_No,@Account_No,@Amount_Credit,@Credit_Date,@Total_Amount_Credit)";
          
int i = dataGridView1.CurrentRow.Index;
              
for (i = 0; i <= dataGridView1.Rows.Count - 1; i++)
{
    cmd = new SqlCommand(query, cn);
    cmd.Prepare();
    cmd.Parameters.AddWithValue("@Account_No", dataGridView1.Rows[i].Cells[0].Value);
 
    cmd.Parameters.AddWithValue("@Transaction_No", dataGridView1.Rows[i].Cells[1].Value);
    cmd.Parameters.AddWithValue("@Amount_Credit", dataGridView1.Rows[i].Cells[2].Value);
    cmd.Parameters.AddWithValue("@Credit_Date", dataGridView1.Rows[i].Cells[3].Value);
    cmd.Parameters.AddWithValue("@Total_Amount_Credit", dataGridView1.Rows[i].Cells[4].Value);
    cn.Open();
 
    cmd.ExecuteNonQuery();
    cn.Close();
}
  Permalink  

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

  Print Answers RSS
Your Filters
Interested
Ignored
     
0 Arun Vasu 339
1 OriginalGriff 320
2 Sergey Alexandrovich Kryukov 315
3 Tadit Dash 221
4 CPallini 178
0 Sergey Alexandrovich Kryukov 9,955
1 OriginalGriff 7,589
2 CPallini 4,028
3 Rohan Leuva 3,422
4 Maciej Los 2,949


Advertise | Privacy | Mobile
Web02 | 2.6.130523.1 | Last Updated 30 Dec 2012
Copyright © CodeProject, 1999-2013
All Rights Reserved. Terms of Use
Layout: fixed | fluid