Member Avatar

I cannot work out how to get an integer value from a textbox from inside a datalist for a variable to insert into SQL.

Here is my code.

int qty = int.parse(dlProducts.Controls[0].FindControl("txtProductQty"));

and this variable will be put into an insert Query.

into a textbox inside a datalist id="dlProducts"

<asp:TextBox ID="txtProductQty" runat="server" Height="22px" 
                 Width="15px"></asp:TextBox>

Recommended Answers

Hi,

the code dlProducts.Controls[0].FindControl("txtProductQty") will return an TextBox and not an value.

Do it by steps, to see what is going wrong. Example:

TextBox txtQty = dlProducts.Controls[0].FindControl("txtProductQty");
int qty = txtQty.Value;

Debug the code and see if the textbox is found, if it is then …

Jump to Post

try txtQty.text

Jump to Post

That's fine. You can use a field validator in visual studio, or plain JavaScript to validate that the textbox will only accept integers.

This doesn't change the fact that you retrieve the value from the textbox as txtbox.text.

Jump to Post

First ensure that the textbox will only allow for integers. Assign the textbox value to a variable that is of type integer. Use that variable in your SQL routines.

Jump to Post

All 13 Replies

Hi,

the code dlProducts.Controls[0].FindControl("txtProductQty") will return an TextBox and not an value.

Do it by steps, to see what is going wrong. Example:

TextBox txtQty = dlProducts.Controls[0].FindControl("txtProductQty");
int qty = txtQty.Value;

Debug the code and see if the textbox is found, if it is then you'll get the value.

I don't know if the class name is exatcly TextBox, it may be HTMLTextBox or something like that.

Member Avatar

txtQty.value; does not appear to work for C# asp.net

try txtQty.text

Member Avatar

I want the input from the text to be an intiger (Quantity for a product). The column in SQL is an intiger so does the input variable needs to be.

That's fine. You can use a field validator in visual studio, or plain JavaScript to validate that the textbox will only accept integers.

This doesn't change the fact that you retrieve the value from the textbox as txtbox.text.

Member Avatar

I want to insert it into an SQL databse which requires an intiger value. A formatexception keeps coming up.

First ensure that the textbox will only allow for integers. Assign the textbox value to a variable that is of type integer. Use that variable in your SQL routines.

Member Avatar

this is in a datalist. I have set text mode to numbers. SQL daaata type = int. I cant see how to enter a value from the browser to SQL which is an integer. My insert Query does not have any single quotes around the variable name only " + qty + ". I have tried int.parse around (TextBox)dlProducts.Controls[0].FindControl("txtProductQty"); and it's variable. Im not sure how to solve this problem. It seems like an impossibility.

Adrian, you don't seem to be understanding what we are saying.

(TextBox)dlProducts.Controls[0].FindControl("txtProductQty") will return an TextBox, NOT A INT.

The int value will be in the Value/Text of the TextBox.
Do as I said before:

TextBox txtQty = (TextBox) dlProducts.Controls[0].FindControl("txtProductQty");
int qty = int.Parse( txtQty.Value );
//OR
int qty = int.Parse( txtQty.Text );
Member Avatar

No txtQty.Value or .Text . does not work. Keep getting FormatError. Not sure what it is.

I am getting System.Web.UI.TextBox does not contains a definition for 'Value' and no extension method 'Value' accepting
a first argument of type 'System.Web.UI.WebControls.TextBox' could be found (are you missing a using directive or an assembly reference.

Member Avatar

I think the probem is getting the value from the textbox in the datalist, rather than formatting the value.

I directed the value to a label outside the datalist and produced "System.Web.UI.WebControls.TextBox"

As shown at msdn the TextBox does have a Text property.

So, int.Parse( txtQty.Text ); should work.

Yes, the textBox does have a text property, not value.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.21 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.