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基本相同的問題。 –