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());
}
未将对象引用设置到对象的实例。
{
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());
}
未将对象引用设置到对象的实例。
解决方案 »
- net怎么检测uploadfile上传的文件大小
- 如何让控件的text换行(ps:不用代码实现)
- 多线程 ftp 上传问题
- 请教利用字典控制对象的一个问题
- 在线等待:vs.net 2008编译的程序不能在windows 2000下运行吗?
- 含水晶报表的WEB工程发布后,报表工具栏有问题!
- 求(类似于)摇奖程序
- 【C#窗口滚动】怎么实现panel的背景图片大于pael的窗体大小时,出现滚动条
- 关于DropDownList控件条件限制查询
- 一个简单的问题(等待)
- 一个UpdatePanel中放两个ModalPopupExtender,两个Panel 但是在UpdatePanel中弹出一个消息框的同时,用ModalPopupExtender的那个弹出层其中一个也显示出来了 求高手解决!
- gridview添加新列
{
DataSet ds = new DataSet();
sda.Fill(ds);
DT = ds.Tables[0];
}
catch
{
}
finally
{
cmd = null;
con.Close();
}
{
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();
}
}
session["a"]=DT;DT=(datetable)session["a"];
MessageBox.Show(DT.Rows.Count.ToString());
*****************************************************************************
欢迎使用CSDN论坛专用阅读器 : CSDN Reader(附全部源代码)
http://feiyun0112.cnblogs.com/
{
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去掉
用通常的说就是没有new某个对象.
对的。
DataTable DT = new DataTable(); 这样只是在Form1_Load中又定义了一个DT,并没有实例化你的全局变量public DataTable DT。
改成DT=new DataTable();
而且变量一般设置为private
{
sda.Fill(DT);
} 建议楼主:调试一下,跟踪值,就发现问题了!这样下次就很深刻了!
里面的DT,和外面的DT不是同一个 private void button1_Click(object sender, EventArgs e)
{
MessageBox.Show(DT.Rows.Count.ToString());
} 这个DT,用的是外面那个DT,没有初始化,当然报错了