添加数据库记录问题?
下面是代码:SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["LocalSqlServer"].ConnectionString);
string SqlStrInsert = "Insert into nianjian(chepai,bencisongjianriqi) values(' " + this.TBchepaihao.Text.ToString().Trim() + " ',' " +this.Ddlnianjianriqi.Value.ToString("d").Trim())";
string SqlSelect = "Select * from nianjian where chepai='" + this.TBchepaihao.Text.ToString().Trim()+ "'";
SqlCommand cmd = new SqlCommand(SqlSelect, conn);
SqlCommand cmdInsert = new SqlCommand(SqlStrInsert, conn);
try
{
conn.Open();
SqlDataReader sdr = cmd.ExecuteReader();
if (sdr.Read())
{
lblMessage.Text = "您输入的车牌号已经存在,请输入其它车牌号";
sdr.Close();
}
else
{
sdr.Close();
try
{
if (conn.State.ToString() == "Closed")
conn.Open();
cmdInsert.ExecuteNonQuery();
}
catch (Exception ex)
{
Response.Write("数据库错误,错误原因:" + ex.Message);
Response.End();
}
lblMessage.Text = "信息添加成功";
}
}
catch (System.Exception ee)
{
Response.Write("<script language=javascript>alert(" + ee.Message.ToString() + ")</script>");
}
finally
{
cmd.Dispose();
cmdInsert.Dispose();
if (conn.State.ToString() == "Open")
conn.Close();
}
}
求助:为什么每次输入相同的车牌号就是不会提示我要显示的信息:你的车牌号已经存在。。。。(红色的部分不执行)
就是会显示数据库的错误原因:。。。
不知道错在哪里?请高手看看!