Question

My code is working fine if the statement (numtickets > tickav) is true (if tickets available is greater than tickets ordered) But if other wise, it throws in this error "FormatException was unhandled by user code, Input string was not in a correct format" on int numTick = Convert.ToInt32(txtNumberOfTickets.Text); I do know that somehow I can use tryparse, i need help putting it in the code. Any help would be appreciated, thank you

 namespace TicketsApp
 {
 public partial class TicketOrder : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["description"] != null && Session["EventID"] != null &&                             Session["numtickets"] != null && Session["ticketcost"] != null
            && Session["State"] != null && Session["Section"] != null && Session["Row"] != null && Session["date"] != null)
        {
            if (!IsPostBack)
            {
                try
                {
                    txtEventDescription.Text = Session["description"].ToString();
                    txtEventID.Text = Session["EventID"].ToString();
                    txtTicketsAvailable.Text = Session["numtickets"].ToString();
                    txtTicketCost.Text = Session["ticketcost"].ToString();
                    txtState.Text = Session["State"].ToString();
                    txtSectionNumber.Text = Session["Section"].ToString();
                    txtRowNumber.Text = Session["Row"].ToString();

                    txtNumberOfTickets.Focus();
                    lblOutput.Visible = false;

                }
                catch
                {
                    lblError.Text = "Please Search for Tickets First!";
                    lblError.Visible = true;
                    btnOrderTickets.Visible = false;
                    Response.Redirect("TicketSearch.aspx");
                    return;
                }
            }
        }
    }


    protected void btnOrderTickets_Click(object sender, EventArgs e)
    {

        TicketsDataAccessDataContext NewOrder = new TicketsDataAccessDataContext();

        int numTick = Convert.ToInt32(txtNumberOfTickets.Text);
        string s = txtTotalCost.Text.Substring(1);
        int totc = Convert.ToInt32(s);
        int id = Convert.ToInt32(txtEventID.Text);
        DateTime dt = Convert.ToDateTime(Session["date"]);

        int returnedValue = NewOrder.PlaceOrderFull(id, txtEventDescription.Text, dt, Session["State"].ToString(), Session["section"].ToString(), Session["Row"].ToString(), numTick, totc, "vfateev");
        if (returnedValue != 0)
        {
            lblOutput.Text = "Error has occured. Please try again";
            lblOutput.Visible = true;
            btnOrderTickets.Visible = false;
        }
        else
        {
            lblOutput.Visible = true;
            lblOutput.Text = "Thank you";
            btnOrderTickets.Visible = false;
        }

    }

    protected void txtNumberOfTickets_TextChanged1(object sender, EventArgs e)
    {
        int cos = Convert.ToInt32(txtTicketCost.Text);
        int numtickets = Convert.ToInt32(txtNumberOfTickets.Text);
        int tickav = Convert.ToInt32(txtTicketsAvailable.Text);

        if (numtickets > tickav)
        {
            lblError.Text = "Please Enter a valid ticket quantity";
            lblError.Visible = true;

            lblOutput.Text = "";
            txtNumberOfTickets.Text = "";
        }


        else
        {

            int cost = cos * numtickets + 5;
            txtTotalCost.Text = "$" + cost.ToString();
            lblOutput.Visible = false;
            lblFee.Text = "There is a $5 shipping fee";
            lblFee.Visible = true;
            lblError.Text = "";}
    }
}

}

Was it helpful?

Solution 2

Here is one of your methods rewritten using Int32.TryParse. I assumed you're doing txtTotalCost.Substring(1) to trim off the currency symbol. There are probably safe ways to do this, I'm just going to trim "$" for this example.

protected void btnOrderTickets_Click(object sender, EventArgs e)
{
    int numberOfTickets, ticketCost, eventId;
    if(Int32.TryParse(txtNumberOfTickets.Text, out numberOfTickets) &&
        Int32.TryParse(txtTotalCost.Text.TrimStart('$'), out ticketCost) &&
        Int32.TryParse(txtEventID.Text, out eventId))
    {
        DateTime dt = Convert.ToDateTime(Session["date"]);

        TicketsDataAccessDataContext NewOrder = new TicketsDataAccessDataContext();
        int returnedValue = NewOrder.PlaceOrderFull(eventId, txtEventDescription.Text, dt, Session["State"].ToString(), Session["section"].ToString(), Session["Row"].ToString(), numberOfTickets, ticketCost, "vfateev");
        if (returnedValue != 0)
        {
            lblOutput.Text = "Error has occured. Please try again";
            lblOutput.Visible = true;
            btnOrderTickets.Visible = false;
        }
        else
        {
            lblOutput.Visible = true;
            lblOutput.Text = "Thank you";
            btnOrderTickets.Visible = false;
        }
    }
    else
    {
        lblOutput.Visible = true;
        lblOutput.Text = "Some validation error message here...";
    }
}

You will need to make similar modifications to txtNumberOfTickets_TextChanged1 to ensure the user has entered valid text.

OTHER TIPS

You can use int.TryParse which returns a boolean and does not throw an exception.

int numTick = 0;
bool result = int.TryParse(txtNumberOfTickets.Text, out numTick );

You can also do some client side validation to ensure that the field is filled in and contains a number.

simply use something like To use IsNumeric in C#, add a reference to Microsoft.VisualBasic.dll then

if (Information.IsNumeric(value))
   {
    DoSomthing();
   }
   else
   {
    DoSomethingElse();
   }

UPDATE OPEN VISUAL STUDIO ==> YOUR PROJECT

Click on solution and add reference, choose Microsoft.VisualBasic.dll confrim the new reference will be add to your references into the project.

Go at the top of your page and declare the import statemnet for Microsoft.VisualBasic.dll alias

using Microsoft.VisualBasic.dll;

then where you need to check the value of your textbox

 if (Information.IsNumeric(yourtextbox.text.trim()))
   {
    //case true alias your value is numeric 
        //do what you need here like assing value to a var or any
        //else
   }
   else
   {
         //put your logic here in case result is false and value 
         //is not numeric
   }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top