C# Getting data from a database with comboboxes

rajpatel275

New Member
Greetings programmers,I have two comboboxes, called combobox1 and combobox2 (very original names) these comboboxes are connected to my database called database1 (also very original). Now is my problem, I can collect data with combobox1 and show this is some textboxes but with the very same code I can't collect data for combobox2. Both comboboxes are bound to a dataset and combobox2 is connected to combobox1, here is my code:\[code\]private void comboBox1_TextUpdate(object sender, EventArgs e) { string kc = Convert.ToString(comboBox1.SelectedValue); string sql = "SELECT * FROM Klanten WHERE Klantcode = " + kc; Data.Klant_Gegevens(sql,kc); txtadres.Text = Data.adres; txtnummer.Text = Data.nummer; txtplaats.Text = Data.plaats; txttel.Text = Data.tel; } private void comboBox1_SelectedValueChanged(object sender, EventArgs e) { string kc = Convert.ToString(comboBox1.SelectedValue); string sql = "SELECT * FROM Klanten WHERE Klantcode =" + kc; Data.Klant_Gegevens(sql,kc); txtadres.Text = Data.adres; txtnummer.Text = Data.nummer; txtplaats.Text = Data.plaats; txttel.Text = Data.tel; } private void comboBox2_SelectedValueChanged(object sender, EventArgs e) { string pc = Convert.ToString(comboBox2.SelectedValue); string sql2 = "SELECT * FROM PC gegevens WHERE PC_ID =" + pc; Data.PC_Gegevens(sql2,pc); txtmoederbord.Text = Data.moederbord; txthdd.Text = Data.hdd; txtos.Text = Data.os; txtprocessor.Text = Data.processor; txtram.Text = Data.ram; txtvideokaart.Text = Data.videokaart; } private void comboBox2_TextUpdate(object sender, EventArgs e) { string pc = Convert.ToString(comboBox2.SelectedValue); string sql2 = "SELECT * FROM PC gegevens WHERE PC_ID =" + pc; Data.PC_Gegevens(sql2,pc); txtmoederbord.Text = Data.moederbord; txthdd.Text = Data.hdd; txtos.Text = Data.os; txtprocessor.Text = Data.processor; txtram.Text = Data.ram; txtvideokaart.Text = Data.videokaart; }\[/code\]And my Clas called Data:\[code\]public static string cs = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + Application.StartupPath + "\\Database1.accdb"; public static string adres = ""; public static string nummer = ""; public static string plaats = ""; public static string tel = ""; public static string os = ""; public static string moederbord = ""; public static string processor = ""; public static string ram = ""; public static string videokaart = ""; public static string hdd = ""; public static void Klant_Gegevens(string sql, string kc) { using (OleDbConnection con = new OleDbConnection(cs)) { if (kc == "") { adres = ""; nummer = ""; plaats = ""; tel = ""; return; } else { OleDbCommand com = new OleDbCommand(sql, con); con.Open(); OleDbDataReader DB = com.ExecuteReader(); if (!DB.Read()) { return; } adres = DB["Adres"].ToString(); nummer = DB["Huisnummer"].ToString(); plaats = DB["Woonplaats"].ToString(); tel = DB["Telefoonnummer"].ToString(); DB.Dispose(); con.Close(); return; } } } public static void PC_Gegevens(string sql, string pc) { using (OleDbConnection con2 = new OleDbConnection(cs)) { if (pc == "") { os = ""; moederbord = ""; processor = ""; ram = ""; videokaart = ""; hdd = ""; return; } else { OleDbCommand myCommand = new OleDbCommand(sql, con2); con2.Open(); OleDbDataReader DB2 = myCommand.ExecuteReader(); if (!DB2.Read()) { return; } moederbord = DB2["Moederbord"].ToString(); processor = DB2["Processor"].ToString(); ram = DB2["RAM"].ToString(); hdd = DB2["Hardeschijf"].ToString(); videokaart = DB2["Videokaart"].ToString(); os = DB2["OS_Versie"].ToString(); con2.Close(); return; } } }\[/code\]Regards user241062,
 
Back
Top