Click here to Skip to main content
15,905,316 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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;
}





}




}
Posted
Comments
Sergey Alexandrovich Kryukov 6-May-13 2:13am    
Please stop posting non-answers as "solution". It can give you abuse reports which eventually may lead to cancellation of your CodeProject membership.
Comment on any posts, reply to available comments, or use "Improve question" (above).
Also, keep in mind that members only get notifications on the post sent in reply to there posts.
—SA

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,
 
Share this answer
 
Comments
mr.priyank 29-Mar-13 9:43am    
You dont need to worry about the disconnected method. The disconnected method comes in play when you perform any changes in the DataTable and Save these changes to database through this datatable. In this case we are performing only select query. So in this case the disconnected method dosent comes in play.
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...
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 6-May-13 2:12am    
Not an answer, should be removed.
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900