private void Page_Load(object sender, System.EventArgs e)
{
SqlConnection conn=new SqlConnection(System.Configuration.ConfigurationSettings.AppSettings["SQL_Connection"]);

string classid;

string sqlstr="select * from table1 where parentid =0";
conn.Open();
SqlCommand comm=new SqlCommand(sqlstr,conn);
SqlDataReader dr=comm.ExecuteReader();

while (dr.Read())
{
Response.Write(dr["classname"].ToString()+"<br>");
GetType(dr["classid"].ToString());
}


conn.Close();
} public void GetType(string parentid)
{
if (parentid!="0")
{
SqlConnection conn1=new SqlConnection(System.Configuration.ConfigurationSettings.AppSettings["SQL_Connection"]);
conn1.Open();
string sqlstr="select * from table1 where classid="+parentid;
SqlCommand comm1=new SqlCommand(sqlstr,conn1);
comm1.Connection=conn1;
comm1=new SqlCommand(sqlstr);
SqlDataReader dr1=comm1.ExecuteReader();
while(dr1.Read())
{

Response.Write(dr1["classname"].ToString()+"<br>");

}
conn1.Close();
}
}
--------------------------
为什么一运行程序就失败
ExecuteReader: Connection 属性尚未初始化。

解决方案 »

  1.   

    SqlDataReader dr=comm.ExecuteReader();
    大哥 COMM是什么东东啊  你看清楚
      

  2.   

    comm是SqlCommand的实例,跟这个没关系吧~!问题出在只要以调用GetType(classid)就报错,为什么能没有连接呢?
      

  3.   

    SqlCommand comm1=new SqlCommand(sqlstr,conn1);
    comm1.Connection=conn1;
    comm1=new SqlCommand(sqlstr)
    这里好像有问题啊
      

  4.   

    那你看看你的web.config文件中的这个节AppSettings有SQL_Connection吗?
      

  5.   

    哦,你在这GetType(dr["classid"].ToString());
    之后没有关conn
    然后GetType中你又打开了一次