List Info

Thread: Loop through dataset




Loop through dataset
country flaguser name
United States
2007-09-22 22:09:40

Hi,

I have a dataset I am trying to loop through. For each row(record)
in the dataset, I need to perform an action - in this case, insert a
new record.

The problem is this - my code is successful in looping through the
number of rows in the dataset, Unfortunatley it is inserting the
same record for each pass.

My code is below:

Dim irow As Data.DataRow
For Each irow In dsBustDataSet.Tables(0).Rows
Dim myConnection4 As New
Data.SqlClient.SqlConnection(srcPage2.ConnectionString)
Dim cmdAddToCart As New
Data.SqlClient.SqlCommand()
cmdAddToCart.CommandType =
Data.CommandType.Text
cmdAddToCart.Connection = myConnection4
cmdAddToCart.CommandText = "Insert into
tblShoppingCart (intCustomerID, decPrice, intQuantity, intProductID,
strRangeName, strSize, intThemeID, intColourID, intCandyStripeID,
intPlainColourID, strChildName, strBirthDate, strFontName, intOpen)
Values (CustomerID, Price, Quantity, ProductID, RangeName,
Size, ThemeID, ColourID, CandyStripeID, PlainColourID,
ChildName, BirthDate, FontName, 1)"
cmdAddToCart.Parameters.AddWithValue
("CustomerID", Session("LoggedCustomerID").replace(".", ""))
cmdAddToCart.Parameters.AddWithValue
("Price";, dsBustDataSet.Tables("FindCartItemsQuery").Rows(0).Item
("decPrice"))
cmdAddToCart.Parameters.AddWithValue
("Quantity", dsBustDataSet.Tables("FindCartItemsQuery").Rows(0).Item
("intQuantity")) '***column does not belong to the query - yes it
does!!!
cmdAddToCart.Parameters.AddWithValue
("ProductID", dsBustDataSet.Tables("FindCartItemsQuery").Rows
(0).Item("intProductID"))
cmdAddToCart.Parameters.AddWithValue
("RangeName", dsBustDataSet.Tables("FindCartItemsQuery").Rows
(0).Item("strRangeName"))
cmdAddToCart.Parameters.AddWithValue
("Size", dsBustDataSet.Tables("FindCartItemsQuery").Rows(0).Item
("strSize"))
cmdAddToCart.Parameters.AddWithValue
("ThemeID", dsBustDataSet.Tables("FindCartItemsQuery").Rows(0).Item
("intThemeID"))
cmdAddToCart.Parameters.AddWithValue
("ColourID", dsBustDataSet.Tables("FindCartItemsQuery").Rows(0).Item
("intColourID"))
cmdAddToCart.Parameters.AddWithValue
("CandyStripeID", dsBustDataSet.Tables("FindCartItemsQuery").Rows
(0).Item("intCandyStripeID"))
cmdAddToCart.Parameters.AddWithValue
("PlainColourID", dsBustDataSet.Tables("FindCartItemsQuery").Rows
(0).Item("intPlainColourID"))
cmdAddToCart.Parameters.AddWithValue
("ChildName", dsBustDataSet.Tables("FindCartItemsQuery").Rows
(0).Item("strChildName"))
cmdAddToCart.Parameters.AddWithValue
("BirthDate", dsBustDataSet.Tables("FindCartItemsQuery").Rows
(0).Item("strBirthDate"))
cmdAddToCart.Parameters.AddWithValue
("FontName", dsBustDataSet.Tables("FindCartItemsQuery").Rows(0).Item
("strFontName"))
myConnection4.Open()
cmdAddToCart.ExecuteNonQuery()
cmdAddToCart = Nothing
myConnection4.Close()
myConnection4.Dispose()
Next

Any help would be severely appreciated lol.

Rach

__._,_.___
.

__,_._,___
RE: Loop through dataset
country flaguser name
United Kingdom
2007-09-23 10:20:54


When you loop through the table rows like this

For Each irow In dsBustDataSet.Tables(0).Rows
...
Next

you are operating with a DataRow (called irow, in this case). So you need to
reference this DataRow in your loop. In place of
dsBustDataSet.Tables(";FindCartItemsQuery").Rows(0) you will be using irow

so the line:

cmdAddToCart.Parameters.AddWithValue("Price";,
dsBustDataSet.Tables("FindCartItemsQuery").Rows(0).Item("decPrice"))

is replaced by:

cmdAddToCart.Parameters.AddWithValue(";Price";, irow.Item("decPrice"))

etc.

Also, note that you should keep the connection open all the time you are
adding rows, so the loop goes in between the connection open and close
statements.

PS: I assume that dsBustDataSet.Tables(0) is the same table as
dsBustDataSet.Tables(";FindCartItemsQuery")

Mark Pawelek

________________________________________
From: AspNetAnyQuestionIsOk%40yahoogroups.com">AspNetAnyQuestionIsOkyahoogroups.com
[mailto: AspNetAnyQuestionIsOk%40yahoogroups.com">AspNetAnyQuestionIsOkyahoogroups.com] On Behalf Of Rachael
Sent: 23 September 2007 04:10
To: AspNetAnyQuestionIsOk%40yahoogroups.com">AspNetAnyQuestionIsOkyahoogroups.com
Subject: [AspNetAnyQuestionIsOk] Loop through dataset

Hi,

I have a dataset I am trying to loop through. For each row(record)
in the dataset, I need to perform an action - in this case, insert a
new record.

The problem is this - my code is successful in looping through the
number of rows in the dataset, Unfortunatley it is inserting the
same record for each pass.

My code is below:

Dim irow As Data.DataRow
For Each irow In dsBustDataSet.Tables(0).Rows
Dim myConnection4 As New
Data.SqlClient.SqlConnection(srcPage2.ConnectionString)
Dim cmdAddToCart As New
Data.SqlClient.SqlCommand()
cmdAddToCart.CommandType =
Data.CommandType.Text
cmdAddToCart.Connection = myConnection4
cmdAddToCart.CommandText = "Insert into
tblShoppingCart (intCustomerID, decPrice, intQuantity, intProductID,
strRangeName, strSize, intThemeID, intColourID, intCandyStripeID,
intPlainColourID, strChildName, strBirthDate, strFontName, intOpen)
Values (CustomerID, Price, Quantity, ProductID, RangeName,
Size, ThemeID, ColourID, CandyStripeID, PlainColourID,
ChildName, BirthDate, FontName, 1)"
cmdAddToCart.Parameters.AddWithValue
("CustomerID", Session("LoggedCustomerID").replace(".", ""))
cmdAddToCart.Parameters.AddWithValue
("Price";, dsBustDataSet.Tables("FindCartItemsQuery").Rows(0).Item
("decPrice"))
cmdAddToCart.Parameters.AddWithValue
("Quantity", dsBustDataSet.Tables("FindCartItemsQuery").Rows(0).Item
("intQuantity")) '***column does not belong to the query - yes it
does!!!
cmdAddToCart.Parameters.AddWithValue
("ProductID", dsBustDataSet.Tables("FindCartItemsQuery").Rows
(0).Item("intProductID"))
cmdAddToCart.Parameters.AddWithValue
("RangeName", dsBustDataSet.Tables("FindCartItemsQuery").Rows
(0).Item("strRangeName"))
cmdAddToCart.Parameters.AddWithValue
("Size", dsBustDataSet.Tables("FindCartItemsQuery").Rows(0).Item
("strSize"))
cmdAddToCart.Parameters.AddWithValue
("ThemeID", dsBustDataSet.Tables("FindCartItemsQuery").Rows(0).Item
("intThemeID"))
cmdAddToCart.Parameters.AddWithValue
("ColourID", dsBustDataSet.Tables("FindCartItemsQuery").Rows(0).Item
("intColourID"))
cmdAddToCart.Parameters.AddWithValue
("CandyStripeID", dsBustDataSet.Tables("FindCartItemsQuery").Rows
(0).Item("intCandyStripeID"))
cmdAddToCart.Parameters.AddWithValue
("PlainColourID", dsBustDataSet.Tables("FindCartItemsQuery").Rows
(0).Item("intPlainColourID"))
cmdAddToCart.Parameters.AddWithValue
("ChildName", dsBustDataSet.Tables("FindCartItemsQuery").Rows
(0).Item("strChildName"))
cmdAddToCart.Parameters.AddWithValue
("BirthDate", dsBustDataSet.Tables("FindCartItemsQuery").Rows
(0).Item("strBirthDate"))
cmdAddToCart.Parameters.AddWithValue
("FontName", dsBustDataSet.Tables("FindCartItemsQuery").Rows(0).Item
("strFontName"))
myConnection4.Open()
cmdAddToCart.ExecuteNonQuery()
cmdAddToCart = Nothing
myConnection4.Close()
myConnection4.Dispose()
Next

Any help would be severely appreciated lol.

Rach

__._,_.___
.

__,_._,___
[1-2]

about | contact  Other archives ( Real Estate discussion Medical topics )