The text of this blog could not be loaded. Read it on the old site: .net
Join the conversation! Your thoughts help the community grow.
Sign in to leave a comment
It is the same account you read, post and publish with — and you will come straight back to this page.
pooja bhPosted Mar 25, 2010, 1:03 PM
Imports System.Data Imports System.Data.SqlClient Partial Class gridreader Inherits System.Web.UI.Page Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load Dim cmd As SqlCommand = New SqlCommand("SELECT *FROM Customers ORDER BY ContactName", New SqlConnection("Server=localhost;Database=northwind;Trusted_Connection=True;")) cmd.Connection.Open() dgrCustomers.DataSource = cmd.ExecuteReader() dgrCustomers.DataBind() cmd.Connection.Close() cmd.Connection.Dispose() End Sub Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click Response.ContentType = "application/vnd.ms-excel" Response.Charset = "" Me.EnableViewState = False Dim stringWriter As New System.IO.StringWriter() Dim htmlWriter As New System.Web.UI.HtmlTextWriter(stringWriter) dgrCustomers.RenderControl(htmlWriter) Response.Write(stringWriter.ToString()) Response.End() End Sub End Class on click of button will display information in excel sheet next is aspx file <%@ Page Language="VB" AutoEventWireup="false" CodeFile="sqldatareader.vb" Inherits="gridreader" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" > <head runat="server"> <title>Untitled Page</title> </head> <body> <form id="form1" runat="server"> <div> <asp:Button ID="button1" runat="server" Text="click me"/> <asp:Label ID="lblOut" runat="server" Text="<h2>Northwind products</h2>" /> <asp:DataGrid ID="dgrCustomers" runat="server" BorderColor="black" BorderWidth="1" GridLines="Both" CellPadding="3" CellSpacing="0" Font-Names="verdana" /> <asp:label ID="lblStatus" runat="server"></asp:label> </div> </form> </body> </html>