Hi Everyone.
I wanted to get a value from a Combo and display it on the grid. I am getting this exception "Input string was not in a correct format" . The code is as follows:-
private void CboItemLookUp_EditValueChanged(object sender, EventArgs e) {
try {
currentItem = new StockEO();
currentItem.Load(int.Parse(CboItemLookUp.EditValue.ToString())); // The exception Is thrown here
id = currentItem.ID;
int quan = 1;
if (currentItem.StockAtHand() < quan) {
MessageBox.Show("Insufficient stock amount for item " + currentItem.Name.ToUpper(), "SMIS 2013", MessageBoxButtons.OK, MessageBoxIcon.Warning);
return;
}
Nullable vatAmount = (currentItem.SellPrice * currentItem.Vat) / 100;
Nullable vatableAmount = currentItem.SellPrice + vatAmount;
this.GridPos.Rows.Add(currentItem.ID, currentItem.ItemCode, currentItem.Name, currentItem.SellPrice, quan, currentItem.Vat, vatableAmount, vatAmount, 0, ((currentItem.SellPrice + vatAmount) * quan));
GridPos.CurrentRow.Cells["colQuantity"].Selected = true;
//SendKeys.Send("{ESC}");
GridPos.Focus();
int k = GridPos.Rows.Count;
GridPos.CurrentCell = GridPos.Rows[k - 1].Cells["colQuantity"];
GetTransactionTotals();
} catch (Exception ex) {
MessageBox.Show(ex.GetBaseException().Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
}
I managed to get the desired Display on the grid. but the exception is thrown before I get the results. Thanks
Gowtham RajamanickamPosted Apr 6, 2015, 4:27 AM
Peter DzuyaPosted Apr 6, 2015, 4:00 AM
Peter DzuyaPosted Apr 6, 2015, 4:00 AM
Mukesh NayakPosted Apr 5, 2015, 3:16 PM
Can you try following and see what the output is
Tom MohanPosted Apr 5, 2015, 7:47 AM
In the above code, you are not handling null/empty string properly. use this line of code
currentItem.Load(string.IsNullOrEmpty(CboItemLookUp.EditValue.ToString()) ? 0 : int.Parse(CboItemLookUp.EditValue.ToString()));
Peter DzuyaPosted Apr 5, 2015, 7:04 AM
I added the trim(). Still having the same exception. Thanks
Mukesh NayakPosted Apr 5, 2015, 6:52 AM
From the exception it seems the place where you are using int.parse(CboItemLookUp.EditValue.ToString()) the value
CboItemLookUp.EditValue.ToString() is not in an integer format. For example if you try something like
try
{
string objFirst = "abcd";
var value = int.Parse(objFirst);
}
catch (Exception ex)
{
}
You will get the same exception because string "abcd" cannot be converted into integer whereas if you change the var objFirst = "1234" it will work correctly.
I will suggest rather than using int.parse try using int.tryParse .
Hope this will help
Akhil GargPosted Apr 5, 2015, 5:28 AM
Rajeesh MenothPosted Apr 5, 2015, 3:20 AM