Click here to Skip to main content
15,903,033 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
how i can convert data reader to the data set

C#
public void GetData( )
        {
            SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["Mob"].ConnectionString);
            SqlCommand cmd = new SqlCommand("GetData", con);
            DataSet ds = new DataSet();
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Connection = con;
            con.Open();
            SqlDataReader dr = cmd.ExecuteReader();
       

            con.Close();
       
    }
Posted
Updated 30-Aug-12 2:54am
v2
Comments
Sandeep Mewara 30-Aug-12 8:59am    
Why convert a datareader to dataset? Both are totally different.
Christian Amado 30-Aug-12 9:09am    
Please mark one of the three solutions as answer. =)

Hi,
No need to use SqlDataReader here. Use SqlDataAdapte.
Try this:
C#
public void GetData( )
{
    DataSet ds = new DataSet();
    SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["Mob"].ConnectionString);
    con.Open();
    SqlCommand cmd = new SqlCommand("GetData", con);
    cmd.CommandType = CommandType.StoredProcedure;
    SqlDataAdapter da = new SqlDataAdapte(cmd); 
    //Filling the records in dataset
    da.Fill(ds); 
    con.Close();       
}



--Amit
 
Share this answer
 
Comments
Christian Amado 30-Aug-12 8:59am    
5'ed :)
_Amy 30-Aug-12 9:00am    
Thanks Christian. :)
Christian Amado 30-Aug-12 9:02am    
I called your solution: "Optimized" :) You're welcome!
Manas Bhardwaj 30-Aug-12 9:15am    
+5!
_Amy 30-Aug-12 9:17am    
Thanks Manas. :)
C#
public DataSet GetData( )
        {
DataSet ds = new DataSet();
            SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["Mob"].ConnectionString);
            SqlCommand cmd = new SqlCommand("GetData", con);
            DataSet ds = new DataSet();
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Connection = con;

            con.Open();
SqlDataAdapter da = new SqlDataAdapter(cmd); //You don't need a DataReader, fill the dataset instead.
da.Fill(ds);   
            con.Close();
       return ds;
    }


Hope it helps.
 
Share this answer
 
v2
Comments
Manas Bhardwaj 30-Aug-12 8:58am    
Good +5!
_Amy 30-Aug-12 9:05am    
My +5! :)
 
Share this answer
 
Comments
Christian Amado 30-Aug-12 8:59am    
5'ed :)
Manas Bhardwaj 30-Aug-12 9:14am    
thx
_Amy 30-Aug-12 8:59am    
Nice links. +5! :)
Manas Bhardwaj 30-Aug-12 9:15am    
thx

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