Reputation: 193
I have an error with a combobox
My code:
SqlConnection conn = new SqlConnection();
try
{
conn = new SqlConnection(@"Data Source=SHARKAWY;Initial Catalog=Booking;Persist Security Info=True;User ID=sa;Password=123456");
string query = "select FleetName, FleetID from fleets";
SqlCommand cmd = new SqlCommand(query, conn);
cmd.CommandText = query;
conn.Open();
SqlDataReader drd = cmd.ExecuteReader();
while (drd.Read())
{
cmbTripName.Items.Add(drd["FleetName"].ToString());
cmbTripName.ValueMember = drd["FleetID"].ToString();
cmbTripName.DisplayMember = drd["FleetName"].ToString();
}
}
catch
{
MessageBox.Show("Error ");
}
The data is presented in the combobox, but when you change the selection valuemember the displaymember does not change.
It's working now but when I click the button to show the data
private void button1_Click(object sender, EventArgs e)
{
label1.Text = cmbTripName.DisplayMember;
label2.Text = cmbTripName.ValueMember;
}
This is displayed:
FleetName
FleetID
It does not display the value
Upvotes: 13
Views: 164924
Reputation: 1
You should use like this
private void button1_Click(object sender, EventArgs e)
{
label1.Text = cmbTripName.Text;
label2.Text = cmbTripName.SelectedValue.ToString();
}
Upvotes: 0
Reputation: 23
Try this:
using MySql.Data.MySqlClient;
Write that:
MySqlConnection con = new MySqlConnection("datasource=172.16.2.104;port=3306;server=localhost;database=DB_Name;uid=root;password=DB_Password;sslmode=none;charset=utf8;");
MySqlCommand cmd = new MySqlCommand();
var query2 = "SELECT `salary` FROM `employees_db` ";
using (var command2 = new MySqlCommand(query2, con))
{
using (var reader2 = command2.ExecuteReader())
{
while (reader2.Read())
{
combox.Text = reader2.GetString("salary");
}
}
}
Upvotes: 0
Reputation: 19
string query = "SELECT column_name FROM table_name"; //query the database
SqlCommand queryStatus = new SqlCommand(query, myConnection);
sqlDataReader reader = queryStatus.ExecuteReader();
while (reader.Read()) //loop reader and fill the combobox
{
ComboBox1.Items.Add(reader["column_name"].ToString());
}
Upvotes: 0
Reputation: 31
void Fillcombobox()
{
con.Open();
cmd = new SqlCommand("select ID From Employees",con);
Sdr = cmd.ExecuteReader();
while (Sdr.Read())
{
for (int i = 0; i < Sdr.FieldCount; i++)
{
comboID.Items.Add( Sdr.GetString(i));
}
}
Sdr.Close();
con.Close();
}
Upvotes: 3
Reputation: 1
private void StudentForm_Load(object sender, EventArgs e)
{
string q = @"SELECT [BatchID] FROM [Batch]"; //BatchID column name of Batch table
SqlDataReader reader = DB.Query(q);
while (reader.Read())
{
cbsb.Items.Add(reader["BatchID"].ToString()); //cbsb is the combobox name
}
}
Upvotes: 0
Reputation: 11
SqlConnection conn = new SqlConnection(@"Data Source=TOM-PC\sqlexpress;Initial Catalog=Northwind;User ID=sa;Password=xyz") ;
conn.Open();
SqlCommand sc = new SqlCommand("select customerid,contactname from customers", conn);
SqlDataReader reader;
reader = sc.ExecuteReader();
DataTable dt = new DataTable();
dt.Columns.Add("customerid", typeof(string));
dt.Columns.Add("contactname", typeof(string));
dt.Load(reader);
comboBox1.ValueMember = "customerid";
comboBox1.DisplayMember = "contactname";
comboBox1.DataSource = dt;
conn.Close();
Upvotes: 1
Reputation: 1313
To use the Combobox
in the way you intend, you could pass in an object to the cmbTripName.Items.Add
method.
That object should have FleetID
and FleetName
properties:
while (drd.Read())
{
cmbTripName.Items.Add(new Fleet(drd["FleetID"].ToString(), drd["FleetName"].ToString()));
}
cmbTripName.ValueMember = "FleetId";
cmbTripName.DisplayMember = "FleetName";
The Fleet
Class:
class Fleet
{
public Fleet(string fleetId, string fleetName)
{
FleetId = fleetId;
FleetName = fleetName
}
public string FleetId {get;set;}
public string FleetName {get;set;}
}
Or, You could probably do away with the need for a Fleet
class completely by using an anonymous type...
while (drd.Read())
{
cmbTripName.Items.Add(new {FleetId = drd["FleetID"].ToString(), FleetName = drd["FleetName"].ToString()});
}
cmbTripName.ValueMember = "FleetId";
cmbTripName.DisplayMember = "FleetName";
Upvotes: 8
Reputation: 8359
You will have to completely re-write your code. DisplayMember and ValueMember point to columnNames! Furthermore you should really use a using block
- so the connection gets disposed (and closed) after query execution.
Instead of using a dataReader to access the values I choosed a dataTable and bound it as dataSource onto the comboBox.
using (SqlConnection conn = new SqlConnection(@"Data Source=SHARKAWY;Initial Catalog=Booking;Persist Security Info=True;User ID=sa;Password=123456"))
{
try
{
string query = "select FleetName, FleetID from fleets";
SqlDataAdapter da = new SqlDataAdapter(query, conn);
conn.Open();
DataSet ds = new DataSet();
da.Fill(ds, "Fleet");
cmbTripName.DisplayMember = "FleetName";
cmbTripName.ValueMember = "FleetID";
cmbTripName.DataSource = ds.Tables["Fleet"];
}
catch (Exception ex)
{
// write exception info to log or anything else
MessageBox.Show("Error occured!");
}
}
Using a dataTable may be a little bit slower than a dataReader but I do not have to create my own class. If you really have to/want to make use of a DataReader you may choose @Nattrass approach. In any case you should write a using block!
EDIT
If you want to get the current Value of the combobox try this
private void cmbTripName_SelectedIndexChanged(object sender, EventArgs e)
{
if (cmbTripName.SelectedItem != null)
{
DataRowView drv = cmbTripName.SelectedItem as DataRowView;
Debug.WriteLine("Item: " + drv.Row["FleetName"].ToString());
Debug.WriteLine("Value: " + drv.Row["FleetID"].ToString());
Debug.WriteLine("Value: " + cmbTripName.SelectedValue.ToString());
}
}
Upvotes: 23
Reputation: 4703
Out side the loop, set following.
cmbTripName.ValueMember = "FleetID"
cmbTripName.DisplayMember = "FleetName"
Upvotes: 1