Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
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 Minute223.6K
v2
Comments
AlbinAbel at 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 at 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 at 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
0 OriginalGriff 6,656
1 Sergey Alexandrovich Kryukov 6,490
2 CPallini 5,310
3 George Jonsson 3,584
4 Gihan Liyanage 2,650


Advertise | Privacy | Mobile
Web04 | 2.8.140921.1 | Last Updated 15 Mar 2011
Copyright © CodeProject, 1999-2014
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