我收到此错误如何解决 [英] I got this error how to resolve it
本文介绍了我收到此错误如何解决的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
protected void Page_Load(object sender, EventArgs e)
{
string connection = System.Configuration.ConfigurationManager.ConnectionStrings["itax1"].ConnectionString;
SqlConnection con = new SqlConnection(connection);
con.Open();
SqlDataAdapter da;
da = new SqlDataAdapter("Select * from pa_salary_master", con);
da = new SqlDataAdapter();
DataSet ds = new DataSet();
da.Fill(ds);
GridView1.DataSource = ds;
GridView1.DataBind();
}
我收到此错误
在调用填充"之前,尚未初始化SelectCommand属性.
这里的数据是水平对齐的,但是我是垂直对齐的.
i got this error
The SelectCommand property has not been initialized before calling ''Fill''.
here data is align horigentally but i nedd vortically. how to align vartically.
推荐答案
SqlDataAdapter da;
da = new SqlDataAdapter("Select * from pa_salary_master", con);
da = new SqlDataAdapter();
仅使用单适配器.
use only singe adapter.
ie. da = new SqlDataAdapter("Select * from pa_salary_master", con);
Remove da = new SqlDataAdapter();
&
Remove con.Open();
将此代码替换为
Replace your code with this
protected void Page_Load(object sender, EventArgs e)
{
string connection = System.Configuration.ConfigurationManager.ConnectionStrings["itax1"].ConnectionString;
SqlConnection con = new SqlConnection(connection);
SqlDataAdapter da;
DataSet ds = new DataSet();
using(da = new SqlDataAdapter("Select * from pa_salary_master", con))
{
da.Fill(ds);
}
GridView1.DataSource = ds;
GridView1.DataBind();
}
您已重新初始化da.并且新的da没有初始化选择命令文本
只需删除该行
You have reinitialized da. and the new da dont have select command text initialized
just remove the line
da = new SqlDataAdapter();
您也不需要在这里打开连接
适配器将对其进行管理.
所以也要删除
also you dont need to open the connection here
adapter will manage it.
so also remove
con.Open();
这篇关于我收到此错误如何解决的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文