asp.net取到数据库里的值为空值的时候报错“在没有任何数据时进行无效的读取尝试”
stringconnstr=WebConfigurationManager.ConnectionStrings["zhanjianziliao"].ConnectionS...
string connstr = WebConfigurationManager.ConnectionStrings["zhanjianziliao"].ConnectionString;
SqlConnection conn = new SqlConnection(connstr);
conn.Open();
string yhm = TextBox1.Text;
string ma = TextBox2.Text;
string dzyx = TextBox3.Text;
string yanzhen = "select Name,Passward from yonghu where Name='" + yhm + "'";
SqlCommand selectcmd = new SqlCommand(yanzhen, conn);
SqlDataReader sdr;
sdr = selectcmd.ExecuteReader();
sdr.Read();
if (sdr["Name"] == DBNull.Value)
{
string sqlstr = "insert into yonghu values('" + yhm + "','" + ma + "','" + dzyx + "')";
SqlCommand cmd = new SqlCommand(sqlstr, conn);
cmd.ExecuteNonQuery();
conn.Close();
TextBox1.Text = "";
TextBox2.Text = "";
TextBox3.Text = "";
Response.Write("<script>alert('注册成功!请在右侧登录!')</script>");
}
else
{
conn.Close();
Response.Write("<script>alert('该用户名已被注册!')</script>");
}
if (sdr["Name"] == DBNull.Value)这句在我的数据表里的Name字段为null的时候会报错
下面是我的数据库
例:
当yhm为hh,ma为hh时,会显示该用户名已被注册!
当yhm为cc,ma为cc时,会提示出错 展开
SqlConnection conn = new SqlConnection(connstr);
conn.Open();
string yhm = TextBox1.Text;
string ma = TextBox2.Text;
string dzyx = TextBox3.Text;
string yanzhen = "select Name,Passward from yonghu where Name='" + yhm + "'";
SqlCommand selectcmd = new SqlCommand(yanzhen, conn);
SqlDataReader sdr;
sdr = selectcmd.ExecuteReader();
sdr.Read();
if (sdr["Name"] == DBNull.Value)
{
string sqlstr = "insert into yonghu values('" + yhm + "','" + ma + "','" + dzyx + "')";
SqlCommand cmd = new SqlCommand(sqlstr, conn);
cmd.ExecuteNonQuery();
conn.Close();
TextBox1.Text = "";
TextBox2.Text = "";
TextBox3.Text = "";
Response.Write("<script>alert('注册成功!请在右侧登录!')</script>");
}
else
{
conn.Close();
Response.Write("<script>alert('该用户名已被注册!')</script>");
}
if (sdr["Name"] == DBNull.Value)这句在我的数据表里的Name字段为null的时候会报错
下面是我的数据库
例:
当yhm为hh,ma为hh时,会显示该用户名已被注册!
当yhm为cc,ma为cc时,会提示出错 展开
2个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询