Tuesday, September 17, 2013

Using CheckBox, TextBox, Label, Button Controls in a Windows Forms Application

The Form shape : 





The Table in the database: 



// OrderID is an Identity, the table name is Order_T

The Order Button code : 

        private void button1_Click(object sender, EventArgs e)
        {
            string Meal1;
            string Meal2;
            string Meal3;
            if (CBMeal1.Checked == true)
            {
                Meal1 = "Ordered";
            }
            else
            {
                Meal1 = "Not ordered";
            }

            if (CBMeal2.Checked == true)
            {
                Meal2 = "Ordered";
            }
            else
            {
                Meal2 = "Not ordered";
            }

            if (CBMeal3.Checked == true)
            {
                Meal3 = "Ordered";
            }
            else
            {
                Meal3 = "Not ordered";
            }

            SqlConnection Con = new SqlConnection();
            Con.ConnectionString = @"Data Source=.\SQLEXPRESS;AttachDbFilename=C:\Users\Saleem\Documents\Project.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True";
            Con.Open();
            String query = "Insert into Order_T(Name , Place, Phone, Meal1, Meal2, Meal3)Values('" + txtName.Text + "','" + txtPlace.Text + "','" + txtPhone.Text + "','" + Meal1 + "','" + Meal2 + "','" + Meal3 + "')";
            SqlCommand com = new SqlCommand(query, Con);
            com.ExecuteNonQuery();
            Con.Close();
            txtName.Text = string.Empty;
            txtPhone.Text = string.Empty;
            txtPlace.Text = string.Empty;
            CBMeal1.Checked = false;
            CBMeal2.Checked = false;
            CBMeal3.Checked = false;
        }

Example : 

The Form : 




// The data entered is only for testing 

The Table Data after pressing the Order Button : 






No comments :

Post a Comment