-2

i have a shopping cart website and i have a track your order function wherein the admin can type in a "number" and transaction details will be viewed. however, i am trying to break my program and tried to input letters(abc) inside the transaction text box. it showed me an error that the input string was not in the correct format. should i use char? string? any references and answers are highly appreciated. :)

here is my code on the button in my transaction text box.

protected void btnGo_Click(object sender, EventArgs e)
    {
        if (txtTransactionNo.Text != string.Empty)
        {
            rblOrderDetails.Visible = true;
            ShowOrderDetails(rblOrderDetails.SelectedValue, Convert.ToInt32(txtTransactionNo.Text));
        }
        else
        {
            rblOrderDetails.Visible = false;
            Panel1.Visible = false;
            Panel2.Visible = false;
            Panel3.Visible = false;
        }
    }

and here where i declared the transactionNO text box

public string TransactionNoText
    {
        get { return txtTransactionNo.Text; }
        set { txtTransactionNo.Text = value; }
    }
Paolo Duhaylungsod
  • 487
  • 1
  • 7
  • 25
  • See this link - http://stackoverflow.com/questions/463299/how-do-i-make-a-textbox-that-only-accepts-numbers – TDG Jul 06 '15 at 07:06
  • You cannot convert a `string` to an `integer` unless it is in the proper format. A better approach would be to add validation to your textbox control before sending it to the server. – jmc Jul 06 '15 at 07:07
  • @TDG thanks for the reference. i understand how it works but seems like its not available in the toolbox? (numericupdown) im using VS 2010. – Paolo Duhaylungsod Jul 06 '15 at 07:35
  • @jmc thanks for the response. i have checked all of the validations and i think no one fits this issue. i am avoiding my program to crash when i input letters to the txtbox. any references? thanks again :) – Paolo Duhaylungsod Jul 06 '15 at 07:36
  • Why don't you use ajax Masked textbox controls like this: http://www.ajaxcontroltoolkit.com/MaskedEdit/MaskedEdit.aspx – NeverHopeless Jul 06 '15 at 07:39
  • thanks @NeverHopeless. i only need like 1 to 2 digit number. i think ill go with the numericupdown but its not showing in the toolbox. any idea? – Paolo Duhaylungsod Jul 06 '15 at 07:40
  • Perhaps this: http://www.asp.net/web-forms/videos/ajax-control-toolkit/how-do-i-use-the-numericupdown-extender-control – NeverHopeless Jul 06 '15 at 07:46

1 Answers1

0

instead of directly converting to a number using Convert.ToInt32(), use int.TryParse()`.

int result=0;

if(int.TryParse(txtTransactionNo.Text,out result))

{

ShowOrderDetails(rblOrderDetails.SelectedValue,result);

}

Joseph
  • 1,054
  • 1
  • 11
  • 25