Author: Zunnair
Download Source Code : 907_asp.zip
In this simple article you will learn to working with asp.net datalist control
Create a web application in c#
Drag an asp.net datasource on the form and add new web.config file
Now write connection string in web.config file
<connectionStrings>
<add name="DataConnectionString" connectionString="data source= LAPTOP\SQLDEVELOPER;UID=sa;PWD=lahore;DATABASE=shoppingcart"/>
connectionStrings>
Then write code in default.aspx file in form tag
<div>
<asp:SqlDataSource ID="DSMobileCategory" runat="server" ConnectionString="<%$ ConnectionStrings:DataConnectionString %>"
OnSelecting="DSMobileCategory_Selecting" ProviderName="System.Data.SqlClient"
SelectCommand="SELECT [PID], [Name], [Description], [Model] FROM [SHOPProducts]">asp:SqlDataSource>
div>
Now write this code for showing data in datalist control in form tag
<asp:DataList ID="DataList1" runat="server" DataKeyField="PID" DataSourceID="DataSource1">
<ItemTemplate>
PID:
<asp:Label ID="PIDLabel" runat="server" Text='<%# Eval("PID") %>'>asp:Label><br />
Name:
<asp:Label ID="NameLabel" runat="server" Text='<%# Eval("Name") %>'>asp:Label><br />
Description:
<asp:Label ID="DescriptionLabel" runat="server" Text='<%# Eval("Description") %>'>
asp:Label><br />
Model:
<asp:Label ID="ModelLabel" runat="server" Text='<%# Eval("Model") %>'>asp:Label><br />
<br />
ItemTemplate>
asp:DataList>
Now write code on from load event
C#
protected void Page_Load(object sender, EventArgs e)
{
try
{
Label1.Text = “”;
}
catch (Exception ex)
{
Label1.Text = ex.Message;
}
}
VB
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
Try
Label1.Text = “”
Catch ex As Exception
Label1.Text = ex.Message
End Try
End Sub
This is simple code for asp.net datalist control.