andreassin
Mitglied
Hallo,
ich bin noch recht frisch was die Programmierung mit C# angeht.
Habe eine CommoBox mit Werten aus einer MySQL-Datenbank gefüllt:
this.cb.Location = new System.Drawing.Point(384, 56);
this.cb.Name = "cb";
this.cb.Size = new System.Drawing.Size(121, 21);
this.cb.TabIndex = 7;
this.cb.Text = "Auswahl";
this.cb.SelectedIndexChanged += new System.EventHandler(this.CbSelectedIndexChanged);
OdbcConnection con = new OdbcConnection();
OdbcDataReader dr = null;
OdbcCommand sql = null;
con.ConnectionString="driver={MySQL ODBC 3.51 Driver};server=localhost;database=adressen;User ID=root;Password=";
con.Open();
sql = new OdbcCommand("select * from person", con);
dr = sql.ExecuteReader();
while (dr.Read()) {
string id = dr.GetString(0);
string vname = dr.GetString(1);
string nname = dr.GetString(2);
cb.Items.Add(id+vname+" "+nname);
}
con.Close();
Jetzt möchte ich, ähnlich wie im HTML nicht die Namen sondern die ID aus der Datenbank als Wert der Auswahl übergeben.
Geht das in C#, wenn ja wie?
Oder gibt es prinzipiell andere Wege so eine ComboBox zu füllen?
Danke
Andreas
ich bin noch recht frisch was die Programmierung mit C# angeht.
Habe eine CommoBox mit Werten aus einer MySQL-Datenbank gefüllt:
this.cb.Location = new System.Drawing.Point(384, 56);
this.cb.Name = "cb";
this.cb.Size = new System.Drawing.Size(121, 21);
this.cb.TabIndex = 7;
this.cb.Text = "Auswahl";
this.cb.SelectedIndexChanged += new System.EventHandler(this.CbSelectedIndexChanged);
OdbcConnection con = new OdbcConnection();
OdbcDataReader dr = null;
OdbcCommand sql = null;
con.ConnectionString="driver={MySQL ODBC 3.51 Driver};server=localhost;database=adressen;User ID=root;Password=";
con.Open();
sql = new OdbcCommand("select * from person", con);
dr = sql.ExecuteReader();
while (dr.Read()) {
string id = dr.GetString(0);
string vname = dr.GetString(1);
string nname = dr.GetString(2);
cb.Items.Add(id+vname+" "+nname);
}
con.Close();
Jetzt möchte ich, ähnlich wie im HTML nicht die Namen sondern die ID aus der Datenbank als Wert der Auswahl übergeben.
Geht das in C#, wenn ja wie?
Oder gibt es prinzipiell andere Wege so eine ComboBox zu füllen?
Danke
Andreas