DataTable DT在Form1_Load中使用没有问题,但在别的模块中就不能引用,我已将开头将DataTable DT设为全局变量,为什么还不可以呢?       public DataTable DT = null;        private void Form1_Load(object sender, EventArgs e)
        { 
            SqlConnection con = new SqlConnection(ConnString);
            con.Open();
            SqlCommand cmd = new SqlCommand("select * from bumen", con);
            SqlDataAdapter sda = new SqlDataAdapter(cmd);
            DataTable DT = new DataTable();            try
            {
                sda.Fill(DT);
            }
            catch
            {
            }
            finally
            {
                cmd = null;
                con.Close();
            }
         }        private void button1_Click(object sender, EventArgs e)
        {
            MessageBox.Show(DT.Rows.Count.ToString());
        }
未将对象引用设置到对象的实例。

解决方案 »

  1.   

    try 
                { 
                    DataSet ds = new DataSet();                
                    sda.Fill(ds);
                    DT = ds.Tables[0]; 
                } 
                catch 
                { 
                } 
                finally 
                { 
                    cmd = null; 
                    con.Close(); 
                } 
      

  2.   

    public DataTable DT = null;  //这里是全局的DT        private void Form1_Load(object sender, EventArgs e) 
            { 
                SqlConnection con = new SqlConnection(ConnString); 
                con.Open(); 
                SqlCommand cmd = new SqlCommand("select * from bumen", con); 
                SqlDataAdapter sda = new SqlDataAdapter(cmd); 
                //DataTable DT = new DataTable(); //这里是局部变量DT
                DT = new DataTable(); //改成这样
     
                try 
                { 
                    sda.Fill(DT); 
                } 
                catch 
                { 
                } 
                finally 
                { 
                    cmd = null; 
                    con.Close(); 
                } 
            } 
      

  3.   

    webform不能这样用sda.Fill(DT); 
    session["a"]=DT;DT=(datetable)session["a"];
    MessageBox.Show(DT.Rows.Count.ToString());   
    *****************************************************************************
    欢迎使用CSDN论坛专用阅读器 : CSDN Reader(附全部源代码) 
    http://feiyun0112.cnblogs.com/
      

  4.   

    public DataTable DT = null;         private void Form1_Load(object sender, EventArgs e) 
            { 
                SqlConnection con = new SqlConnection(ConnString); 
                con.Open(); 
                SqlCommand cmd = new SqlCommand("select * from bumen", con); 
                SqlDataAdapter sda = new SqlDataAdapter(cmd); 
                DataTable DT = new DataTable();             try 
                { 
                    sda.Fill(DT); 
                } 
                catch 
                { 
                } 
                finally 
                { 
                    cmd = null; 
                    con.Close(); 
                } 
            }         private void button1_Click(object sender, EventArgs e) 
            { 
                MessageBox.Show(DT.Rows.Count.ToString()); 
            }把红字部分的DataTable去掉
      

  5.   

    未将对象引用设置到对象的实例。
    用通常的说就是没有new某个对象.
      

  6.   


    对的。
    DataTable DT = new DataTable(); 这样只是在Form1_Load中又定义了一个DT,并没有实例化你的全局变量public DataTable DT。
      

  7.   

    DataTable DT = new DataTable(); 
    改成DT=new DataTable();
    而且变量一般设置为private
      

  8.   

    DataTable DT = new DataTable(); -->>>>>  DT = new DataTable();  DataTable DT = new DataTable();   DT 还不是没数据的!            try 
                { 
                    sda.Fill(DT); 
                } 建议楼主:调试一下,跟踪值,就发现问题了!这样下次就很深刻了!
      

  9.   

    你load里面又申请了一个变量~!!!
    里面的DT,和外面的DT不是同一个        private void button1_Click(object sender, EventArgs e) 
            { 
                MessageBox.Show(DT.Rows.Count.ToString()); 
            } 这个DT,用的是外面那个DT,没有初始化,当然报错了