Click here to Skip to main content
15,896,730 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i'm try to retrieve data using stored procedure and pass it to dropdownlist in presentation layer
but the problem is how to this using sqldatareader
Posted

C#
        public static getrecords()
{
            DataTable tbl = null;
            SqlConnection conn = null;
            try
            {
                conn = "Your Connection String";
                cmd.Connection = conn;
                tbl = new DataTable();
                SqlDataReader dr = cmd.ExecuteReader();
                tbl.Load(dr);

                dr.Close();
                dr.Dispose();
            }
            catch (Exception ex)
            {
                LogFile.WriteLog(ex.ToString());
                throw ex;
            }
            finally
            {
                if (conn != null)
                {
                    conn.Close();
                }
            }
            return tbl;
}


THis way you can use it.
 
Share this answer
 
Comments
Er. Puneet Goel 3-Apr-14 8:33am    
What is problem with this code?
youseph 3-Apr-14 12:45pm    
your code works fine
Er. Puneet Goel 3-Apr-14 23:58pm    
Actually you have down voted this by voting 2...that's y i asked you about any problem in code.
upto vote 3 : its -Ve Voting and 4 and 5 is +Ve Voting
 
Share this answer
 
v2
Comments
youseph 2-Apr-14 20:27pm    
is there any exemple of using sqldatareader in DataAccesslayer
Peter Leow 2-Apr-14 20:42pm    
Added in solution 1.
A SqlDataReader may not be the best choice for use in a DAL. The reason being that it is not like other objects that you can pass around. You can pass a datareader but you have to keep calling .Read() on it to get the next rows, it is not the whole set of data in one shot. DataSets or DataTables are better for DAL, in my opinion.

However, a DAL is whatever you want it to be. You are writing the code and are therefore in charge of what it looks like so make it work for you.
 
Share this answer
 
this is the code that i used, works good but can anyone take look & tell me is there any thing that i could change

code DAL.cs :

public DataTable villelist()
    {
        DataTable tbl = null;
        SqlConnection conn = Conection.GetDbConnection();
        SqlCommand cmd = new SqlCommand("BindVilleList", conn);
        cmd.CommandType = CommandType.StoredProcedure;
        conn.Open();
        tbl = new DataTable();
        SqlDataReader dr = cmd.ExecuteReader();
        tbl.Load(dr);
        dr.Close();
        dr.Dispose();
        return tbl;
    }


code BLL.cs :

SQL
public DataTable villelist()
    {
        return userDAL.villelist();
    }


code default.aspx :

<pre lang="cs">protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            //try
            //{
                //DataSet ds, ds1 = null;
                DataTable dtl = null;
                dtl = userBLL.villelist();
                ddlVillle.DataSource = dtl;
                ddlVillle.DataTextField = "Nom_ville";
                ddlVillle.DataValueField = "Id_vll";
                ddlVillle.DataBind();
              } 
        }
}
 
Share this answer
 

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