Hi micah,
Use below code.
Database
For this example I have used of Northwind database that you can download using the link given below.
Download Northwind Database
HTML
<asp:DataList ID="dlCustomers" runat="server" DataSourceID="SqlDataSource1" RepeatColumns = "3" CellSpacing = "3" RepeatLayout = "Table">
<ItemTemplate>
<table class = "table">
<tr>
<th colspan="2">
<b><%# Eval("ContactName") %></b>
</th>
</tr>
<tr>
<td colspan="2">
<%# Eval("City") %>,
<%# Eval("PostalCode") %><br />
<%# Eval("Country")%>
</td>
</tr>
<tr>
<td>
Phone:
</td>
<td>
<%# Eval("Phone")%>
</td>
</tr>
<tr>
<td>
Fax:
</td>
<td>
<%# Eval("Fax")%>
</td>
</tr>
</table>
</ItemTemplate>
</asp:DataList>
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
string conString = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
string query = "SELECT * FROM Customers";
using (SqlConnection con = new SqlConnection(conString))
{
SqlCommand cmd = new SqlCommand(query);
using (SqlDataAdapter sda = new SqlDataAdapter())
{
cmd.Connection = con;
sda.SelectCommand = cmd;
using (DataTable dt = new DataTable())
{
sda.Fill(dt);
ViewState["Customers"] = dt;
this.BindDatalist();
}
}
}
}
}
protected void BindDatalist()
{
dlCustomers.DataSource = ViewState["Customers"] as DataTable;
dlCustomers.DataBind();
}
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not Me.IsPostBack Then
Dim conString As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Dim query As String = "SELECT * FROM Customers"
Using con As SqlConnection = New SqlConnection(conString)
Dim cmd As SqlCommand = New SqlCommand(query)
Using sda As SqlDataAdapter = New SqlDataAdapter()
cmd.Connection = con
sda.SelectCommand = cmd
Using dt As DataTable = New DataTable()
sda.Fill(dt)
ViewState("Customers") = dt
Me.BindDatalist()
End Using
End Using
End Using
End If
End Sub
Protected Sub BindDatalist()
ddCustomers.DataSource = TryCast(ViewState("Customers"), DataTable)
dlCustomers.DataBind()
End Sub
Screenshot