我无法阻止我的网络应用程序向我的数据库发送值

时间:2016-12-08 19:52:16

标签: c# sql asp.net database visual-studio

所以我试图使用visual studio在c#中创建一个asp.net web应用程序。我的一个网页有两个单选按钮(添加一个父母和一个孩子),根据选择的那个,一些文本框将变得可见,必须填写进行注册。我已成功将提交按钮连接到我的数据库,并且正确地将文本框中输入的值发送给相应的父母' &安培; '儿童'我的数据库中的表格。

我现在遇到的问题是我试图设置它以便在所有文本框中都有一个条目,如果没有,那么会出现一个消息框告诉用户他们需要填写所有字段。我实际上设法让这个工作,我遇到的问题是,即使它告诉你需要填写所有字段,它仍然发送填写到数据库的值。

我想知道的是,如果没有填写任何文本框,是否有办法不打开数据库连接。我在下面附上了我的代码,谢谢:)。

protected void submitBtn_Click(object sender, EventArgs e)
    {
        SqlConnection connect = new SqlConnection("Data  Source=THEBEAST;Initial Catalog=newregDB;Integrated Security=True;Pooling=False");
        {
            if (firstNameBox.Text == "" || surnameBox.Text == "" || dayDobList.Text == "" || monthDobList.Text == "" || yearDobList.Text == "" || genderList.Text == "" || postcodeBox.Text == "" || teleBox.Text == "" || emailBox.Text == "" || userBox.Text == "" || passwordBox.Text == "")
                Response.Write("<script>alert('Please ensure all fields have an entry');</script>");

            if (parentRadBtn.Checked)

            {
                SqlCommand pa = new SqlCommand("INSERT INTO parent(parentID, firstname, surname, postcode, telephone, email, password) VALUES (@parentID, @firstname, @surname, @postcode, @telephone, @email, @password)", connect);
                pa.Parameters.AddWithValue("@parentID", userBox.Text);
                pa.Parameters.AddWithValue("@firstname", firstNameBox.Text);
                pa.Parameters.AddWithValue("@surname", surnameBox.Text);
                pa.Parameters.AddWithValue("@postcode", postcodeBox.Text);
                pa.Parameters.AddWithValue("@telephone", teleBox.Text);
                pa.Parameters.AddWithValue("@email", emailBox.Text);
                pa.Parameters.AddWithValue("@password", passwordBox.Text);

                connect.Open();
                pa.ExecuteNonQuery();
                connect.Close();

                if (IsPostBack)
                {
                    userBox.Text = "";
                    firstNameBox.Text = "";
                    surnameBox.Text = "";
                    postcodeBox.Text = "";
                    teleBox.Text = "";
                    emailBox.Text = "";
                    passwordBox.Text = "";
                }
            }
            else if (childRadBtn.Checked)
            {
                SqlCommand ca = new SqlCommand("INSERT INTO children(childID, firstname, dob, gender, password) VALUES (@childID, @firstname, @dob, @gender, @password)", connect);
                ca.Parameters.AddWithValue("@childID", userBox.Text);
                ca.Parameters.AddWithValue("@firstname", firstNameBox.Text);
                ca.Parameters.AddWithValue("@dob", dayDobList.Text + monthDobList.Text + yearDobList.Text);
                ca.Parameters.AddWithValue("@gender", genderList.Text);
                ca.Parameters.AddWithValue("@password", passwordBox.Text);

                connect.Open();
                ca.ExecuteNonQuery();
                connect.Close();

                if (IsPostBack)
                {
                    userBox.Text = "";
                    firstNameBox.Text = "";
                    dayDobList.Text = "";
                    monthDobList.Text = "";
                    yearDobList.Text = "";
                    genderList.Text = "";
                    passwordBox.Text = "";
                }
            }
        }

2 个答案:

答案 0 :(得分:1)

你需要处理验证方案,但我不会这样做,因为它不是你问的。

你需要在“if”中添加一个else case来检查这些框以防止执行其余的代码:

if (firstNameBox.Text == "" || surnameBox.Text == "" || dayDobList.Text == "" || monthDobList.Text == "" || yearDobList.Text == "" || genderList.Text == "" || postcodeBox.Text == "" || teleBox.Text == "" || emailBox.Text == "" || userBox.Text == "" || passwordBox.Text == "")
            Response.Write("<script>alert('Please ensure all fields have an entry');</script>");
else
{
    //Put the rest of the code here.
}

答案 1 :(得分:0)

 protected void submitBtn_Click(object sender, EventArgs e)
 {
    SqlConnection connect = new SqlConnection("Data  Source=THEBEAST;Initial Catalog=newregDB;Integrated Security=True;Pooling=False");
    {
        if (firstNameBox.Text == "" || surnameBox.Text == "" || dayDobList.Text == "" || monthDobList.Text == "" || yearDobList.Text == "" || genderList.Text == "" || postcodeBox.Text == "" || teleBox.Text == "" || emailBox.Text == "" || userBox.Text == "" || passwordBox.Text == "")
            Response.Write("<script>alert('Please ensure all fields have an entry');</script>");
        else
        {
            if (parentRadBtn.Checked)
            {
                SqlCommand pa = new SqlCommand("INSERT INTO parent(parentID, firstname, surname, postcode, telephone, email, password) VALUES (@parentID, @firstname, @surname, @postcode, @telephone, @email, @password)", connect);
                pa.Parameters.AddWithValue("@parentID", userBox.Text);
                pa.Parameters.AddWithValue("@firstname", firstNameBox.Text);
                pa.Parameters.AddWithValue("@surname", surnameBox.Text);
                pa.Parameters.AddWithValue("@postcode", postcodeBox.Text);
                pa.Parameters.AddWithValue("@telephone", teleBox.Text);
                pa.Parameters.AddWithValue("@email", emailBox.Text);
                pa.Parameters.AddWithValue("@password", passwordBox.Text);

                connect.Open();
                pa.ExecuteNonQuery();
                connect.Close();

                if (IsPostBack)
                {
                    userBox.Text = "";
                    firstNameBox.Text = "";
                    surnameBox.Text = "";
                    postcodeBox.Text = "";
                    teleBox.Text = "";
                    emailBox.Text = "";
                    passwordBox.Text = "";
                }
            }
            else if (childRadBtn.Checked)
            {
                SqlCommand ca = new SqlCommand("INSERT INTO children(childID, firstname, dob, gender, password) VALUES (@childID, @firstname, @dob, @gender, @password)", connect);
                ca.Parameters.AddWithValue("@childID", userBox.Text);
                ca.Parameters.AddWithValue("@firstname", firstNameBox.Text);
                ca.Parameters.AddWithValue("@dob", dayDobList.Text + monthDobList.Text + yearDobList.Text);
                ca.Parameters.AddWithValue("@gender", genderList.Text);
                ca.Parameters.AddWithValue("@password", passwordBox.Text);

                connect.Open();
                ca.ExecuteNonQuery();
                connect.Close();

                if (IsPostBack)
                {
                    userBox.Text = "";
                    firstNameBox.Text = "";
                    dayDobList.Text = "";
                    monthDobList.Text = "";
                    yearDobList.Text = "";
                    genderList.Text = "";
                    passwordBox.Text = "";
                }
            }
        }
    }
}