Show DB Tables In DropDownList - Select From Table

 
 
Sometimes, we have the need to merely look at the tables from a database, choosing the actual table dynamically. This example shows how to list the Table Names from a SQL Server, in a DropDownList, select from the list, and then shows the results of the table in a DataGrid. (Since these tables are not in separate db in this system, it will show up with 'NW' as a prefix to each tablename - naturally, I don't want to show all of my tables :). For a reference, it uses the NorthWind database, so to use it on your page, just copy the code into a new page and change the connection properties.
 
 
  1. <%@ Import Namespace="System.Data" %>

  2. <%@ Import Namespace="System.Data.SQLClient" %>

  3.  

  4. <script language="VB" runat="server">

  5. Dim sql as String

  6. Sub Page_Load(Source as Object, E as EventArgs)

  7.         if not Page.IsPostBack then

  8.                 Dim strConn as string = "server=YourServer;uid=UID;pwd=PWD;database=Northwind"

  9.                 sql  = "SELECT [name] FROM sysobjects WHERE xtype='U' ORDER BY name"

  10.                 Dim conn as New SQLConnection(strConn)

  11.                 Dim Cmd as New SQLCommand(sql, conn)

  12.                 conn.Open()

  13.                 ddl.DataSource = Cmd.ExecuteReader(system.data.CommandBehavior.CloseConnection)

  14.                 ddl.DataBind()

  15.                 conn.Close

  16.         end if

  17.         label1.text=""

  18.         label1.text=sql

  19. End Sub

  20.  

  21. Sub getstuff(Source as Object, E as EventArgs)

  22.         Dim strConn as string = "server=YourServer;uid=UID;pwd=PWD;database=Northwind"

  23.         sql = "Select * from [" 
 
Copy to Clipboard      Download code as Text Format
 
  Date entered : 25th Aug 2007
  Rating : No Rating
  Accessed  :  6562
  Submitted by :  loopsnlogic
 
   Add Comment  Printer friendly
   View All Comments  Email to a friend
   Add to my Favourites Download PDF version
   Rating  
 
                 Click each image to add
this page to each site.
 
 
 
Comments Available
    well,This site is very informative and increases my knowledge a lot about 352-001. I am happy to see this site that plays very important role in our new technology development related to mcp dumps. Articles are also very informative for itil v3 dumps. nice work...keep it up  -     By : pretty11

 
Related ASP.NET Source Codes
  • Spring
  • Wraps a string to a given number of characters using a string break character
  • Uppercase the first character of each word in a string
  • Make a string's first character uppercase
  • Strip whitespace (or other characters) from the beginning and end of a string

  • Copyright © 2013 VisualBuilder. All rights reserved

    Warning: Unknown(): write failed: Disk quota exceeded (122) in Unknown on line 0

    Warning: Unknown(): Failed to write session data (files). Please verify that the current setting of session.save_path is correct (/tmp) in Unknown on line 0