NET C# How to retrieve data from database using SelectCommand in Code Behind

Hi Expert:

Currently I doing ok fetching data from the database and displaying same in gridview using following SELECT code in my (HTML) aspx file:

          connectionstring="<%$ ConnectionStrings:MyNorthwind%>"
          selectcommand="SELECT LastName FROM Employees WHERE Title= @Title">
              <asp:Parameter name="Title" controlid="DropDownList1" propertyname="SelectedValue"/>

And in my aspx.cs (behindcode) I set the user's input search @ parameters something like have something like

Within my Page_Load, I have something like:

C#:  productDataSource1.SelectParameters["Title"].DefaultValue = strTitles;

However, now in my codebehind I need to retrieve the same data retrieved from database and currently displayed in gridview and pass this same retrieved data (all the gridview records) into a piece of code which is suppose to print all the gridview row records to an EXCEL file by clicking a  printbutton on located the gridview page?

NET C# Again, in code behind - How do I go about passing the same retrieved data records stream to a print
Excel routine located in my codehind file.


Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Are you using Visual Studio .NET?

If so what version?
u will have to fetch all those records again .. using sessions etc to store the state of the grid is not advicible .. fetch all the records, change the contentType of the response object to excel .. u will loads of examples for the same in the net ..

these links should help you (google search using the following URL found the following links ..

Export ASP.NET DataGrid to Excel

you should be able to find other examples too .. ie if u want to export from a dataset .. but in ur case exporting from a datagrid is more approriate ..


Cloud Class® Course: Certified Penetration Testing

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

i just noticed you have mentioned "gridview" and also using "sqldatasource".. so you must be using VS2005 and examples mentioned above uses VS2003 and uses datagrid .. but the same could be applciable for VS2005 and gridview too ..

I want to write a page that connects to a db on the network(LAN), client retreives data from this db on the other PC. Please help
pls ask a new question and its not nice to ask ur queries here, esp. when its not even related to the topic been discussed ..

RSSIAdminAuthor Commented:
Date: 03/21/2006 03:49PM EST

Are you using Visual Studio .NET?

Yes  I am.

did u have a look at the links I have given above? Are u still having any issues ..
RSSIAdminAuthor Commented:

Thanks for getting back to me!

Yes the links look good.

But I am curious about  how in my code behind ? can I retrieve the same data from the database like my selectcommand="SELECT LastName FROM Employees WHERE Title= @Title"> located in the aspx page (Design page)
is doing?

For example do I have to create a SQL query string and connection in my codebehind file to fetch the same data from the database. Is there a way to grab the data returned from the selectcommand in my aspx page which is currently being displayed in my  gridview.

am not very sure about that .. but as ur grid is been exported directly .. there is no need for another SQL statement .. right? its the contentType of the Response that u r changing .. the rest of the page remains as it is .. rememner ur page should not have any other buttons etc when been exported to excel .. just the grid ..
RSSIAdminAuthor Commented:
Rejojohny........sounds good!

Frankly I am new to this .NET 2.0 stuff  althought it looks neat!

Can you think of any more gridview to  Excel examples in C# that I could reference.


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.