Reputation: 25
I've got this where I fill in info from my database, but I now want to pull out RTID
void FillCombo()
{
//FJERN IKKE DET HER, IT WORKS, DON'T FIX IT
SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=Alpha-KHMB;Integrated Security=True");
string sql = "SELECT* FROM ResourceType";
SqlCommand cmd = new SqlCommand(sql,con);
SqlDataReader myreader;
//FJERN IKKE DET HER, IT WORKS, DON'T FIX IT
try
{
con.Open();
myreader = cmd.ExecuteReader();
while (myreader.Read())
{
int RTID = myreader.GetInt32(0);
string sname = myreader.GetString(1);
Bx_RT.Items.Add(RTID + " " + sname);
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
This is what I got so far pulling out the RTID, but I can't because it tells me it's an object, I'm at a loss for how to fix this.
private void Btn_Sv_Click(object sender, RoutedEventArgs e)
{
string RTID = ((ComboBoxItem)Bx_RT.SelectedItem).Content.ToString();
CreateResource(RTID);
}
Upvotes: 0
Views: 83
Reputation: 149
What you can do is when you get your data from database, you can pass whole data to combobox's DataSource property. Then assign ValueMember and DisplayMember properties with corresponding column names of data (ex. Id and Name) to display items properly.
ComboBox1.ValueMember = "id";
ComboBox1.DisplayMember = "name";
ComboBox1.DataSource = dataTable;
After that in your click event you can just get Selectedvalue of combobox. This will return you ValueMember of selected item.
ComboBox1.SelectedValue
Upvotes: 2