Solved

Export dataset to MS Excel

Posted on 2004-08-06
3
252 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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Suggested Solutions

I will show you how to create a ASP.NET Captcha control without using any HTTP HANDELRS or what so ever. you can easily plug it into your web pages. For Example a = 2 + 3 (where 2 and 3 are 2 random numbers) Session("Answer") = 5 then we…
Shoutout to Emily Plummer (http://www.experts-exchange.com/members/eplummer26.html) for giving me this article! She did most of it, I just finished it up and posted it for her :)    Introduction In a previous article (http://www.experts-exchang…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

747 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

12 Experts available now in Live!

Get 1:1 Help Now