Fary
Fary

Reputation: 29

c# query for combobox selected text

I am creating a windows form application that generates timetable automatically. I am having a problem to write a query when a user select semester number from the dropdown(from 1-8) then what should I write in query? My Code is below.

private void button_generate_Click(object sender, EventArgs e)
{
    SqlCommand com;
    SqlConnection con = new SqlConnection("Data Source=MAJOR-DYNASTI;Initial Catalog=ESS;Integrated Security=True");
   con.Open();
    string a = comboBox_semester.SelectedText;
    string str = "SELECT  CourseName, TeacherName, RoomName FROM Course_teacher, RoomInfo where Semester= ORDER BY NEWID(); ";
    com = new SqlCommand(str, con);
    SqlDataReader reader = com.ExecuteReader();
    if (reader.HasRows)
    {

        Random random = new Random();
        var labels = new Label[] { label1, label2, label3, label4, label5, label6, label7, label8,label9,label10,label11,label12,label13,label14,label15,label16,
                                   label17,label18,label19,label20,label21,label22,label23,label24,label25,label26,label27,label28,label29,label30,label31,
                                    label32,label33,label34,label35,label36,label37,label38,label39,label40,label41,label42,label43,label44,label45,label46,label47,label48,
                                    label49,label50};
        label1.Text = ""; label2.Text = ""; label3.Text = ""; label4.Text = ""; label5.Text = ""; label6.Text = "";
        label7.Text = "";label8.Text = "";label9.Text = "";label10.Text = "";label11.Text = "";label12.Text = "";label13.Text = "";label14.Text = "";label15.Text = "";label16.Text = "";
                                   label17.Text = "";label18.Text = "";label19.Text = "";label20.Text = "";label21.Text = "";label22.Text = "";label23.Text = "";label24.Text = "";label25.Text = "";label26.Text = "";label27.Text = "";label28.Text = "";label29.Text = "";label30.Text = "";label31.Text = "";
                                   label32.Text = ""; label33.Text = ""; label34.Text = ""; label35.Text = ""; label36.Text = ""; label37.Text = ""; label38.Text = ""; label39.Text = ""; label40.Text = ""; label41.Text = ""; label42.Text = ""; label43.Text = ""; label44.Text = ""; label45.Text = ""; label46.Text = ""; label47.Text = ""; label48.Text = "";
                                    label49.Text = "";label50.Text = "";

        if (
            comboBox1.SelectedItem==null ||
            comboBox2.SelectedItem==null ||
            comboBox_semester.SelectedItem==null)
        {
            MessageBox.Show("Please Select Complete Detail");

        }
        else
        {
            while (reader.Read())
            {

                int randomNumber = random.Next(1, 50);

                labels[randomNumber].Text = String.Format("{0},\r\n{1},\r\n{2}", reader["CourseName"], reader["TeacherName"], reader["RoomName"]);
            }
        }
    }

I want query for these two lines.

string a = comboBox_semester.SelectedText;
string str = "SELECT  CourseName, TeacherName, RoomName FROM Course_teacher, RoomInfo where Semester=(what should I need to write here) ORDER BY NEWID(););

Please Help

Upvotes: 1

Views: 171

Answers (2)

Drew Kennedy
Drew Kennedy

Reputation: 4168

You need to put your variable a into your query like so:

string str = "SELECT CourseName, TeacherName, RoomName FROM Course_teacher, " + 
"RoomInfo WHERE Semester = @Semester ORDER BY NEWID()";

com.Parameters.Add(new SqlParameter("Semester", a));

Just note that I cleaned up your query a little bit, and I introduced parameterized queries in case you're unfamiliar with them.

Upvotes: 1

aaron
aaron

Reputation: 174

The best way to do this is going to look something like this:

"Where Semester = @Semester"

Then, on a following line, you add a Parameter to your SqlCommand object like so:

com.Parameters.AddWithValue("@Semester", a);

This lets the SQLCommand object know to substitute your variable (I've named it @Semester for now) with the value you have received from the user.

Upvotes: 0

Related Questions