![](/img/trans.png)
[英]HtmlAgilityPack example for changing links doesn't work. How do I accomplish this?
[英]My if else statement doesn't seems to work. How do I prevent sending empty data to the Database?
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();
}
它似乎有效,但只有一个缺陷:当我同时提交两个文本为空时,弹出“请输入过敏类型”。 如果两个文本都为空,怎么办呢?
如果txtNewAllergy
和txtReactions
是TextBox
,那么.Text
永远不会为null
; 您需要检查一个空的非null字符串。 尝试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");
}
您的INSERT
查询缺少INTO
关键字。
尝试将strAllergies
更改为此:
string strAllergies = @"INSERT INTO ALLERGIES (allergyType, allergyDesc, patientID)
VALUES (@insertType, @insertDesc, @insertPatient)";
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.