petre ricardo

petre ricardo

  • NA
  • 194
  • 0

load datatable to combox box and the value of the selectedIndex at SelectedIndexChangd event!

Apr 6 2010 11:47 PM

hi friend,
i have the following code to load a list of Products codes to a combo box, the plan is when user selects a prouct code it should find the name of that product from DB and load ti to the textbox tbxProdName... but the laod value to combo box is fine... but in the selectedIndexChanged event, the controls enters the event passing the two conditions Items count and selectedIndex 1= -1
Here si teh code:
 
private
void Form1_Load(object sender, EventArgs e)
{
SqlDataAdapter da = new SqlDataAdapter();
SqlConnection con
=
new SqlConnection(@"Data Source=NEXTELL-0136367\SQLEXPRESS;Initial Catalog=SuperMarketDBV10;Integrated Security=true");
SqlCommand cmd = new SqlCommand();
da.SelectCommand = cmd;
da.SelectCommand.CommandType =
CommandType.StoredProcedure;
da.SelectCommand.CommandText =
"MstSPGetProductCodes";
da.SelectCommand.Connection = con;
da.SelectCommand.Parameters.Add(
"@PProdID", SqlDbType.Int, -1).Direction = ParameterDirection.Output;
da.SelectCommand.Parameters.Add(
"@PProdCode", SqlDbType.Char, 6).Direction = ParameterDirection.Output;

DataTable dt = new DataTable();

con.Open();
da.Fill(dt);
con.Close();

cmbProdNumber.DataSource = dt;
cmbProdNumber.DisplayMember =
"PProdCode";
cmbProdNumber.ValueMember =
"PProdCode";
 
}
private void cmbProdNumber_SelectedIndexChanged(object sender, EventArgs e)
{
if (cmbProdNumber.Items.Count != 0)
{
if (cmbProdNumber.SelectedIndex != -1)
{
Console.WriteLine("Hello");

}
}

 }

 
When the form loads it prints "Hello" twice ... however... the index chaged order of the combox when the form laods is -1 --> 0 if thats te case :
1. how come it enters through the second condition?
2. How do i stop print of the "Hello" when form load and only print "Hello" when the user selects an item from the combox?
thanks

Answers (1)