Click here to Skip to main content
15,891,372 members
Please Sign up or sign in to vote.
4.00/5 (2 votes)
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.

VB
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
Updated 15-Mar-11 2:15am
v2
Comments
Albin Abel 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
Albin Abel 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

try this on button click
VB
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()
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900