connectseekers.com

Thursday, 7 August 2014

Bind Data In DataList Using C#.Net and VB.Net in Asp.Net

8/07/2014 - By Pranav Singh 0

This article will show you the simplest way to bind the DataList control using sql server data base table with three columns in asp.net using c#.net.


So for this article first we will create a new asp.net application and add the below code into your asp.net page.

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="BindDataInListbox.aspx.cs" Inherits="ProjectDemo_Asp.et.BindDataInListbox" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Bind Data In DataList Using C#.Net and VB.Net in Asp.Net</title>
   
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:DataList ID="DataList1" runat="server" BorderColor="Maroon"
            BorderWidth="1px" CellPadding="4" CellSpacing="4" RepeatColumns="3">
            <ItemTemplate>
            <div style="text-align:center;border:1 solid black;" border="1">
              <img src="http://icons.iconarchive.com/icons/taytel/orb/128/bullet-icon.png" style="height: 75px; width: 81px" /><br />
              <asp:Label ID="Label1" runat="server" Text='<%# Eval("title") %>'></asp:Label>
              </div>
            </ItemTemplate>
        </asp:DataList>
    </div>
    </form>
</body>
</html>

Now add the below code into your .cs page. In above code I have add a datalist control in page and in item template of the datalist I have added an image and bind the one field value to DB table field which is a book title.

C#.Net
using System;
using System.Data;
using System.Data.SqlClient;

namespace ProjectDemo_Asp.et
{
    public partial class BindDataInListbox : System.Web.UI.Page
    {
        public string connectionstring = "";
        protected void Page_Load(object sender, EventArgs e)
        {
            DataTable _objdt = new DataTable();
            _objdt = GetDataFromDataBase();
            if (_objdt.Rows.Count > 0)
            {
               DataList1.DataSource = _objdt;
               DataList1.DataBind();
            }
        }

        ///
        /// Function for binding retribing the data from database
        /// In this i have used Access DB you can use SQL DB to bind the data
         ///
        public DataTable GetDataFromDataBase()
        {
            DataTable _objdt = new DataTable();
            string querystring = "select * from Books;";
            SqlConnection _objcon = new SqlConnection(connectionstring);
            SqlDataAdapter _objda = new SqlDataAdapter(querystring, _objcon);
            _objcon.Open();
            _objda.Fill(_objdt);
            return _objdt;
        }
    }
}

       

VB.Net
Imports System.Data
Imports System.Data.SqlClient

Namespace ProjectDemo_Asp.et
    Partial Public Class BindDataInListbox
        Inherits System.Web.UI.Page
        Public connectionstring As String = ""
        Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
            Dim _objdt As New DataTable()
            _objdt = GetDataFromDataBase()
            If _objdt.Rows.Count > 0 Then
                DataList1.DataSource = _objdt
                DataList1.DataBind()
            End If
        End Sub

        '''
        ''' Function for binding retribing the data from database
        ''' In this i have used Access DB you can use SQL DB to bind the data
        '''
        Public Function GetDataFromDataBase() As DataTable
            Dim _objdt As New DataTable()
            Dim querystring As String = "select * from Books;"
            Dim _objcon As New SqlConnection(connectionstring)
            Dim _objda As New SqlDataAdapter(querystring, _objcon)
            _objcon.Open()
            _objda.Fill(_objdt)
            Return _objdt
        End Function
    End Class
End Namespace

        

Now we have done run the application to check output.


About the Author

We are the group of people who are expertise in different Microsoft technology like Asp.Net,MVC,C#.Net,VB.Net,Windows Application,WPF,jQuery,Javascript,HTML. This blog is designed to share the knowledge.

Get Updates

Subscribe to our e-mail newsletter to receive updates.

Share This Post

0 comments:

Please let me know your view

Free Ebooks


About Us

We are the group of people who are expertise in different Microsoft technology like Asp.Net,MVC,C#.Net,VB.Net,Windows Application,WPF,jQuery,Javascript,HTML. This blog is designed to share the knowledge.

Contact Us

For writing article in this website please send request by your

GMAIL ID: dotnetpools@gmail.com
Email Id : contact@aspdotnet-pools.com

Bugs and Suggestions

As we all know that this website is for sharing knowledge and providing proper solution. So while reading the article is you find any bug or if you have any suggestion please mail us at contact@aspdotnet-pools.com.

Partners


Top jQuery Plugins

Global Classified : Connectseekers.com
© 2014
back to top