交易数量已经被取消 [英] Transaction no has already been decleared

查看:100
本文介绍了交易数量已经被取消的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

想要将gridview的数据插入数据库,得到错误 - >事务没有被解除Plz帮我这个。 Thanx

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

推荐答案

AddWithValue将一个参数及其值添加到当前命令。由于您是在循环中执行它,因此您需要在添加新参数之前删除参数的现有副本。

您可以做两件事:

1)把这一行放在你的循环顶部:

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 )在循环外添加没有值的参数,然后仅在循环内设置参数值。


2) Add the parameters without values outside the loop, and then set the parameter values only inside the loop.


更改下面的查询

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


这篇关于交易数量已经被取消的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆