2014-01-31 13 views
0
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缺陷:當我提交兩份案文爲空,彈出「請在密鑰類型的過敏「。如果兩個文本都是空的,我怎麼做它什麼都不做。

+0

' 「」 是= null'所以它會通過使用'如果(txtNewAllergy.Text = NULL && txtNewAllergy.Text = ||的String.Empty txtReactions! .Text!= null && txtReactions.Text!= string.Empty)' – WiiMaxx

+0

'「allergiesResult完全沒有做任何事情''你至少得到最後一個」未提交「的消息,對吧? –

+0

你指定'allergiesResult'沒有做任何事情。你期望從'int'有什麼樣的功能? **編輯**:與@Grant基本相同的問題。 –

回答

7

如果txtNewAllergytxtReactionsTextBox,那就永遠不會成爲預期.Textnull;您需要檢查一個空的非空字符串。嘗試string.IsNullOrEmpty(...)

if (!string.IsNullOrEmpty(txtNewAllergy.Text) 
    || !string.IsNullOrEmpty(txtReactions.Text)) 

爲方便起見,我們傾向於使用擴展方法:

public static bool HasValue(this string value) { 
    return !string.IsNullOrEmpty(value); 
} 

然後,它是:

if (txtNewAllergy.Text.HasValue() || txtReactions.Text.HasValue()) 

另外:注意,就是什麼都不做的代碼路徑(參見「這裏發生了什麼?」):

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 
     { 
      // WHAT HAPPENS HERE? 
     } 
    } 
    else 
    { 
     MessageBox.Show("Submitted, fool"); 
    } 
} 
else 
{ 
    MessageBox.Show("Not submitted, fool"); 
} 
+0

不錯的工作可愛的擴展 – WiiMaxx

+0

此外,如果合適,並使用.NET 4.0或更高版本,請考慮IsNullOrWhitespace()。 – bland

+0

@bland a!String.IsNullOrEmpty和.Length> 0檢查後會更好。 – Max

0

您的INSERT查詢缺少INTO關鍵字。

嘗試改變strAllergies此:

string strAllergies = @"INSERT INTO ALLERGIES (allergyType, allergyDesc, patientID) 
         VALUES (@insertType, @insertDesc, @insertPatient)"; 
+0

感謝您指出,但不會改變任何東西。 – user3195396

+0

您的查詢是否實際插入了任何內容?從我收集的信息來看,事實並非如此。 –