返回首页

这里是我的代码......

 if (this.MdiParent is MDIParent1)

                {

 

                    MDIParent1 css = (MDIParent1)this.MdiParent;

                    try

                    {

                        if (Compcls.Gaction == "a")

                        {

                            if (con.State == ConnectionState.Closed)

                            {

                                con.Open();

                            }

                            cmd.Transaction = transaction;

                           

                            string STR11 = "insert into ITEMMAST(ITEMCODE,COMPCODE)VALUES('" + textBox1.Text + "','" + Compcls.Gcomp_cd + "' )";

                            cmd = new SqlCommand(STR11,con,transaction);

                            cmd.ExecuteNonQuery();

                            

                            string str = "select Ac_code,broktype,brokrate,trantype,tranrate, bcycle from accountd where  COMPCODE='" + Compcls.Gcomp_cd + "'and brksys='s'";

                            SqlDataAdapter DA = new SqlDataAdapter(str, con);

                            DataSet ds = new DataSet();

                            DA.Fill(ds);

                            if (ds.Tables[0].Rows.Count > 0)

                            {

                                for (int i = 0; i < ds.Tables[0].Rows.Count; i++)

                                {

                                    string ac    = ds.Tables[0].Rows[i][0].ToString();

                                    string btype = ds.Tables[0].Rows[i][1].ToString();

                                    string brate = ds.Tables[0].Rows[i][2].ToString();

                                    string ttype = ds.Tables[0].Rows[i][3].ToString();

                                    string trate = ds.Tables[0].Rows[i][4].ToString();

                                    string ccycle = ds.Tables[0].Rows[i][5].ToString();

                                    //string brksys = dt.Tables[0].Rows[i][0].ToString();



                                    string query = "insert into PITBROK (compcode,Ac_code,broktype,brokrate,trantype,tranrate ,bcycle,ITEMCODE)VALUES('" + Compcls.Gcomp_cd + "','" + ac + "','" + btype + "','" + brate + "','" + ttype + "','" + trate + "','" + ccycle + "','" + textBox1.Text + "') ";

                                    SqlCommand cmd22 = new SqlCommand(query, con, transaction);

                                    cmd22.ExecuteNonQuery();

                                }

                                //MessageBox.Show("SAVED");

                            }

                        }

                       // transaction = con.BeginTransaction();

                        string act;

                        if (radioButton2.Checked == true)

                        {

                            act = "0";

                        }

                        else

                        {

                            act = "1";

                        }

                        string INSTP = cmbinstrutype.SelectedItem.ToString();

                        string insttype;

                        switch (INSTP)

                        {

                            case "FUTURE":

                                insttype = "F";

                                break;

                            case "OPTION":

                                insttype = "O";

                                break;

                            default:

                                insttype = "C";

                                break;

                        }

                        string STR22 = "UPDATE itemmast SET  ITEMNAME='" + textBox2.Text + "',EXHCODE ='" + textBox5.Text + "' ,LOT ='" + textBox7.Text + "',EXCHANGECODE='" + comboBox1.SelectedValue.ToString() + "',PriceUnit='" + textBox8.Text + "', Regularlot='" + textBox11.Text + "',Qtyunit='" + textBox10.Text + "',Deliverylot='" + textBox12.Text + "',deliveryunit='" + textBox9.Text + "',active='" + act + "',INSTTYPE='" + insttype + "'WHERE ITEMCODE = '" + textBox1.Text + "'AND compcode='" + Compcls.Gcomp_cd + "'";

                        cmd = new SqlCommand(STR22,con,transaction);

                        cmd.ExecuteNonQuery();

                        transaction.Commit();

                        con.Close();

                        MessageBox.Show("RECORD SAVED", "Save Record", MessageBoxButtons.OK, MessageBoxIcon.Information);

                        filllist();

                        panel1.Enabled = false;

 

                      

                    }

                    catch (SqlException sqlError) 

                    {

                        transaction.Rollback();

                        MessageBox.Show("operation stop due to error");

                      

                    }

                    finally

                    {

                        con.Close();

                    }

                }
但我得到的错误= da.fill(DS)
ExecuteReader需要有一个交易时,分配给该命令的连接处于挂起的本地事务是命令。该命令的Transaction属性尚未初始化。

[编辑]喊删除 - OriginalGriff [/编辑]

回答

评论会员:OriginalGriff 时间:2012/02/07
您已指定交易时设立的SqlCommand,但你不能在任何一点的BeginTransaction
。直到你做,你会得到一个错误
评论会员:游客 时间:2012/02/07
Sarvesh库马尔・古普塔
你没有使用你的代码中的BeginTransaction。使用这一点,如果有任何错误,现在还告诉我:帕瓦尔Anuja多尔:嗨,

你的代码看起来有点丑,你不能使用存储过程和StoredProcedure的交易?

这将提高代码的可读性。

感谢
艾米特:amitgajjar:你必须开始交易,你应该使用相同的交易数据适配器。也尝试使用一个与交易相关的命令:
-
- {A1}]
评论会员:游客 时间:2012/02/07
米卡Wendelius:如果存在(选择rollno从寄存器表条件)将返回true,如果表中存在辊登录如果没有返回false