SqlCommand cmd2 = new SqlCommand("select img.img from img inner join cars on img.carid= cars.carid where cars.carnum = '"+crn+"'", con);
SqlDataReader reader = cmd2.ExecuteReader();
while (reader.Read())
{
Label2.Text = reader.GetString(0);
}
string car = Label2.Text;
if (car == null)
{
string qry1 = "insert into img(img, carid)" + " values ('noimg.jpg', (select carid from cars where carnum ='" + crn + "'";
SqlCommand cmd3 = new SqlCommand(qry1, con);
cmd3.ExecuteNonQuery();
}
int ab = cmd1.ExecuteNonQuery();
if (ab != null)
{
string myStringVariable = string.Empty;
myStringVariable = "Your Car data has been entered";
ClientScript.RegisterStartupScript(this.GetType(), "myalert", "alert('" + myStringVariable + "');", true);
}
我想要做的是,如果图像表的select查询传递null,则在数据库中插入默认图像..但是if(car == null)部分根本不执行。它会跳过if部分。要做什么?
答案 0 :(得分:4)
尝试使用if (string.IsNullOrEmpty(car))
。我怀疑标签的文本是否为空而不是空字符串,即""
,但这可以处理任何一种情况。
答案 1 :(得分:1)
而不是检查NULL
;使用IsNullOrEmpty()
方法或使用{/ 1}}方法(例如
IsNullOrWhiteSpace()
(OR)
if (string.IsNullOrEmpty(car))
{