2 Replies - 1031 Views - Last Post: 28 July 2011 - 04:37 PM Rate Topic: -----

#1 p00ndawg  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 82
  • Joined: 09-November 09

How to populate table from SQL

Posted 27 July 2011 - 06:20 PM

I want to populate my asp:table with the contents from my SQL query.
The page loads but with nothing in the table. any help?

<%@ Page Title="stuff"Language="vb" AutoEventWireup="false" MasterPageFile="~/Site1.Master" CodeBehind="stuff.aspx.vb" Inherits="WebApplication1.WebForm1" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>
<%@ Import Namespace="System.Net.Mail" %>

<script runat ="server">
    
    Dim dbconn1 As SqlConnection = New SqlConnection("server = .\SQLEXPRESS;uid=*****;password=****;database=webapp")
    
    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs)
        If Not Page.IsPostBack() Then
            fill_employeetable()
        End If
        
    End Sub
    
    Sub fill_employeetable()
        dbconn1.Open()
        Dim dbcommR As SqlCommand = New SqlCommand("SELECT UserName FROM aspnet_users", dbconn1)
        Dim daR As SqlDataAdapter = New SqlDataAdapter
        Dim dsR As DataSet = New DataSet
        Dim dee As DataTable = New DataTable
        daR.SelectCommand = dbcommR
        daR.Fill(dsR)
        dbconn1.Close()
        
    End Sub

   
</script>

<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">
</asp:Content>
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
        <div class="Outer">
            <div class="Innerleft">
         
            </div>
             
            <div class="InnerRight">
            
            </div>            
        </div>
        <div class="From">From:</div>
            <div class="Tablediv">
                <asp:Table ID="Table1" runat="server"></asp:Table>        
           </div>
               <div class="subHeading">
               
               </div>
</asp:Content>


This post has been edited by p00ndawg: 27 July 2011 - 06:35 PM


Is This A Good Question/Topic? 0
  • +

Replies To: How to populate table from SQL

#2 demausdauth  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 162
  • View blog
  • Posts: 565
  • Joined: 03-February 10

Re: How to populate table from SQL

Posted 28 July 2011 - 11:00 AM

I know that a DataGrid or GridView object would be able to automatically make the columns for you when you bind to it, using the DataSource property and DataBind() method.

I believe if you want to use a Table object you are going to need to construct a method to loop over each row of the DataTable and create TableRow and then for each DataColumn you will need to create TableCell object. You can make your own method for this, but the best would be to put the code in the OnDataBinding() method for the Table and then use.

Table1.DataBind()



Example using the DataBind() for a table
Was This Post Helpful? 0
  • +
  • -

#3 p00ndawg  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 82
  • Joined: 09-November 09

Re: How to populate table from SQL

Posted 28 July 2011 - 04:37 PM

View Postdemausdauth, on 28 July 2011 - 12:00 PM, said:

I know that a DataGrid or GridView object would be able to automatically make the columns for you when you bind to it, using the DataSource property and DataBind() method.

I believe if you want to use a Table object you are going to need to construct a method to loop over each row of the DataTable and create TableRow and then for each DataColumn you will need to create TableCell object. You can make your own method for this, but the best would be to put the code in the OnDataBinding() method for the Table and then use.

Table1.DataBind()



Example using the DataBind() for a table



Thank you for the response I was able to finally get it working but I am running into another issue. I am not sure If I should make a new thread but I will post it here. I am populating the table now, but I would like to be able to control how I am populating it.


  Dim dc As DataColumn
        Dim dr As DataRow
        For Each dc In ds.Tables(0).Columns
            Dim trow As New TableRow
            For Each dr In ds.Tables(0).Rows
                Dim tcell As New TableCell
                tcell.Controls.Add(New LiteralControl(dr(dc.ColumnName).ToString))
                trow.Cells.Add(tcell)
            Next
            Table1.Rows.Add(trow)
        Next




this code is generating my table

<asp:Table ID="Table1" runat="server" CellPadding="5"
GridLines="horizontal" HorizontalAlign="Center">
   <asp:TableRow>
     <asp:TableCell>1</asp:TableCell>
     <asp:TableCell>2</asp:TableCell>
   </asp:TableRow>
   <asp:TableRow>
     <asp:TableCell>3</asp:TableCell>
     <asp:TableCell>4</asp:TableCell>
   </asp:TableRow>
</asp:Table>




I am having trouble with the asp:table attributes getting the table to populate how I would like.

I want the table to populate data in 3 columns for about 8 rows.
I have attached a screenshot of what the table currently appears like.

Do you have any suggestions?

Attached image(s)

  • Attached Image

This post has been edited by p00ndawg: 28 July 2011 - 04:39 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1