private void stdName() { string MyConnection2 = "datasource=127.0.0.1;port=3306;username=root;password=;database=anecdotal;"; MySqlConnection MyConn2 = new MySqlConnection(MyConnection2); MySqlDataReader row; MySqlCommand MyCommand2 = new MySqlCommand("SELECT `ID`, `student_id`, `name` FROM student", MyConn2); try { MyConn2.Open(); row = MyCommand2.ExecuteReader(); while (row.Read()) { //Display student in combobox id_no student_id and student_name std_detail.Items.Add(row.GetValue(0).ToString()); std_detail.Items.Add(row.GetValue(1).ToString()); std_detail.Items.Add(row.GetValue(2).ToString()); } MyConn2.Close(); } catch (Exception ex) { MessageBox.Show(ex.Message, "Message", MessageBoxButtons.OK, MessageBoxIcon.Information); } }
Here's my question; 1.)How can I ouput in as one row in my combobox i'm fetching the pk_id,student_id,student_name? 2.)Since in my code I was trying to output it one by one how can make it as one output coming from the database?
Currently in my code it output as a column instead of in one row only I want an output like this (0,BL20201339,CRUZ JUANO) for each loop
Write it like below please:-
while (row.Read()) { std_detail.Items.Add(string.Format("{0},{1},{2}",row.GetValue(0).ToString(),row.GetValue(1).ToString(),row.GetValue(2).ToString())); }
1.4m articles
1.4m replys
5 comments
57.0k users