问题描述
- System.NullReferenceException: 未将对象引用设置到对象的实例
-
private void CourceDetailsShow()
{
string connStr = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString; //取出连接字符串
string SqlStr = "SELECT Cource.*,Teacher.teaName,Depart.departName from Cource,Teacher ,Depart where Cource.teaID=Teacher.teaID and Cource.courceID='" + Request["courceID"].ToString() + "' and Cource.teaID='" + Request["teaID"].ToString() + "'and Depart.departID='" + Request["courceDepart"] + "'";
SqlConnection conn = new SqlConnection(connStr); //创建连接对象
try
{
if (conn.State.ToString() == "Closed") //连接如果关闭,打开
conn.Open();
SqlCommand cmd = new SqlCommand(SqlStr, conn);
SqlDataReader sdr = cmd.ExecuteReader();
if (sdr.Read())
{
courceName.Text = sdr["courceName"].ToString();
txtName.Text = sdr["courceName"].ToString();
ddlTeacherName.SelectedItem.Text = sdr["teaName"].ToString();
txtDepart.Text = sdr["departName"].ToString();
txtTime.Text = sdr["courceTime"].ToString();
txtAddress.Text = sdr["courceAddress"].ToString();
txtDetails.Text = sdr["courceInfo"].ToString();
}
else
{
Response.Write("数据库错误,没查询到该门课程!");
Response.End();
}
}上面是主要的代码,但是就是一直提示System.NullReferenceException: 未将对象引用设置到对象的实例,也找不出哪里错,急!!!
解决方案
Request["xxx"]
这些都可能不存在。仔细调试下,才知道问题
时间: 2024-09-20 05:40:02