Reputation: 73
When I click submit button without typing a number in my text box
,
I get error message
FormatException was unhandled by user code, Input string was not in a correct format.
Here is my code:
protected void btnSubmit_Click(object sender, EventArgs e)
{
int section = Convert.ToInt32(lstSectionNumber.SelectedValue);
double ticketQuantity = Convert.ToInt32(txtNumberOfTickets.Text);
}
I understand that I can validate the text box so I have to type in the number, but can anyone guide me to the correct way of fixing this?
Thank you, I've only been using C# for a week.
Upvotes: 2
Views: 5957
Reputation: 1453
Try using int.TryParse method.
protected void btnSubmit_Click(object sender, EventArgs e)
{
int section = 0; //or your default value
double ticketQuantity = 0d; //Or your default value
int.TryParse(lstSectionNumber.SelectedValue, out section);
double.TryParse(txtNumberOfTickets.Text, out ticketQuantity);
}
Upvotes: 3
Reputation: 9460
<tr>
<td style="width:30%; text-align:right;">
<asp:Label ID="lbltext" runat="server">Text Box</asp:Label>
</td>
<td style="width:30%; text-align:left;">
<asp:TextBox ID="txtbox1" runat="server"
ValidationGroup="Sample"
Width="100%" >
</asp:TextBox>
</td>
<td>
<asp:RegularExpressionValidator ID="RegularExpressionValidator1"
runat="server"
ControlToValidate="txtbox1"
Display="Dynamic"
ErrorMessage="Only Numbers"
ForeColor="Red"
ValidationExpression="^[0-9]*$">
</asp:RegularExpressionValidator>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1"
runat="server"
ControlToValidate="txtbox1"
Display="Dynamic"
ErrorMessage="Field cannot be left blank"
ForeColor="Red">
</asp:RequiredFieldValidator>
</td>
</tr>
Add RegularExpression to validate the textbox gets only numbers in the textbox and also add required field to the textbox so that it does not allows empty string to be passed.
By this way you achieve this
if you dont want to use RegularExpression and RequiredField Validators you can change your code like below
protected void btnSubmit_Click(object sender, EventArgs e)
{
int section;
int.TryParse(ddl1.SelectedValue, out section);
double ticketQuantity;
double.TryParse(txtbox1.Text, out ticketQuantity);
}
By this you will be getting Section
value as 0
and ticketQuantity
as 0.0
Upvotes: 0
Reputation: 8923
int section ;
int.TryParse(lstSectionNumber.SelectedValue,out section);
double ticketQuantity;
double.TryParse(txtNumberOfTickets.Text,out ticketQuantity);
In case of error you will get 0 or 0.0 without throwing error.
Upvotes: 0
Reputation: 5900
Try using the return boolean in TryParse
like so:
protected void btnSubmit_Click(object sender, EventArgs e)
{
int section = Convert.ToInt32(lstSectionNumber.SelectedValue);
double ticketQuantity;
if(!int.TryParse(txtNumberOfTickets.Text, out section))
{
//Some error handling here...like MessageBox.Show("Please enter a valid ticket quanity");
}
//Do you regular logic here...
}
On a side note: why is your ticketQuantity
a double but you're converting to an int? Also note that, depending on what 1stSectionNumber
is using as a source, you could have a similar conversion error there. When handling user input, it's usually a good idea to wrap in a Try-Catch so that your app doesn't crash. Even MessageBox with the error is usually better than a crash to desktop.
Upvotes: 0
Reputation: 3292
If you want to throw an exception for this try this one:
protected void btnSubmit_Click(object sender, EventArgs e)
{
try
{
int section = Convert.ToInt32(lstSectionNumber.SelectedValue);
double ticketQuantity = Convert.ToInt32(txtNumberOfTickets.Text);
}
catch (FormatException ex)
{
// put an error message.
}
}
If the user enters a non-numeric value, it will throw an exception instead.
Upvotes: 0
Reputation: 31
private void btnSubmit_Click(object sender, EventArgs e)
{
int section;
int.TryParse(lstSectionNumber.SelectedItem.ToString(),out section);
MessageBox.Show(section.ToString());
}
Upvotes: 0