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

Rajeesh MenothRank #118 · 18.4K reputationPosted Apr 4, 2015, 11:20 PM EDT
Akhil GargRank #772 · 3K reputationPosted Apr 5, 2015, 1:28 AM EDT
Mukesh NayakRank #1,172 · 1.5K reputationPosted Apr 5, 2015, 2:52 AM EDT
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
Peter Dzuya156 reputationPosted Apr 5, 2015, 3:04 AM EDT
I added the trim(). Still having the same exception. Thanks
Tom MohanRank #912 · 2.3K reputationPosted Apr 5, 2015, 3:47 AM EDT
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()));
Mukesh NayakRank #1,172 · 1.5K reputationPosted Apr 5, 2015, 11:16 AM EDT
Can you try following and see what the output is
Peter Dzuya156 reputationPosted Apr 6, 2015, 12:00 AM EDT
Peter Dzuya156 reputationPosted Apr 6, 2015, 12:00 AM EDT
Gowtham RajamanickamRank #57 · 29K reputationPosted Apr 6, 2015, 12:27 AM EDT