if的嵌套结构问题
if (ds.Tables[0].Rows.Count != 0)
{
if (textBox11.Text.ToString().Trim() != null )
{
cmd.CommandText = "update 书籍信息表 set BookName = '" + textBox11.Text.ToString().Trim()
+ "' where Bookno = '" + textBox12.Text.Trim() + "'";
success = cmd.ExecuteNonQuery();
}
if (textBox10.Text.ToString().Trim() != null )
{
cmd.CommandText = "update 书籍信息表 set Publisher = '" + textBox10.Text.ToString().Trim()
+ "' where Bookno = '" + textBox12.Text.Trim() + "'";
success = cmd.ExecuteNonQuery();
}
}
大家帮我看看这个嵌套是不是写得有问题,我断点单步调试了,那个textbox明明是空的,它居然会执行if里面的代码,搞不懂为什么
------解决方案--------------------
if(textBox11.Text.ToString().Trim() != null)
类似这种是多余的,即使用户什么都没填,textBox的Text也有值,为""(空串),也就是说永远也不可能为null
------解决方案--------------------C# code
if (ds.Tables[0].Rows.Count != 0)
{
if (textBox11.Text.ToString().Trim() != "")
{
cmd.CommandText = "update 书籍信息表 set BookName = '" + textBox11.Text.ToString().Trim()+ "' where Bookno = '" + textBox12.Text.Trim() + "'";
success = cmd.ExecuteNonQuery();
}
if (textBox10.Text.ToString().Trim() != "")
{
cmd.CommandText = "update 书籍信息表 set Publisher = '" + textBox10.Text.ToString().Trim()+ "' where Bookno = '" + textBox12.Text.Trim() + "'";
success = cmd.ExecuteNonQuery();
}
}