Input string was not in a correct format

L

Looch

Hi All,

I'm using the following code:

private void cmdConsumUpdate_Click(object sender, EventArgs e)
{
try
{
DataGridViewRow dgvr = dgView2.CurrentRow;
string stat = dgvr.Cells[2].ToString ();
string trac = dgvr.Cells[6].ToString ();
Int32 ticknum =
Convert.ToInt32(dgvr.Cells[8].ToString());
amslpsvr1.WebService ws = new
CustomerService_BreaCS.amslpsvr1.WebService();
ws.UpdateConsumOrder(stat, trac, ticknum);
getInfo();
}
catch (Exception ex)
{
MessageBox.Show (ex.Message)
}
}

I keep getting the error "Input string was not in a correct format.".
I commented out the web service call and got the same error so I know
that's not the issue. I have a feeling this is above the code I have
here but I really don't have any idea what is going on!

Any help would be much appreciated.
 
E

Egghead

I believe that is the problem "Int32 ticknum =
Convert.ToInt32(dgvr.Cells[8].ToString())" Are you sure all the cells[8]
are in numbers?
 
N

Nicholas Paldino [.NET/C# MVP]

Looch,

It is most likely this call:

Int32 ticknum = Covert.ToInt32(dgvr.Cells[8].ToString());

What is the value in dgvr.Cells[8]? Also, why are you calling Convert?
If the type in the cell is an integer, then cast it out, don't convert it to
a string and then convert it back to an int.
 
L

Looch

Nick,

Thanks for the reply.

The cell is a string. If I try "Int32 ticknum =
Covert.ToInt32(dgvr.Cells[8]);" I get 'Cannot convert
system.datagridcell to type Int' (something to that effect) before it
will run in the debugger.
 
G

Guest

You probably are not getting the value of the cell. Try doing something like:

Int32 ticknum =
Covert.ToInt32(dgvr.Cells[8].Value);
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top