0

Essentially trying to capture information when a checkbox is checked off, if it is then capture the quantity inputted. Attached is the code.

  <asp:TemplateField HeaderText="Quantity">
        <ItemTemplate>
            <asp:TextBox ID="TextboxQuantity" runat="server"></asp:TextBox>
        </ItemTemplate>
  </asp:TemplateField>
</Columns>

Here is my aspx.cs code.

 //check to see if a check box is checked
for (int row = 0; row < gv_Input.Rows.Count; row++)
{

    CheckBox Cbox = (CheckBox)gv_Input.Rows[row].FindControl("CheckboxSelect");
    TextBox Tbox = (TextBox)gv_Input.Rows[row].FindControl("TextboxQuantity");
    int quantity = Convert.ToInt32(Tbox.Text);
    if (Cbox.Checked)
    {
        if (Tbox == null)
        {
            Response.Write("<script>alert('Fill in textbox')</script>");
        }
        else
        {
            Response.Write(
              "<script>alert('Something was inputted into the textbox')</script>");
        }
    }
}

The line that gives the error is this line

int quantity = Convert.ToInt32(Tbox.Text);

Error: Input string was not in the correct format

  • Please [edit] question so it shows exactly what line fails and also include data for that line. Ideally shorten code to just that line with hardcoded data (like `int quantity = Convert.ToInt32("salmon");`). – Alexei Levenkov Feb 24 '21 at 00:21
  • Just edited the question my apologies @AlexeiLevenkov – Steven Polka Feb 24 '21 at 00:25

2 Answers2

0

Even if the text box is left blank, the test if (Tbox == null) is never going to be true because you are checking the reference to the text box, not its content. I believe that your test should be:

if(Tbox == null || string.IsNullOrWhitespace(Tbox.Text) == true) {
Dave Holden
  • 157
  • 5
  • Thank you for this! The code won't even get that far because there is an error to convert the textbox to an int. I appreciate your input though! – Steven Polka Feb 24 '21 at 00:31
  • Glad it helped. Please remember to give credit where it is due and accept or at least upvote the answer that helped you fix your problem. In your solution all you did was avoid the exception by removing the integer conversion. Instead you should use Int32.Tryparse() to perform the type conversion instead if Convert.ToInt32, which throws an exception if the user entered something that is not a number. – Dave Holden Feb 24 '21 at 01:28
  • 1
    I am trying to but my reputation is too low – Steven Polka Feb 24 '21 at 01:55
0

Through further testing. I attempted using a foreach loop and it seemed to work. Thank you for you help here is my solution

foreach (GridViewRow row in gv_Input.Rows)
            {

                CheckBox Cbox = (CheckBox)row.FindControl("CheckboxSelect");
                TextBox Tbox = (TextBox)row.FindControl("TextboxQuantity");
                if (Cbox.Checked)
                {
                    if (Tbox.Text == null || string.IsNullOrEmpty(Tbox.Text) == true)
                    {
                        Response.Write("<script>alert('Fill in textbox')</script>");
                    }
                    else {
                        Response.Write("<script>alert('Successful find')</script>");
                    }