【发布时间】:2014-01-31 13:06:29
【问题描述】:
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");
}
}
嗯,medicalHistoryResult 似乎工作正常,但 allergiesResult 根本没有做任何事情。
我的 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 patientID=@searchPatientID";
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;
}
--------------------------更新------ -------------------------------------------------------
好的,这是我的新逻辑:
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();
}
它似乎有效,但只有一个缺陷:当我将两个文本都提交为空时,会弹出“请输入过敏类型”。如果两个文本都是空的,我该怎么做,它什么都不做。
【问题讨论】:
-
"" is != null所以它会通过使用if(txtNewAllergy.Text != null && txtNewAllergy.Text != string.Empty || txtReactions.Text != null&& txtReactions.Text != string.Empty) -
您指定
allergiesResult没有做任何事情。您希望int提供什么样的功能? 编辑:基本上与@Grant 相同的问题。 -
@Grant 有一个什么都不做的代码路径......(没有任何消息)
-
我重新表述我的问题:
insertAllergies在什么情况下返回大于 0 的数字? -
int allergiesResult = insertAllergies(); ?
标签: c# database winforms visual-studio-2010 if-statement