mercredi 22 juillet 2015

Empty Textbox still saves data in MS Sql even columns in table are not allowed null.

I am facing this issue, when I click save button when all textboxes are empty it shows stars labels on all of them. When I fill last textbox leaving all others empty, it saves data into database with empty strings.

How can I handle this issue?

if (tbIDCardNum.Text.Trim() == "")
        {
            lblStarIDCardNum.Visibility = Visibility.Visible;
        }

        if (tbFirstName.Text.Trim() == "")
        {
            lblStarFirstName.Visibility = Visibility.Visible;
        }

        if (rbMale.IsChecked == false && rbFemale.IsChecked == false)
        {
            lblStarGender.Visibility = Visibility.Visible;
        }


        if (tbDOB.Text == "")
        {
            lblStarDOB.Visibility = Visibility.Visible;
        }

        if (tbDateOfJoining.Text == "")
        {
            lblStarDOJ.Visibility = Visibility.Visible;
        }

        if (tbEducation.Text.Trim() == "")
        {
            lblStarEducation.Visibility = Visibility.Visible;
        }

        if (tbCNIC.Text.Trim() == "")
        {
            lblStarCNIC.Visibility = Visibility.Visible;
        }

        if (tbSalary.Text.Trim() == "")
        {
            lblStarSalary.Visibility = Visibility.Visible;
        }

        if (tbAddress.Text.Trim() == "")
        {
            lblStarAddress.Visibility = Visibility.Visible;
        }

        if (tbEmail.Text.Trim() == "")
        {
            lblStarEmail.Visibility = Visibility.Visible;
        }

        if (tbContact1.Text.Trim() == "")
        {
            lblStarContact.Visibility = Visibility.Visible;
        }
        else
        {
            try
            {
                conn.Open();

                cmd.CommandText = "insert into teacher (tIDCardNum, tFirstName, tLastName,tGender, tDOB, tCNIC, tEducation, tSalary, tJoinedOn, tAddress, tEmail, tContact1, tContact2, tContact3,tStatus) values ('" + tbIDCardNum.Text.Trim() + "' , '" + tbFirstName.Text.Trim() + "' , '" + tbLastName.Text.Trim() + "' , '" + gender + "' , '" + tbDOB.Text + "', '" + tbCNIC.Text + "' , '" + tbEducation.Text + "' , '" + tbSalary.Text.Trim() + "' , '" + tbDateOfJoining.Text.Trim() + "' , '" + tbAddress.Text.Trim() + "', '" + tbEmail.Text + "' , '" + tbContact1.Text + "' , '" + tbContact2.Text + "' , '" + tbContact3.Text + "',1)";

                cmd.Connection = conn;
                cmd.ExecuteNonQuery();
                cmd.Clone();
                conn.Close();
                HideStars();
                Refresh();
                MessageBox.Show("Saved");

            }
            catch (Exception ex)
            {
                if (ex.Message.Contains("Violation of PRIMARY KEY constraint "))
                {

                    conn.Close();
                    MessageBox.Show(ex.Message);
                }
                else
                {
                    MessageBox.Show(ex.Message);
                }


            }
        }

Aucun commentaire:

Enregistrer un commentaire