views:

43

answers:

4

I have a DataList Control as follows

<asp:DataList ID="DataList1" runat="server" DataKeyField="FruitID"  RepeatColumns="2" Width="387px">
<ItemTemplate>
    <asp:Image ID="Image1" runat="server" Height="104px" ImageUrl='<%# Eval("ImageUrl") %>' Width="135px" />
    <br />
    Item ID:
    <asp:Label ID="lblItemID" runat="server" Text='<%# Eval("FruitID") %>' />
    <br />
    FruitName:
    <asp:Label ID="lblFruitNameLabel" runat="server" Text='<%# Eval("FruitName") %>' />
    <br />
    UnitPrice:
    <asp:Label ID="lblUnitPriceLabel" runat="server" Text='<%# Eval("UnitPrice") %>' />
    <br />
    Quantity:
    <asp:TextBox ID="txtQuantity" runat="server"></asp:TextBox>
    <br />
    <br />
</ItemTemplate>
</asp:DataList>
<asp:Button ID="btnAddtoCart" runat="server" onclick="Button1_Click"  Text="Add to Cart" />

and in the code behind im using the following code to get the values of the controls inside the DataList control

int id = int.Parse(((Label)DataList1.Controls[0].FindControl("lblItemID")).Text.ToString());
string Name = ((Label)DataList1.Controls[0].FindControl("lblFruitNameLabel")).Text;
double Price = double.Parse(((Label)DataList1.Controls[0].FindControl("lblUnitPriceLabel")).Text.ToString());
int Quantity = int.Parse(((TextBox)DataList1.Controls[0].FindControl("txtQuantity")).Text.ToString());
string Url = ((Image)DataList1.Controls[0].FindControl("Image1")).ImageUrl;

I'm getting the following exception

Input string was not in a correct format.

Exception occurs in the following line

int Quantity = int.Parse(((TextBox)DataList1.Controls[0].FindControl("txtQuantity")).Text.ToString());

I'm very much sure that i'm entering a Integer value to the textbox :)

am I missing something?

A: 

Maybe that would be great when you little bit rewrite your code to this

var myQuantity = ((TextBox)DataList1.Controls[0].FindControl("txtQuantity")).Text.ToString();
int Quantity = int.Parse(myQuantity);

and check with Debugger what type is myQuantity. This should help you to locate your issue.

markoniuss
myQuantity is getting a null value
chamara
Seemingly there is different problem, myQuantity should not be null. Try browse whole variable, e.g. type only var myQuantity = ((TextBox)DataList1.Controls; with debugger, you should be able to find out all entities and browse with them.
markoniuss
A: 

It could be because of the type casting problem, from the number you entered, to integer. Try making it a long data type instead and also make sure that no characters exists in between the string you entered.

Siva Gopal
A: 

hiiiii

try this

int Quantity =Convert.ToInt32(((TextBox)DataList1.Controls[0].FindControl("txtQuantity")).Text.ToString());
Nithesh
:( its not working
chamara
A: 
        <br />
        Quantity :
        <asp:TextBox ID="txtQuantity" runat="server"></asp:TextBox>
        <br />

i saw that u have not make any bind to txtQuantity. And Convert.ToInt32 gives exception because it s null string. i think so

AEMLoviji