私はVisual Studioを使用してC#でasp.net Webアプリケーションを作成しようとしています。私のWebページの1つに2つのラジオボタンがあります(子を追加するには、子を追加するには&を追加します)。どちらが選択されているかによって、多数のテキストボックスが表示され、登録する必要があります。送信ボタンをデータベースに正常に接続しました。テキストボックスに入力された値は、データベースの対応する 'parents' & 'children'テーブルに正しく送信されています。私のデータベースに値を送信する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 = "";
}
}
}
笑私は私のWebアプリはに値の送信を停止することはできません '@:あなたは残りのコードの実行を防ぐために、ボックスをチェックしている「場合」に、他のケースを追加する必要が
私のデータベース '。それは感覚的になった! – Jonesopolis
'if ... else'は101のプログラミングです。 – Steve
私はそれがばかげて聞こえることを知っている、それは私の頭の中でより良いと思う。私はおそらくそれの周りに代替、少ないダムの方法があることを知っている私はそれを見つけるように見えない! – ACostea