简体   繁体   中英

Input string was not in a correct format. (Editable Gridview)

Whenever I try to edit information inside my gridview it throws me this error....

here is my code for row editing

protected void GRDTech_RowEditing(object sender, GridViewEditEventArgs e)
{  
 GRDTech.EditRowStyle.BackColor = System.Drawing.Color.LightYellow;
 GRDTech.EditIndex = e.NewEditIndex;
 BindData();
}

and here is my code:

 bool comparedResults = true;
TextBox txtAddFROMKMREAD = (TextBox)Default._def.GRDTech
    .FooterRow.FindControl("txtAddFROMKMREAD");
DropDownList drpPlateNum = (DropDownList)Default._def.GRDTech
    .FooterRow.FindControl("drpPlateNum");
string txt = txtAddFROMKMREAD.Text;
int readDepartureKM = Convert.ToInt32(txtAddFROMKMREAD.Text);

TextBox txtAddTOKMREADING = (TextBox)Default._def
    .GRDTech.FooterRow.FindControl("txtAddTOKMREADING");

int arrivalKMreading = Convert.ToInt32(txtAddTOKMREADING.Text);

comparedResults = (arrivalKMreading < readDepartureKM);

the part int readDepartureKM keeps on giving me an error that the input string is incorrect. how do I fix this?

It seems that the value you have in txtAddTOKMREADING.Text is not a vlid integer (might be blank?).

When you are not sure the text is valid (for example, when you use a textbox for an input from the user), It is better to use int.TryParse :

From the documentation of int.TryParse :

Converts the string representation of a number to its 32-bit signed integer equivalent. A return value indicates whether the conversion succeeded.

int readDepartureKM ;
bool parsedSuccessfully = int.TryParse(txtAddFROMKMREAD.Text, out readDepartureKM )
if(parsedSuccessfully)
{
    //Do something with the value that is in readDepartureKM 
}
else
{
   //do something else (perhaps an error message?)
   string message =String.Format("'{0}' Is not a valid integer.",txtAddFROMKMREAD.Text);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM