Click here to Skip to main content
15,921,793 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi everybody,
here i have a problem i'm using 3 tier architecture in my web application i have a method to display the record using interface, property class like below :
public List<EmpPrp> DispEmp()
{
    ConOpen();
    SqlCommand cmd = new SqlCommand("dispemp", con);
    cmd.CommandType = CommandType.StoredProcedure;
    SqlDataReader dr = cmd.ExecuteReader();
    List<EmpPrp> obj = new List<EmpPrp>();
    while (dr.Read())
    {
        EmpPrp k = new EmpPrp();
        k.EmpCod = Convert.ToInt32(dr[0]);
        k.EmpNam = dr[1].ToString();
        obj.Add(k);
    }
    dr.Close();
    cmd.Dispose();
    con.Close();
    return obj;
}


i am using list collection in business tier, now i want to bind my list control in Presentation Tier with the data base with coding i can do so by using Object DataSource but i want to do it with coding. Can you plz help me on this topic, I have no knowlede about it.

Thanks in advance.
Posted
Updated 12-May-11 2:28am
v5

1 solution

You can just use

C#
myList.DataSource = yourEmpList;
myList.DataTextField = "EmpNam";
myList.DataValueField = "EmpCode";
myList.DataBind();


in your Page_Load or similar.

Hope this helps
 
Share this answer
 
v2
Comments
Randeep Chauhan 12-May-11 8:57am    
Thanks for reply sir what is your meaning by yourEmpList and how to give DataTextField & DataValueField as i dont know the Database becuse it my conditon I have only .dll file in my application
Wayne Gaylard 12-May-11 9:12am    
yourEmpList is the List of EmpPrp that you are trying to bind to. I updated my answer to include how to set the DataText and DataValue field. This also assumes myList is your ListControl on your page.
Randeep Chauhan 13-May-11 3:56am    
Sir actualy i dont know the datatext field name or datavaluefield name i have only dll reference i have no information of database
Wayne Gaylard 13-May-11 4:39am    
Your DataTextField is the property of the class you want your user to see in the control, DataValueField is the Property of the class that you want to use as a reference to update the database. In your case your class only has two properties, EmpName and EmpCode. I suggest that you would want to your users to see the Employees name in the ListControl and that you would use the EmpCode property to bind to the data base.
BobJanova 12-May-11 9:45am    
Good answer, but note that this will only bind the UI list control with the in-memory list, not to the database. You need to bind to a DataSet to do that. However, for a web app I wouldn't recommend direct data binding to the database, and it violates the 3 tier system as well. Instead, if the OP actually needs that, the list should be an ObservableCollection (or ASP.net equivalent) and bind itself to the database.

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