petre ricardo

petre ricardo

  • NA
  • 194
  • 0

How to handle selectedIndex changed throws an exception?

Apr 6 2010 7:46 AM



Hi friend,
The following code here:
Dont worry about sytactical errors....

public partial class frmSimulateStacking : Form
{
frmSimulateStacking fSimStacking;
Product myProduct;
public frmSimulateStacking()
{
InitializeComponent();
//soon after the init
//Console.WriteLine(cmbProdNumber.SelectedIndex.ToString());
}
private void frmSimulateStacking_Load(object sender, EventArgs e)
{
//cmbProdNumber.SelectedIndex = -1;
DataTable dt = Product.GetAllProductCodes();
cmbProdNumber.DataSource = dt;
cmbProdNumber.DisplayMember =
"PProdCode";
cmbProdNumber.ValueMember =
"PProdCode";
myProduct =
new Product();
//Console.WriteLine(cmbProdNumber.SelectedIndex.ToString());
}
private void cmbProdNumber_SelectedIndexChanged(object sender, EventArgs e)
{
//not coded
if (cmbProdNumber.Items.Count != 0)
{
//Console.WriteLine(cmbProdNumber.SelectedValue.ToString());
List<string> lstCollection = myProduct.GetProductNameByCode(cmbProdNumber.Text);
foreach (string item in lstCollection)
{
tbxProdName.Text = item;
}
}
}

at form load the combox will be loaded with a datatable of Product Codes. what i need to load is the according product name to the textbox. User selects a Product code from the drop down and it will fecth a Product name of that Product code from the database at formload() i need to set the SelectedIndex of the combox to -1 .... only when the user selects the a product code from the drop down it loaded product name to the textbox...
the problem that im facing is that, when form loads, it throws an error as:

 Photobucket

 
How do i fix this error?
thanks