Click here to Skip to main content
12,349,148 members (64,427 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: VB ASP.NET SQL MS-Excel VB.NET , +
Am trying with the following code to retrieve data from sql server, followed by binding it in datagrid and finally output is displaying in excel sheet.

No errors! But not getting output. Help me out.

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim con = obj_connection_string.getSQLConnection()
If con.State = ConnectionState.Open Then
con.Close()
End If
con.Open()
Dim cmd As New SqlCommand()
cmd.CommandText = "select username,password,isactive from tbl_Login_Details "
cmd.Connection = con
da.SelectCommand = cmd
da.Fill(ds)
cmd.ExecuteNonQuery()
Grid.DataSource = ds
Grid.DataBind()
End Sub
 
Protected Sub btnSubmit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSubmit.Click
Response.Clear()
Response.AddHeader("content-disposition", "attachment;filename=FileName.xls")
 
Response.Charset = ""
 
Response.Cache.SetCacheability(HttpCacheability.NoCache)
 
Response.ContentType = "application/vnd.xls"
 
Dim stringWrite As New System.IO.StringWriter()
 
Dim htmlWrite As System.Web.UI.HtmlTextWriter = New HtmlTextWriter(stringWrite)
 
Grid.RenderControl(htmlWrite)
 
Response.Write(stringWrite.ToString())
 
Response.Close()
End Sub
Posted 15-Mar-11 1:48am
Edited 15-Mar-11 2:15am
Henry Minute225.2K
v2
Comments
AlbinAbel 15-Mar-11 8:25am
   
In the button submit event you have to create a new gridview , set datasource and databind. Use the same datasource variable used for the 'Grid' variable. So that the updated values will be there. Let me know if that works, otherwise let us inform the Author who given this solution
Muthulak 15-Mar-11 8:38am
   
hi the excel sheet is getting opened.But am not getting the fields from table.submit button alone displaying inside the excel sheet
AlbinAbel 15-Mar-11 9:10am
   
You may notify Soni Uma here http://www.codeproject.com/Questions/166124/Export-Gridview-to-Excel-in-ASP-NET.aspx. She/ He may able to help you. From there I got this code snippet. Post a your query as a comment to her answer

1 solution

Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

try this on button click
Response.Clear()
        Response.AddHeader("content-disposition", "attachment;filename=FileName.xls")
 
        Response.Charset = ""
 
        Response.Cache.SetCacheability(HttpCacheability.NoCache)
 
        Response.ContentType = "application/vnd.xls"
 
        Dim stringWrite As New System.IO.StringWriter()
 
        Dim htmlWrite As System.Web.UI.HtmlTextWriter = New HtmlTextWriter(stringWrite)
        Dim dg As System.Web.UI.WebControls.GridView = New System.Web.UI.WebControls.GridView()
 
        dg.DataSource = grid.DataSource
        dg.DataBind()
 
        dg.RenderControl(htmlWrite)
 
        Response.Write(stringWrite.ToString())
 
        Response.End()
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web02 | 2.8.160621.1 | Last Updated 15 Mar 2011
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100