Populate A DataList Control of asp.net From Database

Thursday, 3 April 2014

Populate A DataList Control of asp.net From Database

Populate A DataList  Control of asp.net From Database

Step : First we have to take a datalist control from vs2012 ,vs2010,vs2008 Toolbox .Then click on the right tab of the control ,one small window will open Name :Datalist Tasks .Click on Edit Template Pane and add a html table and make a custom design of it .in my project i have taken two label to populate from database .now my aspx coding

Namespace Needed :

Imports System.Data
Imports System.Data.SqlClient
Imports System.Configuration

.aspx Page :

<asp:DataList ID="DataLst" runat="server" Width="99%" >
                    <AlternatingItemStyle BackColor="#EEEEEE" />
                    <ItemTemplate>
                        <table id="table2" align="left" width="100%">
                            <tr>
                                <td align="left" colspan="2" width="90%">
                                    <asp:Label ID="Lbl_heading" runat="server" Text='<%# Eval("database_field_name")%>' Font-Bold="False" ForeColor="#0066FF" Font-Size="Small"></asp:Label>
                                </td>
                            </tr>
                            <tr>
                                <td align="left" colspan="2">
                                    <asp:Label ID="Sub_head" runat="server" Text='<%# Eval("database_field_name")%>'></asp:Label>
                                </td>
                            </tr>
                          
                          
                             <tr>
                                <td align="left" colspan="2">
                                    &nbsp;&nbsp; &nbsp;</td>
                            </tr>
                        </table>
                    </ItemTemplate>
                </asp:DataList>

.aspx.vb Page :

        Dim da As SqlDataAdapter
        Dim ds As New DataSet
        Dim sqlConn As SqlConnection = New    SqlConnection(ConfigurationManager.ConnectionStrings("Connection name").ConnectionString)
      
 Dim sql As String = " Sql Query to get data from database "
da = New SqlDataAdapter(sql, sqlConn)
        da.Fill(ds)

                     DataLst.DataSource = ds
            DataLst.DataBind()