Link to home
Create AccountLog in
Avatar of RSSIAdmin
RSSIAdmin

asked on

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:

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

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

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.

Help!


Avatar of Argblat
Argblat

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 ..

Rejo
these links should help you (google search using the following URL found the following links .. http://www.google.co.in/search?hl=en&q=excel+export+grid+..&meta=)

Export ASP.NET DataGrid to Excel
http://www.c-sharpcorner.com/Code/2003/Sept/ExportASPNetDataGridToExcel.asp


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 ..

Rejo

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 ..

Rejo
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
Modidy,
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 ..

Rejo
Avatar of RSSIAdmin

ASKER

Argblat
Date: 03/21/2006 03:49PM EST
 Comment  


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 ..
Rejojohny:

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.

Thanks..
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 ..
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.

ASKER CERTIFIED SOLUTION
Avatar of Rejojohny
Rejojohny
Flag of United States of America image

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer