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" 
4 Years
Discussion Span
Last Post by JorgeM


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.


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.


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.


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 );
int qty = int.Parse( txtQty.Text );

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.


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"

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.