Click here to Skip to main content
16,004,969 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Dear Sir
i want to populate datalist with the coding how can i do
and my database in ms-access
i want to show only 3 top record from the specific column can you help me
my database table have to column Id,cat_type and table name is catagory
i want to show top 3 record from cat_type column
how can i do pls help
Posted
Comments
R. Giskard Reventlov 27-Jun-11 10:22am    
Give me million dollars.

Have you even bothered to try and work this out for yourself???
OriginalGriff 27-Jun-11 11:11am    
Man! You are one expensive coder! :laugh:
For that kind of money, I want the source code delivered, engraved on diamonds, by Seven of Nine!
R. Giskard Reventlov 27-Jun-11 11:24am    
:-)

I'll get it right in a minute!

1 solution

set connection string in web.config
XML
<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
 
Share this answer
 
Comments
um_dw 27-Jun-11 21:37pm    
dear Sir
thanks for your reply this code is enough for me
thanks again

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900