ExecuteScalar: Connection 属性尚未初始化。
来源:百度知道 编辑:UC知道 时间:2024/05/20 00:59:52
{
string newsTypeID = Request.QueryString["TypeID"].ToString();
SqlConnection con = DB.createConnection();
con.Open();
SqlCommand cmd = new SqlCommand("select newsTypeName from newsType where newsTypeID='" + newsTypeID + "',con");
this.Label1.Text = Convert.ToString(cmd.ExecuteScalar());
cmd.CommandText = "select * from newsTile where newsTypeID='" + newsTypeID + "'";
SqlDataReader sdr = cmd.ExecuteReader();
this.GridView1.DataSource = sdr;
this.GridView1.DataBind();
}
源错误:
行 21: con.Open();
行 22: SqlCommand cmd = new SqlCommand("select newsTypeName from newsType where newsTypeID='" + newsTypeID + "',con");
行 23: this.Label1.Text = Con
SqlCommand cmd = new SqlCommand("select newsTypeName from newsType where newsTypeID='" + newsTypeID + "'",con);
加上 SqlConnection conn = new SqlConnection(sql);
然后打开数据库
conn.Open();
然后语句运行完毕
conn.Close();
SqlCommand cmd = new SqlCommand("select newsTypeName from newsType where newsTypeID='" + newsTypeID + "',con"); 写错了,
应改为
SqlCommand cmd = new SqlCommand("select newsTypeName from newsType where newsTypeID='" + newsTypeID + "'",con);
xyphoenix 说的对..很明显的错误..前面几个不知道是怎么搞的...在那乱答..
SqlCommand cmd = new SqlCommand("select newsTypeName from newsType where newsTypeID='" + newsTypeID + "',con");
把con移到"外面.."',con"); 换成"'",con);
下次不要这么粗心了
SqlConnection con = DB.createConnection();
con.ConnectionString = "";//你要连接的数据库字符串
将con.Open();一行移到22行