Solved

Export dataset to MS Excel

Posted on 2004-08-06
3
257 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

This article covers the basics of the Sass, which is a CSS extension language. You will learn about variables, mixins, and nesting.
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
The viewer will learn how to count occurrences of each item in an array.

707 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