foreach (Control cnt in panel1.Controls)
{
if (cnt is TextBox)
{
if (cnt.Text == string.Empty)
{
MessageBox.Show("All fields are mandatory");
}
}
else if (cnt is ComboBox)
{
ComboBox cmb = (ComboBox)cnt;
if (cmb.SelectedIndex == -1)
{
MessageBox.Show("All fields are mandatory");
Application.Exit();
}
}
}
string gender;
string dob = cmbDate.Text + "/" + cmbMonth.Text + "/" + cmbYear.Text;
if (rbMale.Checked == true)
gender = rbMale.Text;
else
gender = rbFemale.Text;
query = "Insert into Admissions(Admission_date,Student_name,Father_name,Mother_name,DOB,Gender,Address,State, City,Pincode,Admission_for,Previous_school,Fees)values('" + txtAdmDate.Text + "','" + txtStudentName.Text + "','" + txtFatherName.Text + "','" + txtMotherName.Text + "','" + dob + "','" + gender + "','" + txtAddress.Text + "','" + txtState.Text + "','" + txtCity.Text + "','" + txtPincode.Text + "','" + cmbClass.Text + "','" + txtPreviousSchool.Text + "','" + txtFees.Text + "'); SELECT SCOPE_IDENTITY();";
cmd = new SqlCommand(query, con);
con.Open();
int admId = (int)cmd.ExecuteScalar();
con.Close();
當我離開一些領域,如果我提交表單,我收到一條消息。但是當我點擊OK時,foreach塊之後的代碼也會執行。我如何阻止它發生?如何停止c#中的程序流?
*「當我離開一些領域,如果我提交表單,我收到一條消息。」* - 請準確描述你的意思:什麼字段?哪裏?什麼信息? –
如果你想退出一個循環,你可以使用「break」關鍵字。 – ckuri
如果我使用break,它會從foreach循環中出來,它也會在foreach塊後執行代碼。請參閱我已附上的代碼 – SATYA