Click here to Skip to main content
15,888,590 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Quote:
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.FormatException: Input string was not in a correct format.


What I have tried:

Line 42:         {
Line 43:             var test = Convert.ToString(Session["CID"]);
Line 44:             int q = QAdapter.Insert(Convert.ToInt32("txtserial.Text"), (Convert.ToInt32("0")),
Line 45:                 drpQpapername.SelectedItem.Text, txtEname.Text, txtquestion.Text, 
Line 46:                 txta.Text, txtb.Text, txtc.Text, txtd.Text, DropDownListkey.SelectedItem.Text);
Posted
Updated 25-Jul-17 23:28pm

Simple: The user input was not a valid integer.
Solution: don't use Convert at all. Always use the appropriate TryParse method, and report user problems back to the user instead of assuming they will always type correctly.
VB
Dim serial As Integer
If Not Integer.TryParse(txtserial.Text, serial) Then
	' Report problem to user
	...
	Return
End If
 
Share this answer
 
Comments
sommr0 26-Jul-17 5:46am    
i'm try parse method but issue still same
OriginalGriff 26-Jul-17 6:10am    
Did you try the code I gave you exactly as I showed it?
sommr0 26-Jul-17 6:10am    
yeah
sommr0 26-Jul-17 6:13am    
i'm trying
protected void btnaddquestion_Click(object sender, EventArgs e)
{
var test = Convert.ToString(Session["CID"]);
int q = QAdapter.Insert(int.Parse("txtserial.Text"), (Convert.ToInt32("0")),
drpQpapername.SelectedItem.Text, txtEname.Text, txtquestion.Text,
txta.Text, txtb.Text, txtc.Text, txtd.Text, DropDownListkey.SelectedItem.Text);
QDT = QAdapter.SELECT_QUESTION();
GridViewADDQuestion.DataSource = QDT;
GridViewADDQuestion.DataBind();
}
OriginalGriff 26-Jul-17 6:19am    
Is that exactly what I showed you?
No - it uses int.Parse which *still throws an exception if the value is bad*
That's *why* you use TryParse - it reports errors via a boolean return value, letting you handle it properly instead of assuming the user input is fine.
Convert.ToInt32("txtserial.Text")


You're converting the literal text of "txtserial.Text" to a number, but you probably mean to use the Text property of the txtserial object so you don't use the quotes (quotes denote a string literal)

Convert.ToInt32(txtserial.Text)


That will still error if whatever is in txtserial can't be converted to a number so google how to use int.TryParse instead.
 
Share this answer
 
Comments
sommr0 26-Jul-17 5:46am    
How to fixed it?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900