private void btnSubmitConsultation_Click(object sender, EventArgs e)
{
int medicalHistoryResult = insertMedicalHistory();
if (medicalHistoryResult > 0)
{
MessageBox.Show("Document(s) submitted", "Success");
}
else
{
MessageBox.Show("Insert Fail");
}
int allergiesResult = insertAllergies();
if (allergiesResult > 0)
{
if (txtNewAllergy.Text != null || txtReactions.Text != null)
{
if (txtNewAllergy.Text == null)
{
MessageBox.Show("Please key in the Type of the allergy", "WARNING");
}
else if (txtReactions.Text == null)
{
MessageBox.Show("Please key in the Description of the allergy", "WARNING");
}
}
else
{
MessageBox.Show("Submitted, fool");
}
}
else
{
MessageBox.Show("Not submitted, fool");
}
}
那么医疗历史结果似乎工作正常,但过敏结果并没有做任何事情。我的if else语句似乎不起作用。如何防止向数据库发送空数据?
我的insertAllergies函数只是一个普通的INSERT,没什么奇特的。
这是我insertAllergies功能:
private int insertAllergies()
{
int allergiesResult = 0;
string strConnectionString = ConfigurationManager.ConnectionStrings["DBConnection"].ConnectionString;
SqlConnection connection = new SqlConnection(strConnectionString);
try
{
string strPatient = "SELECT patientID FROM PATIENT WHERE [email protected]";
SqlCommand cmdPatient = new SqlCommand(strPatient, connection);
cmdPatient.Parameters.AddWithValue("@searchPatientID", txtPatientID.Text);
string strAllergies = "INSERT ALLERGIES (allergyType, allergyDesc, patientID) " +
"VALUES (@insertType, @insertDesc, @insertPatient)";
SqlCommand cmdAllergies = new SqlCommand(strAllergies, connection);
connection.Open();
cmdAllergies.Parameters.AddWithValue("@insertType", txtNewAllergy.Text);
cmdAllergies.Parameters.AddWithValue("@insertDesc", txtReactions.Text);
SqlDataReader readPatient = cmdPatient.ExecuteReader();
if (readPatient.Read())
{
string addPatient = readPatient["patientID"].ToString();
cmdAllergies.Parameters.AddWithValue("@insertPatient", addPatient);
}
readPatient.Close();
allergiesResult = cmdAllergies.ExecuteNonQuery();
}
catch (Exception ex)
{
MessageBox.Show("Error: " + ex.Message);
}
finally
{
connection.Close();
}
return allergiesResult;
}
-------------------------------- ------ UPDATE ------------------------------------------- -----
好吧,这是我的新逻辑:
if (string.IsNullOrEmpty(txtNewAllergy.Text) || string.IsNullOrEmpty(txtReactions.Text))
{
if (string.IsNullOrEmpty(txtNewAllergy.Text) && txtReactions.Text != null)
{
MessageBox.Show("Please key in the Type of the allergy", "WARNING");
}
else if (string.IsNullOrEmpty(txtReactions.Text) && txtNewAllergy.Text != null)
{
MessageBox.Show("Please key in the Description of the allergy", "WARNING");
}
}
else if (txtNewAllergy.Text != null && txtReactions.Text != null)
{
int allergiesResult = insertAllergies();
}
看来工作,但只有1缺陷:当我提交两份案文为空,弹出“请在密钥类型的过敏“。如果两个文本都是空的,我怎么做它什么都不做。
' “” 是= null'所以它会通过使用'如果(txtNewAllergy.Text = NULL && txtNewAllergy.Text = ||的String.Empty txtReactions! .Text!= null && txtReactions.Text!= string.Empty)' – WiiMaxx
'“allergiesResult完全没有做任何事情''你至少得到最后一个”未提交“的消息,对吧? –
你指定'allergiesResult'没有做任何事情。你期望从'int'有什么样的功能? **编辑**:与@Grant基本相同的问题。 –