Solved

Export dataset to MS Excel

Posted on 2004-08-06
3
253 Views
Last Modified: 2010-07-27
Hi all

iam trying to export dataset into excel sheet and to be displayed in browser itself. but when i check for the user session, then excel is getting opened and login page is getting displayed. if i comment the code to check user session its working fine.

below is the code snippet...

 Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'Put user code to initialize the page here

        If Session("login") = "" Then
            Response.Redirect("login.aspx")
        End If
             
            Dim dr As SqlClient.SqlDataReader
            cn.ConnectionString = System.Configuration.ConfigurationSettings.AppSettings("ConnectionString")
            cmd.Connection = cn
            SqlAdapt.SelectCommand = cmd
            cn.Open()
            fillGrid()
            'Dim ses As String = Session("login")
            DataSetToExcel.Convert(ds, Response) '', " test")
            cn.Close()
       
    End Sub

Public Shared Sub Convert(ByVal ds As DataSet, ByVal res As HttpResponse)
        'first let's clean up the response.object
        res.Clear()
        res.Charset = ""
        'set the response mime type for excel
        res.ContentType = "application/vnd.ms-excel"
       

        'create a string writer
        Dim stringWrite As New System.IO.StringWriter()
        'create an htmltextwriter which uses the stringwriter
        Dim htmlWrite As New System.Web.UI.HtmlTextWriter(stringWrite)
        'instantiate a datagrid
        Dim dg As New DataGrid()
        'set the datagrid datasource to the dataset passed in
        dg.DataSource = ds.Tables(0)

        'bind the datagrid
        dg.DataBind()
        'tell the datagrid to render itself to our htmltextwriter
        dg.RenderControl(htmlWrite)
        'all that's left is to output the html
        res.Write(stringWrite.ToString)
        res.End()
    End Sub

thanks in advance....

regs
Mohan
0
Comment
Question by:mohanvamsi_g
3 Comments
 
LVL 1

Accepted Solution

by:
Bharat23 earned 50 total points
ID: 11733938
The user may not have directory rights on the server. Hence, when you check for session the code does not work.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
WSDL Soap Error 12 261
xml attribute DTD attribute differences 3 65
Create 4 <div> below each other in php 2 58
Re-position the objects 7 97
Introduction Since I wrote the original article about Handling Date and Time in PHP and MySQL (http://www.experts-exchange.com/articles/201/Handling-Date-and-Time-in-PHP-and-MySQL.html) several years ago, it seemed like now was a good time to updat…
Browsers only know CSS so your awesome SASS code needs to be translated into normal CSS. Here I'll try to explain what you should aim for in order to take full advantage of SASS.
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

910 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now