我的代码如下......
if(txtEditName.Text.Trim() == "" || txtEditAddress.Text.Trim() == "")
{
lblBError.Enabled = true;
lblBError.Visible = true;
lblBError.Text = "Please provide the required field.";
return;
}
else
{
if(txtControl.Text.Trim() == "")
{
if(DropDownClient.Enabled)
{
if(DropDownClient.SelectedItem.Value == "select")
{
lblBError.Enabled = true;
lblBError.Visible = true;
lblBError.Text = "Please select Client.";
return;
}
}
else
{
if(lblClientName.Text.Trim() != "")
{
sql = @"INSERT INTO [BRANCH] (bname,baddress,bcity,bstate,bpostcode,bphone,bfax,bemail,clientID)
VALUES (@bname,@baddress,@bcity,@bstate,@bpostcode,@bphone,@bfax,@bemail,(SELECT clientID FROM [CLIENT] WHERE cname='" + lblClientName.Text + "'))";
}
else
{
sql = @"INSERT INTO [BRANCH] (bname,baddress,bcity,bstate,bpostcode,bphone,bfax,bemail,clientID)
VALUES (@bname,@baddress,@bcity,@bstate,@bpostcode,@bphone,@bfax,@bemail," + DropDownClient.SelectedItem.Value + ")";
}
}
}
else
{
sql = @"INSERT INTO [BRANCH] (bname,baddress,bcity,bstate,bpostcode,bphone,bfax,bemail,clientID)
VALUES (@bname,@baddress,@bcity,@bstate,@bpostcode,@bphone,@bfax,@bemail," + Convert.ToInt32(txtControl.Text.Trim()) + " )";
// SqlCommand cmd = new SqlCommand(sql, connection);
}
}
我遇到的问题是,代码的某些部分没有执行。当我跑步时,它忽略了
的其他部分if(lblClientName.Text.Trim() != "")
{
}
else
{
sql = @"INSERT INTO [BRANCH] (bname,baddress,bcity,bstate,bpostcode,bphone,bfax,bemail,clientID)
VALUES (@bname,@baddress,@bcity,@bstate,@bpostcode,@bphone,@bfax,@bemail," + DropDownClient.SelectedItem.Value + ")";
}
它为else部分跳转sql =“”而不是传递sql ass空字符串。我不确定为什么会这样?我检查一切,一切似乎都很好。请问一点代码的问题是什么?
答案 0 :(得分:0)
首先,正如评论中所述,使用string.IsNullOrWhitespace
或string.IsNullOrEmpty
来帮助您:
if (string.IsNullOrWhitespace(txtEditName.Text) || string.IsNullOrWhitespace(txtEditAddress.Text))
{
lblBError.Enabled = true;
lblBError.Visible = true;
lblBError.Text = "Please provide the required field.";
return;
}
else
{
if (!string.IsNullOrWhitespace(txtControl.Text))
{
if (DropDownClient.Enabled && DropDownClient.SelectedItem.Value == "select")
{
lblBError.Enabled = true;
lblBError.Visible = true;
lblBError.Text = "Please select Client.";
return;
}
else
{
if (!string.IsNullOrWhitespace(lblClientName.Text))
{
sql = @"INSERT INTO [BRANCH] (bname,baddress,bcity,bstate,bpostcode,bphone,bfax,bemail,clientID)
VALUES (@bname,@baddress,@bcity,@bstate,@bpostcode,@bphone,@bfax,@bemail,(SELECT clientID FROM [CLIENT] WHERE cname='" + lblClientName.Text + "'))";
}
else
{
sql = @"INSERT INTO [BRANCH] (bname,baddress,bcity,bstate,bpostcode,bphone,bfax,bemail,clientID)
VALUES (@bname,@baddress,@bcity,@bstate,@bpostcode,@bphone,@bfax,@bemail," + DropDownClient.SelectedItem.Value + ")";
}
}
else
{
sql = @"INSERT INTO [BRANCH] (bname,baddress,bcity,bstate,bpostcode,bphone,bfax,bemail,clientID)
VALUES (@bname,@baddress,@bcity,@bstate,@bpostcode,@bphone,@bfax,@bemail," + Convert.ToInt32(txtControl.Text.Trim()) + " )";
// SqlCommand cmd = new SqlCommand(sql, connection);
}
}
}
现在,如果没有sql= ...
行被执行,则必须是dropdownclient已启用且其所选项目=“select”。我无能为力。
编辑:
我试图重构你的代码。原谅任何错误,我没有太多时间这样做:
private bool EditFieldsAreValid()
{
if (string.IsNullOrWhiteSpace(txtEditName.Text) || string.IsNullOrWhiteSpace(txtEditAddress.Text))
return false;
return true;
}
private string CreateSql(string value)
{
return @"INSERT INTO [BRANCH] (bname,baddress,bcity,bstate,bpostcode,bphone,bfax,bemail,clientID)
VALUES (@bname,@baddress,@bcity,@bstate,@bpostcode,@bphone,@bfax,@bemail,(SELECT clientID FROM [CLIENT] WHERE cname='" + value + "'))";
}
if (!EditFieldsAreValid())
{
lblBError.Enabled = true;
lblBError.Visible = true;
lblBError.Text = "Please provide the required field.";
return;
}
if (string.IsNullOrWhiteSpace(txtControl.Text))
{
if (DropDownClient.Enabled && DropDownClient.SelectedItem.Value == "select")
{
lblBError.Enabled = true;
lblBError.Visible = true;
lblBError.Text = "Please select Client.";
}
else
{
if (string.IsNullOrWhiteSpace(lblClientName.Text))
{
sql = CreateSql(lblClientName.Text);
}
else
{
sql = CreateSql(DropDownClient.SelectedItem.Value);
}
}
}
else
{
sql = CreateSql(txtControl.Text.Trim());
}
答案 1 :(得分:0)
谢谢你们的帮助。提格兰先生指出的是非常有帮助的。我发现程序忽略的else语句不应该放在我所说的地方。它应该是这样的。
if (txtControl.Text.Trim() == "")
{
if (DropDownClient.Enabled)
{
if (DropDownClient.SelectedItem.Value == "select")
{
lblBError.Enabled = true;
lblBError.Visible = true;
lblBError.Text = "Please select Client.";
return;
}
else
{
sql = @"INSERT INTO [BRANCH] (bname,baddress,bcity,bstate,bpostcode,bphone,bfax,bemail,clientID)
VALUES (@bname,@baddress,@bcity,@bstate,@bpostcode,@bphone,@bfax,@bemail," + DropDownClient.SelectedItem.Value + ")";
}
}
else
{
if (lblClientName.Text.Trim() != "")
{
sql = @"INSERT INTO [BRANCH] (bname,baddress,bcity,bstate,bpostcode,bphone,bfax,bemail,clientID)
VALUES (@bname,@baddress,@bcity,@bstate,@bpostcode,@bphone,@bfax,@bemail,(SELECT clientID FROM [CLIENT] WHERE cname='" + lblClientName.Text + "'))";
}
else
{
sql = @"INSERT INTO [BRANCH] (bname,baddress,bcity,bstate,bpostcode,bphone,bfax,bemail,clientID)
VALUES (@bname,@baddress,@bcity,@bstate,@bpostcode,@bphone,@bfax,@bemail," + DropDownClient.SelectedItem.Value + ")";
}
}
虽然花了我几个小时。谢谢大家。