ASP.NET: How to display data in DataList from database using C# and Vb.Net?

Aman Sharma
3
Introduction: 
In this article I will explain how to display data in Data List Data control from SQL Server database. I will use SQL query to get data from database and bind data to DataList Control to display data.



Steps to display Data in DataList Control:
   1.      Create webpage in your application and add Data List control.
   2.      Now customize DataListItemTemplate to Bind Column Name as given in Table.
   3.      Write code in code behind file to bind DataList to database.
Demo:

click on image to enlarge view
Create table in database:
Create Table “Student_Info”  in Database “Blog” and Enter some data to display.
Column Name
DataType
Student_Id
Int
Student_name
Varchar(100)
Age
Int
Class
Varchar(100)

 Now design your web page:
Place DataList control on webpage. Customize it as given below:

<asp:DataList ID="DataList1" runat="server" BorderWidth="1"  RepeatColumns="3" CellPadding="4" ForeColor="#000" RepeatDirection="Horizontal">
            <AlternatingItemStyle BackColor="White" ForeColor="#284775" />
       <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />      
        <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
            <ItemStyle BackColor="#0969D0" ForeColor="#ffffff" />
        <ItemTemplate>
            <table  style="width:100%; border:dotted 1px; ">
                <tr>
                    <td>Student id</td>
                    <td><%# Eval("Student_id") %></td>
                </tr>
                <tr>
                    <td>Name</td>
                    <td><%# Eval("Student_Name") %></td>
                </tr>
                <tr>
                    <td>Age</td>
                    <td><%# Eval("Age") %></td>
                </tr>
                <tr>
                    <td>Class</td>
                    <td><%# Eval("Class") %></td>
                </tr>
                           
            </table>
        </ItemTemplate>
            <SelectedItemStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
        </asp:DataList>


Asp.NET Code: Now write following code to Bind DataList Data Control.
In C#:
Add Following Namespaces:

using  System.Data;
using  System.Data.SqlClient;
using  System.Configuration;


Write following code:
public partial class datalist : System.Web.UI.Page
{
    SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["con"].ConnectionString);
 
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            Bind_DataList();
        }
    }
    public void Bind_DataList()
    {
        if (con.State == ConnectionState.Closed)
        {
            con.Open();
        }

        SqlCommand cmd = new SqlCommand("select * from student_info", con);
        SqlDataAdapter adpData = new SqlDataAdapter(cmd);
        DataTable dt = new DataTable();
        adpData.Fill(dt);
        DataList1.DataSource = dt;
        DataList1.DataBind();
        con.Close();
    }
}

In Vb.Net:

Add Following Namespaces:

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

Write following code:

Partial Public Class datalist
    Inherits System.Web.UI.Page
    Private con As New SqlConnection(ConfigurationManager.ConnectionStrings("con").ConnectionString)

    Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
        If Not IsPostBack Then
            Bind_DataList()
        End If
    End Sub
    Public Sub Bind_DataList()
        If con.State = ConnectionState.Closed Then
            con.Open()
        End If

        Dim cmd As New SqlCommand("select * from student_info", con)
        Dim adpData As New SqlDataAdapter(cmd)
        Dim dt As New DataTable()
        adpData.Fill(dt)
        DataList1.DataSource = dt
        DataList1.DataBind()
        con.Close()
    End Sub
End Class

Post a Comment

3Comments
  1. See I am a beginner in Android mobile application.I have got a database .db. I want to display the same and some queries in mobile application. In order to do that whether I have to convert that database to XML and follow your steps

    ReplyDelete
  2. Not getting the code for insert, update,delete etc.will you please forward the neccessary code

    ReplyDelete
Post a Comment

#buttons=(Accept !) #days=(20)

Our website uses cookies to enhance your experience. Learn More
Accept !