set connection string in web.config
<connectionStrings>
<add name="demoConnectionString" connectionString="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\demo.mdb"
providerName="System.Data.OleDb" />
</connectionStrings>
import package in top of the cs file like this
using System.Data.OleDb;
in code file write connection string like this
private string dbConnectionString = ConfigurationManager.ConnectionStrings["demoConnectionString"].ConnectionString;
write a method for retrive data from database
public DataSet showProductDetails(int pid)
{
string sqlQry = "Select * from Products where ProductID=" + pid + "";
OleDbConnection dataConnection = new OleDbConnection(dbConnectionString);
OleDbCommand command = new OleDbCommand(sqlQry, dataConnection);
dataConnection.Open();
command.CommandType = CommandType.Text;
OleDbDataAdapter dataAdapter = new OleDbDataAdapter(command);
DataSet dsProductDetails = new DataSet();
dataAdapter.Fill(dsProductDetails);
return dsProductDetails;
}
finally set the datasource of datalist in pageload event or if you want to load in button event try this
int category = Convert.ToInt16(Request.QueryString["cid"]);
dsProductList = productList.ShowProductByCategoryID(category);
dtProductList = dsProductList.Tables[0];
DataList1.DataSource = dtProductList;
DataList1.DataBind();
for (int i = 0; i < dtProductList.Rows.Count; i++)
{
ImageButton imgButton =(ImageButton) DataList1.Items[i].FindControl("ImageButton1");
Label lblName =(Label) DataList1.Items[i].FindControl("lblItemName");
Label lblPrice =(Label) DataList1.Items[i].FindControl("lblPrice");
imgButton.ImageUrl = string.Format("~/Images/" + dtProductList.Rows[i]["ImageUrl"].ToString() + "");
imgButton.PostBackUrl = string.Format("~/UI/ProductDetails.aspx?ProductID={0}", dtProductList.Rows[i]["ProductID"].ToString());
lblName.Text = dtProductList.Rows[i]["Name"].ToString();
lblPrice.Text = dtProductList.Rows[i]["Price"].ToString();
}
its a sample example of view product details in datalist hope you will get idea that how to display data in data list. also i suggest you to search google for better solution. if you have any qry or information let me know.
Thanks