Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Retreive items from a datalist...

Status
Not open for further replies.

kroken

Programmer
Dec 13, 2005
3
NO
Hi,

I have trouble getting the right items out of a datalist. The thing is that I have a shopping basket with several product lines, and I want to store each line separately in a database to create order history for each customer. I have a loop, which is looping through the datalist storing data in the database, but here it suddenly stopped. I want to send in an integer which represent the line number, and then retreive right items for this line.



CONFIRM.ASPX.CS

for (int i=0; i<ShoppingCartList.Items.Count; i++)

{

string strSQL1 = "INSERT INTO OrdProducts (OrderId, Qty, Product) VALUES (?, ?, ?)";


OleDbCommand myCmd1 = new OleDbCommand(strSQL1, myConn );


Market.OrderList shoppingCart = ((Market.OrderList) Session["ShoppingCart"]);

myCmd1.Parameters.Add("@OrderId", System.Data.OleDb.OleDbType.VarChar,100).Value = txtNewOrderNo.Text;


HtmlInputText qty = (HtmlInputText) ShoppingCartList.Items.FindControl("Qty");

myCmd1.Parameters.Add("@Qty", System.Data.OleDb.OleDbType.VarChar,100).Value = qty.Value;


string GetProductName = shoppingCart.ProductName(i);

myCmd1.Parameters.Add("@Product", System.Data.OleDb.OleDbType.VarChar,1000).Value = GetProductName;

myCmd1.ExecuteNonQuery();

}

CONFIRM.ASPX

<!-- The datalist -->

<ItemTemplate>
<tr class="h2">
<td width="35">
<input type=text size=1 id="Qty" runat=server value='<%# DataBinder.Eval(Container.DataItem, "Quantity") %>' NAME="Qty">
</td>
<td width="350">
<%# DataBinder.Eval(Container.DataItem, "Name") %>
</td></tr></ItemTemplate>



MARKET.CS

public string ProductName(int i)

{

string ProductName = "";

IEnumerator items = orders.Values.GetEnumerator();

while(items.MoveNext())

{

ProductName = ((OrderItem) items.Current).name;

}


return ProductName;

}



The problem seems to be in the market.cs file. I only found a way to read from the datalist through a IEnumerator, but the problem with this is, as you see, that it loops to the last product line and only returns the items from the last line! Con someone please give me some help with this one! Will be very greatful!!! Thanks in advance!!!








 
I'd suggest using foreach to step through the items:

Code:
foreach (DataListItem dlItem in ThisDataList.Items)
{
   TextBox thisTB = dlItem.FindControl("TextboxName") as TextBox;
   // do what you need with each item here
}

hth

tigerjade

"Always code as if the person who ends up maintaining your code will be a violent psychopath who knows where you live." -- Martin Golding


 
Thanks for your reply.

Seems to be a decent solution for my problem, but I still have problems getting the value of the items in the textbox. I'm using the code like this. I get the value from the HtmlInputText, but not the label... Guess I'm doing something wrong!?!

Code:
foreach (DataListItem dlItem in ShoppingCartList.Items)
{
HtmlInputText qty = dlItem.FindControl("Qty") as HtmlInputText;
myCmd1.Parameters.Add("@Qty", System.Data.OleDb.OleDbType.VarChar,100).Value = qty.Value;

Label ProducName = dlItem.FindControl("ProductName") as Label;
myCmd1.Parameters.Add("@Product", System.Data.OleDb.OleDbType.VarChar,1000).Value = ProducName.Text;	
					
myCmd1.ExecuteNonQuery();
}

Code:
<input type=text size=1 id="Qty" runat=server value='<%# DataBinder.Eval(Container.DataItem, "Quantity") %>' NAME="Qty">
<asp:Label Runat="server" ID="ProductName"><%# DataBinder.Eval(Container.DataItem, "Brand") %</asp:Label>
 
I've had the same problem with labels before. Try putting the label in like this:

Code:
<asp:Label Runat="server" ID="ProductName" Text='<%# DataBinder.Eval(Container.DataItem, "Brand")%>'></asp:Label>

Hopefully, that should allow you to retrieve the .text value. :)

hth

tigerjade

"Always code as if the person who ends up maintaining your code will be a violent psychopath who knows where you live." -- Martin Golding


 
Thanks, that really solved my problem!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top