Library code snippets

Display SQL Server table data in a browser

Just supply your database connection string and this code will give you a radio button list of all your SQL Server tables and will show their fields. This could be built on to create an ASP.NET version of GenericDB.

<%@ Page Language="c#" %>
<%@ import Namespace="System.Data" %>
<%@ import Namespace="System.Data.SqlClient" %>
<script runat="server">

   void page_load(Object sender, EventArgs e) {
   
       if(!IsPostBack) {
           Bind("");
       }
   }
   
   public void Bind(string table) {
       SqlConnection con = new SqlConnection(ConfigurationSettings.AppSettings["con"]);
       SqlDataAdapter da = new SqlDataAdapter("select name from sysobjects where xtype='U' and name<>'dtproperties'", con);
       DataSet ds = new DataSet();
       da.Fill(ds, "xxx");
       dg.DataSource = ds.Tables[0].DefaultView;
       dg.DataBind();
   
       //list columns in one table
       if(table!="") {
           SqlDataAdapter da2 = new SqlDataAdapter("SELECT syscolumns.name AS [Fields in Items Database], syscolumns.type, syscolumns.length, syscolumns.isnullable FROM sysobjects INNER JOIN syscolumns ON sysobjects.id = syscolumns.id WHERE sysobjects.name = '" + table + "' ORDER BY syscolumns.colid", con);
           DataSet ds2 = new DataSet();
           da2.Fill(ds2, "xxx");
           dg2.DataSource = ds2.Tables[0].DefaultView;
           dg2.DataBind();
       }
   }
   
   void dg_SelectedIndexChanged(Object sender, EventArgs e) {
       Bind(dg.SelectedItem.Text);
   }

</script>
<html>
<head>
</head>
<body>
   <form runat="server">
       <table border="0" cellpadding="5" cellspacing="5">
           <tbody>
               <tr>
                   <td valign="top" bgcolor="tan">
                       <asp:RadioButtonList id="dg" runat="server" DataValueField="Name" DataTextField="Name" AutoPostBack="True" OnSelectedIndexChanged="dg_SelectedIndexChanged"></asp:RadioButtonList>
                   </td>
                   <td valign="top">
                       <asp:DataGrid id="dg2" Cellpadding="2" BackColor="LightGoldenrodYellow" runat="server" GridLines="None" BorderWidth="1px" BorderColor="Tan" ForeColor="Black">
                           <FooterStyle backcolor="Tan"></FooterStyle>
                           <HeaderStyle font-bold="True" backcolor="Tan"></HeaderStyle>
                           <PagerStyle horizontalalign="Center" forecolor="DarkSlateBlue" backcolor="PaleGoldenrod"></PagerStyle>
                           <SelectedItemStyle forecolor="GhostWhite" backcolor="DarkSlateBlue"></SelectedItemStyle>
                           <AlternatingItemStyle backcolor="PaleGoldenrod"></AlternatingItemStyle>
                       </asp:DataGrid>
                   </td>
               </tr>
           </tbody>
       </table>
   </form>
</body>
</html>

Comments

  1. 09 Mar 2004 at 22:38

    Code:
    http://www.developerfusion.com/aspnet/100/

    Code:
    http://www.developerfusion.com/forums/post.aspx?fid=8067

  2. 09 Mar 2004 at 22:37

    Code:
    http://www.developerfusion.com/aspnet/100/

  3. 01 Jan 1999 at 00:00

    This thread is for discussions of Display SQL Server table data in a browser.

Leave a comment

Sign in or Join us (it's free).

Edward Tanguay Edward Tanguay updates his personal web site tanguay.info weekly with code, links, quotes and thoughts on web development. Sign up for the free newsletter.

Related podcasts

Events coming up

  • Mar 15

    DevWeek 2010

    London, United Kingdom

    DevWeek is Europe’s leading independent conference for software developers, database professionals and IT architects, and features expert speakers on a wide range of topics, including .NET 4.0, Silverlight 3, WCF 4, Visual Studio 2010, REST, Windows Workflow 4, Thread Synchronization, ASP.NET 4.0, SQL Server 2008 R2, LINQ, Unit Testing, CLR & C# 4.0, .NET Patterns, WPF 4, F#, Windows Azure, ADO.NET, Entity Framework, Debugging, T-SQL Tips & Tricks, and more.

Want to stay in touch with what's going on? Follow us on twitter!