473,387 Members | 1,603 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

SQLCommand insert not working for dataset with more than one row.

I am trying to insert data into 2 tables, Order and Order_Item, in a transaction. Everything works fine if I only have 1 row in my objCartDT dataset. If I have only one row, the 2 tables are updated as expected. But if I have more than one row in the objCartDT, something fails, and I can't figure out what (but I suspect it has to do with the params in conjunction with "cmdNewOrder_Items.ExecuteNonQuery();" and the looping I'm trying to do. Maybe not. I will paste the function that is called on button click (to submit the order). I am using a MS Sql server database.

public void btnSubmitOrder_Click(object sender, EventArgs e)
{
SqlConnection objConn;
SqlTransaction objTran;
SqlCommand cmdNewOrder;
SqlCommand cmdNewOrder_Items;
String strNewOrder;
String strNewOrder_Item;

//Create new Order_ID.//
string varOrder_ID = Guid.NewGuid().ToString();

string varDateTime = Convert.ToString(DateTime.Now);

//Create SQL strings.//
strNewOrder = "INSERT into [Order] (Order_ID, Create_Date, Ordered_By, Order_Status, " +
"Order_Type, Dept_ID, Loc_ID) VALUES (@Order_ID, @Create_Date, @Ordered_By, @Order_Status, " +
"@Order_Type, @Dept_ID, @Loc_ID)";

strNewOrder_Item = "INSERT INTO [ORDER_ITEM] ([ORDER_ITEM_ID], [ORDER_ID], [QTY_ORDERED], [DRUG_ID], [NOTES], [COST]) " +
"VALUES (@ORDER_ITEM_ID, @ORDER_ID, @QTY_ORDERED, @DRUG_ID, @NOTES, @COST)";


string varOrder_Item_Id;
int varQty_Ordered;
string varDrug_Id;
string varNotes;
decimal varCost;


//Create connection string objConn.//
objConn = new SqlConnection(ConfigurationManager.ConnectionStrin gs["myConnStr"].ConnectionString);

cmdNewOrder = new SqlCommand(strNewOrder, objConn);
cmdNewOrder.Parameters.AddWithValue("@Order_ID", varOrder_ID); //varOrder_ID
cmdNewOrder.Parameters.AddWithValue("@Create_Date" , varDateTime);
cmdNewOrder.Parameters.AddWithValue("@Ordered_By", userID); //get userid
cmdNewOrder.Parameters.AddWithValue("@Order_Status ", "Unfulfilled");
cmdNewOrder.Parameters.AddWithValue("@Order_Type", "Order");
cmdNewOrder.Parameters.AddWithValue("@Dept_ID", DropDownList1.SelectedValue);
cmdNewOrder.Parameters.AddWithValue("@Loc_ID", DropDownList2.SelectedValue);

cmdNewOrder_Items = new SqlCommand(strNewOrder_Item, objConn);

//Open the objConn connection.//
objConn.Open();

//Begin the transaction using the connection object.//
objTran = objConn.BeginTransaction();

//Assign the transaction to the commands.//
cmdNewOrder.Transaction = objTran;
cmdNewOrder_Items.Transaction = objTran;

int counter = 0;

try
{
//Insert new order row into Order table.//
cmdNewOrder.ExecuteNonQuery();


//Insert rows from dataset objCartDT into the Order_Item table.//
foreach (DataRow objDR in objCartDT.Rows)
{
counter++;

varOrder_Item_Id = (string)objDR["Order_Item_Id"];
varQty_Ordered = (int)objDR["Qty_Ordered"];
varDrug_Id = (string)objDR["Drug_Id"];
varNotes = (string)objDR["Notes"];
varCost = (Decimal)objDR["Cost"];

cmdNewOrder_Items.Parameters.AddWithValue("@ORDER_ ITEM_ID", varOrder_Item_Id);
cmdNewOrder_Items.Parameters.AddWithValue("@ORDER_ ID", varOrder_ID);
cmdNewOrder_Items.Parameters.AddWithValue("@QTY_OR DERED", varQty_Ordered);
cmdNewOrder_Items.Parameters.AddWithValue("@DRUG_I D", varDrug_Id);
cmdNewOrder_Items.Parameters.AddWithValue("@NOTES" , varNotes);
cmdNewOrder_Items.Parameters.AddWithValue("@COST", varCost);


cmdNewOrder_Items.ExecuteNonQuery();

}

objTran.Commit();

SuccessfulOrder = "1";
}

catch
{
objTran.Rollback();

SuccessfulOrder = "2";
}

finally
{
//Close connection.//
objConn.Close();
//Clear out the session table objCartDT in the objDS DataSet.//
objCartDT.Rows.Clear();

gvCart.DataSource = objCartDT;
gvCart.DataBind();
}

Response.Redirect("neworder.aspx?Successful=" + SuccessfulOrder + "&counter=" + counter);

}
Oct 9 '06 #1
1 5815
I figured it out.

I added 1 line at the end of the loop to clear out all the SqlParameter objects from the SqlParameterCollection:

cmdNewOrder_Items.Parameters.Clear();

So, the loop would look like this:

foreach (DataRow objDR in objCartDT.Rows)
{
counter++;


varOrder_Item_Id = (string)objDR["Order_Item_Id"];
varQty_Ordered = (int)objDR["Qty_Ordered"];
varDrug_Id = (string)objDR["Drug_Id"];
varNotes = (string)objDR["Notes"];
varCost = (Decimal)objDR["Cost"];



cmdNewOrder_Items.Parameters.AddWithValue("@ORDER_ ITEM_ID", varOrder_Item_Id);
cmdNewOrder_Items.Parameters.AddWithValue("@ORDER_ ID", varOrder_ID);
cmdNewOrder_Items.Parameters.AddWithValue("@QTY_OR DERED", varQty_Ordered);
cmdNewOrder_Items.Parameters.AddWithValue("@DRUG_I D", varDrug_Id);
cmdNewOrder_Items.Parameters.AddWithValue("@NOTES" , varNotes);
cmdNewOrder_Items.Parameters.AddWithValue("@COST", varCost);


cmdNewOrder_Items.ExecuteNonQuery();

cmdNewOrder_Items.Parameters.Clear();

}
Oct 9 '06 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: authorking | last post by:
I use the following code to insert a data record in to a datatable of an access database.But every time I execute the command, there will rise an exception and the insert operation can't be...
4
by: Mike Hnatt | last post by:
My goal is to get data from an XML file into a couple of tables in an Access database. The XML file is a little complex so I need control over what I do (I can't just read it into a dataset). ...
0
by: Winshent | last post by:
I have some code which attempts to load the contents of a dataset into a sql server db. the code fills the dataadapter with the datset no prob, but fails on 'ExecuteNonQuery'. the function...
1
by: Tom | last post by:
Hello guys Please have a look on following paragraph from ".NET Data Access Architecture Guide". ''''''''''' Although you can repeatedly use the same SqlCommand object to execute the same...
4
by: Agnes | last post by:
I am confused about these two things 1st)As form load I new a data adapter programmicaly , and I can use stored procedure to 'select sth ' and then fill into the dataset.by using binding context,...
15
by: Khurram | last post by:
I have a problem while inserting time value in the datetime Field. I want to Insert only time value in this format (08:15:39) into the SQL Date time Field. I tried to many ways, I can extract...
9
by: fniles | last post by:
I am using VB.NET 2003 and SQL2000 database. I have a stored procedure called "INSERT_INTO_MYTABLE" that accepts 1 parameter (varchar(10)) and returns the identity column value from that table....
2
by: =?Utf-8?B?Y2F0?= | last post by:
We recently moved an older ASP.NET 1.1 code base on to ASP.NET 2.0, .NET 3.0 and Windows 2003 Server SP2. We started experiencing the following issue with the web cache (the code is straightforward...
6
by: Frank Hauptlorenz | last post by:
Hello, I'm trying to send an SqlCommand to a WCF-Service. For this I'm using the following DataContract: public class SqlCommandComposite { SqlCommand cmd = new SqlCommand();
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.