Click here to Skip to main content
15,881,882 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
hi am using windows form c# language.
while selecting from grid after clicking edit button all fields value should display in text related text box.but am getting error
"Input string was not in a correct format"

here is my code
C#
private void tbSoldAmt_TextChanged(object sender, EventArgs e)
       {
           if ((!string.IsNullOrEmpty(tbSoldAmt.Text)) && (!string.IsNullOrEmpty(tbPurchaseAmt.Text)))
           {
               tbProfit.Text = (Convert.ToInt32(tbSoldAmt.Text) - Convert.ToInt32(tbPurchaseAmt.Text)).ToString();//this line am getting error
           }
       }


and here is btnEdit code

C#
private void btnEdit_Click(object sender, EventArgs e)
       {
           int i;
           i = dataGridView1.SelectedCells[0].RowIndex;
           tbModeOfTransport.Text = dataGridView1.Rows[i].Cells[1].Value.ToString();
           tbCustomerName.Text = dataGridView1.Rows[i].Cells[2].Value.ToString();
           tbAddress.Text = dataGridView1.Rows[i].Cells[3].Value.ToString();

           tbFrom.Text = dataGridView1.Rows[i].Cells[4].Value.ToString();
           tbTo.Text = dataGridView1.Rows[i].Cells[5].Value.ToString();
           tbSeats.Text = dataGridView1.Rows[i].Cells[6].Value.ToString();
           tbFareAmount.Text = dataGridView1.Rows[i].Cells[7].Value.ToString();
           tbPurchaseAmt.Text = dataGridView1.Rows[i].Cells[8].Value.ToString();
           tbSoldAmt.Text = dataGridView1.Rows[i].Cells[9].Value.ToString();//in this line highlighted 
           tbProfit.Text = dataGridView1.Rows[i].Cells[10].Value.ToString();
           tbDate.Text = dataGridView1.Rows[i].Cells[11].Value.ToString();
           tbMobile.Text = dataGridView1.Rows[i].Cells[12].Value.ToString();




       }


can any one suggest me ?thank u
Posted
Updated 24-Jun-12 1:59am
v3
Comments
[no name] 24-Jun-12 7:21am    
And? The error is perfectly clear. One or both of those strings cannot be converted to an int.
ythisbug 24-Jun-12 7:25am    
ya so any solution.thanks

1 solution

As Wes says: "The error is perfectly clear. One or both of those strings cannot be converted to an int."

So you have to detect that, and report it back to the user, instead of continuing.
Have a look at the Int.TryParse[^] method - it returns true if the conversion was ok.
 
Share this answer
 
Comments
Shahin Khorshidnia 24-Jun-12 8:29am    
+5
Oshtri Deka 24-Jun-12 11:28am    
5.
sandeep nagabhairava 25-Jun-12 13:29pm    
my +5

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