C#出错了

来源:百度知道 编辑:UC知道 时间:2024/05/15 06:09:28
用了int c = Convert.ToInt32(cs.ExecuteScalar().ToString());这一语句说“未将对象引用设置到对象的实例。”
快来帮忙呀,谢谢
SqlConnection conn = new SqlConnection("server=.;database=pubs;uid=wang;pwd=wang");
try
{
conn.Open();
SqlCommand m = new SqlCommand("select * from employee where emp_id='" + TextBox1.Text + "'", conn);
int a = Convert.ToInt32(m.ExecuteScalar().ToString());
if (a > 0)
{
SqlDataReader r = m.ExecuteReader();
while (r.Read())
{
Response.Write(r.Read().ToString());

}

}
else
{ Response.Write("<script>alert('对不起,您的输入有误')</script>"); }
}
catch (SqlException ex)
{ throw ex; }
finally { conn.Close(); }

conn.Open();
SqlCommand m = new SqlCommand("select count(*) from employee where emp_id='" + TextBox1.Text + "'", conn);
int a = Convert.ToInt32(m.ExecuteScalar().ToString());

//应用sql函数(count)让ExecuteScalar方法接受单个数值就不报错了。

检查一下cs是否未初始化或者ExecuteScalar()返回了一个空值,注意一下你的SQL语句

cs为空了