Click here to Skip to main content
15,886,362 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
hello, i wanted to get ID of an selected item in combo box in c#.
i tried the following code and getting the value of selected item, but haven't any idea to get ID from database.
C#
int ProductIndex = ProductsComboBox.SelectedIndex;
      string productName = ProductsComboBox.Items[ProductIndex].ToString();
Posted
Updated 2-Feb-23 10:42am

I assume that whatever class your ProductsComboBox contains has overridden ToString, to give you the human readable value. If so, then just cast the object to your class:
C#
int ProductIndex = ProductsComboBox.SelectedIndex;
MyClass selected = ProductsComboBox.Items[ProductIndex] as MyClass;
if (selected != null)
   {
   string productName = selected.ProductName;
   ...
   }
Or better:
C#
MyClass selected = ProductsComboBox.SelectedItem as MyClass;
if (selected != null)
   {
   string productName = selected.ProductName;
   ...
   }
Presumably, your class contains the ID already!
 
Share this answer
 
When binding to combobox use display member property to display name and valuemember property to Id.

C#
CmbProduct.DataSource=datatable1;
CmbProduct.DisplayMember="ProductName";
CmbProduct.ValueMember="ProductId;
CmbProduct.DataBind();



Then in combobox selected index changed,

C#
int ProductIndex = ProductsComboBox.SelectedIndex;//this will give index
string productName = ProductsComboBox.Text.ToString()//this will give DIsplay name;
int ProductId=ProductsComboBox.SelectedValue.ToString();//this will give product Id
 
Share this answer
 
Comments
SR.Rizwan 29-Jul-12 8:33am    
it is giving me the error "Object reference not set to an instance of an object."
string productName = ProductsComboBox.Text.ToString(); This line works fine, it contains the value of productName.
what i am doing wrong?
Santhosh Kumar Jayaraman 29-Jul-12 9:16am    
Make sure you fetch both product name and productid from database and store it in your datatable. Then make sure you set value member of combobox to productid.
SR.Rizwan 29-Jul-12 11:21am    
yes i am fetching the whole row. below is my code. can't figure it out where is the problem.

private void ProductForm_Shown(object sender, EventArgs e)
{
SqlCeConnection Connection = new SqlCeConnection(ConString);
Connection.Open();
SqlCeDataAdapter da = new SqlCeDataAdapter("Select * from CastingMaterial", Connection);
DataTable dt = new DataTable();
da.Fill(dt);
for (int i = 0; i < dt.Rows.Count; i++)
{
ProductsComboBox.Items.Add(dt.Rows[i]["PartName"]);

}
ProductsComboBox.DisplayMember = "PartName";
ProductsComboBox.ValueMember = "PartId";
Connection.Close();
}

private void ProductsComboBox_SelectedIndexChanged(object sender, EventArgs e)
{
int ProductIndex = ProductsComboBox.SelectedIndex;
string productName = ProductsComboBox.Text.ToString();
int ProductId =Convert.ToInt32(ProductsComboBox.SelectedValue);
SqlCeConnection Connection = new SqlCeConnection(ConString);
Connection.Open();
String Query = "SELECT * From CastingMaterial where PartId=@PartId";
SqlCeDataAdapter da = new SqlCeDataAdapter(Query, Connection);
da.SelectCommand.Parameters.AddWithValue("PartId", ProductId);
DataSet ds = new DataSet();
SqlCeCommandBuilder commandBuilder = new SqlCeCommandBuilder(da);

BindingSource bsource = new BindingSource();

da.Fill(ds, "CastingMaterial");
bsource.DataSource = ds.Tables["CastingMaterial"];
Productgv.DataSource = bsource;
Connection.Close();
}
You can pull the selected item out as a "ComboboxItem" and retrieve the "Value" from there:
Int64.Parse((ProductsComboBox.SelectedItem as ComboboxItem).Value.ToString());
 
Share this answer
 
create a table in sqlServer with two columns. Save your Table as myTable. One column for Product and name it PRODUCT as char. And the other column for storing ProductID named ID as char. And use below code to read from database.

C#
SqlConnection con=new SqlConnection("Connection string");

SqlCommand cmd=new SqlCommand("SELECT ID FROM myTable WHERE PRODUCT='" + ProductsComboBox.Items[ProductIndex].ToString()+"'");

if (con.State == ConnectionState.Closed)
     con.Open();

SqlDataReader rdr = cmd.ExecuteReader();          
while (rdr.Read())
{
     string strID=(string)rdr["ID"];
}


then strID will be the ID of your product.
 
Share this answer
 
v2
C#
  object comboValue;
  private void comboFill()
  {
      DataTable dt = DataBase.GetData("Select CategoryName, Id from Category");
      combxgrup.DataSource = dt;
      combxgrup.DisplayMember = "CategoryName";
      combxgrup.ValueMember = "Id";
      comboValue = combxgrup.SelectedValue;
  }

********************************************************************************
 MessageBox.Show(comboValue);
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900