Solved

Export dataset to MS Excel

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Introduction Knockoutjs (Knockout) is a JavaScript framework (Model View ViewModel or MVVM framework).   The main ideology behind Knockout is to control from JavaScript how a page looks whilst creating an engaging user experience in the least …
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 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:
The viewer will learn the benefit of using external CSS files and the relationship between class and ID selectors. Create your external css file by saving it as style.css then set up your style tags: (CODE) Reference the nav tag and set your prop…

830 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