ทำยังไง กับ Local Transaction ดีครับ ExecuteReader requires the command to have a transaction when the connection assigned to the command is in a pending local transaction. The Transaction property of the command has not been initialized
คือผมเจอ Error แบบนี้อะ
ExecuteReader requires the command to have a transaction when the connection assigned to the command is in a pending local transaction. The Transaction property of the command has not been initialized.
Code (C#)
private void insertNewShip()
{
string strConn = "Data Source=MIXAR-PC;Initial Catalog=StockProductInfo;Integrated Security=True";
Conn = new SqlConnection(strConn);
if (Conn.State == ConnectionState.Open)
{
Conn.Close();
}
Conn.ConnectionString = strConn;
Conn.Open();
tr1 = Conn.BeginTransaction();
sb1.Append("INSERT INTO tranShipping ");
sb1.Append("(Ship_ID, Ship_Date, Ship_Time, ");
sb1.Append("From_Location_ID, Staff_ID, Ship_Complete) ");
sb1.Append("VALUES (");
sb1.Append("@ShipID, ");
sb1.Append("@ShipDate, ");
sb1.Append("@ShipTime, ");
sb1.Append("@LocationID, ");
sb1.Append("@StaffID, ");
sb1.Append("@ShipComplete );");
string sqlInsertShip1 = sb1.ToString();
com1.CommandType = CommandType.Text;
com1.CommandText = sqlInsertShip1;
com1.Connection = Conn;
com1.Transaction = tr1;
com1.Parameters.Clear();
com1.Parameters.Add("@ShipID", SqlDbType.VarChar).Value = Session["shipID"].ToString();
com1.Parameters.Add("@ShipDate", SqlDbType.VarChar).Value = DateTime.Now.ToShortDateString();
com1.Parameters.Add("@ShipTime", SqlDbType.VarChar).Value = DateTime.Now.ToLongTimeString();
com1.Parameters.Add("@LocationID", SqlDbType.VarChar).Value = txtLocationID.Text.Trim();
com1.Parameters.Add("@StaffID", SqlDbType.VarChar).Value = txtStaffID.Text.Trim();
com1.Parameters.Add("@ShipComplete", SqlDbType.VarChar).Value = "incomplete";
com1.ExecuteNonQuery();
tr1.Commit();
tr2 = Conn.BeginTransaction();
sb2.Append("INSERT INTO tranOrderShipping ");
sb2.Append("(Ship_ID, Order_ID, Prod_Barcode, ");
sb2.Append("Prod_Qty, WHP_ID) ");
sb2.Append("VALUES (");
sb2.Append("@ShipID, ");
sb2.Append("@OrderID, ");
sb2.Append("@ProdBarcode, ");
sb2.Append("@ProdQty, ");
sb2.Append("@WHP_ID );");
foreach (GridViewRow r in dgvShipList.Rows)
{
string barcode = r.Cells[0].Text;
string orderID = r.Cells[1].Text;
string WHPid = r.Cells[5].Text;
int qty = int.Parse(r.Cells[8].Text);
string sqlInsertShip2 = sb2.ToString();
com2.CommandType = CommandType.Text;
com2.CommandText = sqlInsertShip2;
com2.Connection = Conn;
com2.Transaction = tr2;
com2.Parameters.Clear();
com2.Parameters.Add("@ShipID", SqlDbType.VarChar).Value = Session["shipID"].ToString();
com2.Parameters.Add("@OrderID", SqlDbType.VarChar).Value = orderID;
com2.Parameters.Add("@ProdBarcode", SqlDbType.VarChar).Value = barcode;
com2.Parameters.Add("@ProdQty", SqlDbType.Int).Value = qty;
com2.Parameters.Add("@WHP_ID", SqlDbType.VarChar).Value = WHPid;
com2.ExecuteNonQuery();
}
tr2.Commit();
tr3 = Conn.BeginTransaction();
sb3.Append("INSERT INTO tranStock ");
sb3.Append("(Prod_Barcode, WHP_ID, Date, Time, ");
sb3.Append("Prod_ProduceQty, Prod_SaleQty, Prod_ReturnQty ) ");
sb3.Append("VALUES (");
sb3.Append("@Barcode, ");
sb3.Append("@WHPid, ");
sb3.Append("@Date, ");
sb3.Append("@Time, ");
sb3.Append("@ProduceQty, ");
sb3.Append("@SaleQty, ");
sb3.Append("@ReturnQty );");
foreach (GridViewRow r in dgvShipList.Rows)
{
string barcode = r.Cells[0].Text;
string WHPid = r.Cells[5].Text;
int qty = int.Parse(r.Cells[8].Text);
string sqlInsertShip3 = sb3.ToString();
com3.CommandType = CommandType.Text;
com3.CommandText = sqlInsertShip3;
com3.Connection = Conn;
com3.Transaction = tr3;
com3.Parameters.Clear();
com3.Parameters.Add("@Barcode", SqlDbType.VarChar).Value = barcode;
com3.Parameters.Add("@WHPid", SqlDbType.VarChar).Value = WHPid;
com3.Parameters.Add("@Date", SqlDbType.VarChar).Value = DateTime.Now.ToShortDateString();
com3.Parameters.Add("@Time", SqlDbType.VarChar).Value = DateTime.Now.ToLongTimeString();
com3.Parameters.Add("@ProduceQty", SqlDbType.Int).Value = 0;
com3.Parameters.Add("@SaleQty", SqlDbType.Int).Value = qty;
com3.Parameters.Add("@ReturnQty", SqlDbType.Int).Value = 0;
com3.ExecuteNonQuery();
}
tr3.Commit();
tr4 = Conn.BeginTransaction();
sb4.Append("UPDATE masStock ");
sb4.Append("SET Prod_TotalQty = @QtyTemp ");
sb4.Append(", Prod_QtyInStock = @Qty ");
sb4.Append("WHERE Prod_Barcode = @Barcode ");
foreach (GridViewRow r in dgvShipList.Rows)
{
string barcode = r.Cells[0].Text;
int qtyInStock = int.Parse(r.Cells[7].Text);
int qtyInput = int.Parse(r.Cells[8].Text);
int newQty = qtyInStock - qtyInput;
string sqlUpdateStock = sb4.ToString();
com4.CommandType = CommandType.Text;
com4.CommandText = sqlUpdateStock;
com4.Connection = Conn;
com4.Transaction = tr4;
com4.Parameters.Clear();
com4.Parameters.Add("@QtyTemp", SqlDbType.Int).Value = newQty;
com4.Parameters.Add("@Qty", SqlDbType.Int).Value = newQty;
com4.Parameters.Add("@Barcode", SqlDbType.VarChar).Value = barcode;
com4.ExecuteNonQuery();
}
tr4.Commit();
tr5 = Conn.BeginTransaction();
sb5.Append("UPDATE tranProductOrder ");
sb5.Append("SET PO_ProdQty_Shipped = @QtyShipped ");
sb5.Append("WHERE Prod_Barcode = @Barcode ");
sb5.Append("AND Order_ID = @OrderID ");
foreach (GridViewRow r in dgvShipList.Rows)
{
string barcode = r.Cells[0].Text;
string orderID = r.Cells[1].Text;
int qtyInput = int.Parse(r.Cells[8].Text);
string sqlOrderQty = "select PO_ProdQty, PO_ProdQty_Shipped ";
sqlOrderQty += "from tranProductOrder ";
sqlOrderQty += "where Prod_Barcode = " + barcode;
sqlOrderQty += "and Order_ID = " + orderID;
SqlDataAdapter da1 = new SqlDataAdapter(sqlOrderQty, Conn);
DataSet ds1 = new DataSet();
da1.Fill(ds1, "OrderQty");
int orderQty = int.Parse(ds1.Tables["OrderQty"].Rows[0]["PO_ProdQty"].ToString());
int shipQty = int.Parse(ds1.Tables["OrderQty"].Rows[0]["PO_ProdQty_Shipped"].ToString());
int newShipQty = qtyInput + shipQty;
string sqlUpdateOrder = sb5.ToString();
com5.CommandType = CommandType.Text;
com5.CommandText = sqlUpdateOrder;
com5.Connection = Conn;
com5.Transaction = tr5;
com5.Parameters.Clear();
com5.Parameters.Add("@QtyShipped", SqlDbType.Int).Value = newShipQty;
com5.Parameters.Add("@Barcode", SqlDbType.VarChar).Value = barcode;
com5.Parameters.Add("@OrderID", SqlDbType.VarChar).Value = orderID;
com5.ExecuteNonQuery();
}
tr5.Commit();
tr6 = Conn.BeginTransaction();
sb6.Append("UPDATE tranProductOrder ");
sb6.Append("SET Prod_Complete = @Status ");
sb6.Append("WHERE Prod_Barcode = @Barcode ");
sb6.Append("AND Order_ID = @OrderID ");
foreach (GridViewRow r in dgvShipList.Rows)
{
string barcode = r.Cells[0].Text;
string orderID = r.Cells[1].Text;
string sqlOrderQty = "select PO_ProdQty, PO_ProdQty_Shipped ";
sqlOrderQty += "from tranProductOrder ";
sqlOrderQty += "where Prod_Barcode = " + barcode;
sqlOrderQty += "and Order_ID = " + orderID;
SqlDataAdapter da2 = new SqlDataAdapter(sqlOrderQty, Conn);
DataSet ds2 = new DataSet();
da2.Fill(ds2, "OrderQty");
string sqlUpdItemStatus = sb6.ToString();
com6.CommandType = CommandType.Text;
com6.CommandText = sqlUpdItemStatus;
com6.Connection = Conn;
com6.Transaction = tr6;
com6.Parameters.Clear();
com6.Parameters.Add("@Barcode", SqlDbType.VarChar).Value = barcode;
com6.Parameters.Add("@OrderID", SqlDbType.VarChar).Value = orderID;
if (ds2.Tables["OrderQty"].Rows[0]["PO_ProdQty"].ToString() == ds2.Tables["OrderQty"].Rows[0]["PO_ProdQty_Shipped"].ToString())
{
com6.Parameters.Add("@Status", SqlDbType.VarChar).Value = "complete";
}
else
{
com6.Parameters.Add("@Status", SqlDbType.VarChar).Value = "incomplete";
}
com6.ExecuteNonQuery();
}
tr6.Commit();
tr7 = Conn.BeginTransaction();
sb7.Append("UPDATE tranOrder ");
sb7.Append("SET Order_Complete = @Status ");
sb7.Append("WHERE Order_ID = @OrderID ");
foreach (GridViewRow r in dgvShipList.Rows)
{
string barcode = r.Cells[0].Text;
string orderID = r.Cells[1].Text;
string sqlChkOrdStatus = "select Prod_Barcode, Prod_Complete";
sqlChkOrdStatus += "from tranProductOrder ";
sqlChkOrdStatus += "where Order_ID = " + orderID;
SqlDataAdapter da3 = new SqlDataAdapter(sqlChkOrdStatus, Conn);
DataSet ds3 = new DataSet();
da3.Fill(ds3, "ItemStatus");
string sqlUpdOrderStatus = sb7.ToString();
com7.CommandType = CommandType.Text;
com7.CommandText = sqlUpdOrderStatus;
com7.Connection = Conn;
com7.Transaction = tr7;
com7.Parameters.Clear();
int count = 0;
for (int i = 0; i < ds3.Tables["ItemStatus"].Rows.Count; i++)
{
if (ds3.Tables["ItemStatus"].Rows[i]["Prod_Complete"].ToString()=="complete")
{
count++;
}
}
if (count == ds3.Tables["ItemStatus"].Rows.Count)
{
com7.Parameters.Add("@Status", SqlDbType.VarChar).Value = "complete";
}
else
{
com7.Parameters.Add("@Status", SqlDbType.VarChar).Value = "incomplete";
}
com7.Parameters.Add("@OrderID", SqlDbType.VarChar).Value = orderID;
com7.ExecuteNonQuery();
}
tr7.Commit();
}
คือผมสามารถ execute ได้แค่ 4 transaction แรกอะ
เป็น transaction ที่ไม่มี local transaction
ส่วน 3 อันสุดท้าย ผมต้อง select ข้อมูลมาทำไรบางอย่าง
ก่อนจะ update ใน ฐานข้อมูลอะครับ
รบกวนผู้รู้ ช่วยไขปริศนาผมทีครับTag : - - - -
Date :
2009-02-18 08:56:49
By :
mixarstudio
View :
5421
Reply :
1
ผมไม่เข้าใจครับว่าทำไมจะต้องเปิด Transaction ขึ้นมาหลายตัวด้วยครับ ปกติแล้วการใช้ Conn.BeginTransaction(); จะใช้ครั้งเดียวครับ จากนั้นจะทำการ Insert/Update/Delete ข้อมูลกี่ Query ก็ได้ครับ
โดยที่สุดท้ายจะมีการ Commit หรือ RollBack ข้อมูลก็ขึ้นอยู่กับเงื่อนไขที่จะกระทำลงไปครับ ดูจากตัวอย่างนี้นนี้น่ะครับ
ASP.NET - Transaction()
Date :
2009-02-21 17:09:57
By :
webmaster
Load balance : Server 01