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.
Some of my previous articles are
as follows: GridView
With Fixed Headers in Asp.Net Using C# and jQuery, Highlight
GridView Row on Mouseover Using CSS in Asp.Net C#.Net, Binding
Gridview By Access DataBase Using C#.Net in Asp.Net, GridView
Export to CSV in Asp.Net Using C#.Net and VB.Net,
Code
to Select All Checkbox in GridView in Asp.Net Using jQuery, GridView
Export to HTML in Asp.Net Using C#.Net and VB.Net, Drag
Drop Cells in GridView Control Using Asp.net C# and jQuery, GridView
- Delete Selected Row Record From DataBase On Button Click in Asp.net Using
C#.Net, GridView
Export to Word Document (.doc/.docx) 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.
0 comments:
Please let me know your view