在调用“填充”之前,尚未初始化SelectCommand属性。 [英] The SelectCommand property has not been initialized before calling 'Fill'.

查看:208
本文介绍了在调用“填充”之前,尚未初始化SelectCommand属性。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好我是C#的新手,这是我的代码。当我想从日期范围获得报告(在excel中)时显示错误。错误是在调用''fill''之前,selectcommand属性尚未初始化。





请帮帮我......我的代码是...........











protected void btshow_Click(object sender,EventArgs e)

{

DataSet dsExport = GetData();

System.IO.StringWriter tw = new System.IO.StringWriter();

System.Web.UI.HtmlTextWriter hw =

new System.Web.UI.HtmlTextWriter(tw);

DataGrid dgGrid = new DataGrid();

dgGrid.DataSource = dsExport;



//报告标题



//获取控件的HTML。

dgGrid.HeaderStyle.Font.Bold = true;

dgGrid.DataBind();

dgGrid.RenderControl(hw);



//将HTML写回浏览器。

Response.ContentType =application / vnd.ms-excel;

this.EnableViewState = false;

Response.Write(tw.ToString());

Response.End();



}



public DataSet GetData()

{

DataSet dataSet = new DataSet();

SqlDataAdapter dataAdapter = new SqlDataAdapter();

SqlConnection cnn = new SqlConnection(user id = sa;+

password = prodip @ km; server = PRODIP; +

Trusted_Connection = yes;+

database = Member;);

cnn.Open();

SqlCommand cmd = new SqlCommand(SELECT * FROM [Membermaster] whereled''+ tbfromdate.Text.Trim()+' '和''+ tbtodate.Text.Trim()+'',cnn);

SqlDataReader dr = cmd.ExecuteReader();

dataAdapter.Fill (dataSet);

dataAdapter.Dispose();



返回dataSet;

}











}









}

Hi i am new in C# and this is my code. when i want to get report (in excel) from a date range it shown error. the error is "the selectcommand property has not been initialized before calling ''fill''."


Please help me...... my code is...........





protected void btshow_Click(object sender, EventArgs e)
{
DataSet dsExport = GetData();
System.IO.StringWriter tw = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter hw =
new System.Web.UI.HtmlTextWriter(tw);
DataGrid dgGrid = new DataGrid();
dgGrid.DataSource = dsExport;

//Report Header

// Get the HTML for the control.
dgGrid.HeaderStyle.Font.Bold = true;
dgGrid.DataBind();
dgGrid.RenderControl(hw);

// Write the HTML back to the browser.
Response.ContentType = "application/vnd.ms-excel";
this.EnableViewState = false;
Response.Write(tw.ToString());
Response.End();

}

public DataSet GetData()
{
DataSet dataSet = new DataSet();
SqlDataAdapter dataAdapter = new SqlDataAdapter();
SqlConnection cnn = new SqlConnection("user id=sa;" +
"password=prodip@km;server=PRODIP;" +
"Trusted_Connection=yes;" +
"database=Member;");
cnn.Open();
SqlCommand cmd = new SqlCommand("SELECT * FROM [Membermaster] where actuledate between ''" + tbfromdate.Text.Trim() + "'' and ''" + tbtodate.Text.Trim() + "'' ", cnn);
SqlDataReader dr = cmd.ExecuteReader();
dataAdapter.Fill(dataSet);
dataAdapter.Dispose();

return dataSet;
}





}




}

推荐答案

这里你不需要SqlDataReader



设置dataadapter的Select Command属性。



dataAdapter .SelectCommand = cmd;



然后调用填充方法。



你也可以通过连接dataadapter的构造函数中的字符串或连接和命令文本。



使用dataadapter时你不需要openm并明确关闭连接

datatadpter将管理此自动很简单,
You dont need SqlDataReader here

set the Select Command Property of the dataadapter.

dataAdapter.SelectCommand=cmd;

And then call t he fill method.

You can also pass connection string or connection and Command Text in the constructor of the dataadapter.

also while using dataadapter you dont need to openm and close the connection explicitly
datatadpter will manage this automatically,


非常感谢你先生,它工作正常..



先生,您可以帮助我如何制作毕业视图。我从不这样做。



请给我一步一步解决方案......
Thank u so much sir, it is working fine..

Sir can u help me how i make grad view. i never do this.

please give me steps by steps solution...


这篇关于在调用“填充”之前,尚未初始化SelectCommand属性。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆