Click here to Skip to main content
15,893,588 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
kindly tell me in detail about the data set and data adapter and data fill method...
i know the concept of the data set and data adapter and data fill method but still its getting confused.
can u tell me how it works with simple code and explanation.
Posted

SqlConnection con = new SqlConnection(@"");
SqlCommand cmd = new SqlCommand();
SqlDataAdapter da = new SqlDataAdapter();
DataTable dt = new DataTable();


public void dataBind()
{
    con.Open();
    cmd.Connection = con;
    cmd.CommandText = "select * from Emp";
    da = new SqlDataAdapter(cmd);
    da.Fill(dt);

    GridView1.DataSource = dt;
    GridView1.DataBind();

    con.Close();
}
 
Share this answer
 
C#
public class CommClass
{
    SqlDataAdapter Da;
    DataSet Ds;
    DataTable Dt;
    SqlCommand Cmd;
    SqlConnection Cn;
    SqlConnection CnW;
    SqlDataReader Dr;
    string OnOff;

    public CommClass()
    {
        try
        {
                      Cn = new SqlConnection(ConfigurationManager.ConnectionStrings["Cn"].ConnectionString);
            Cn.Open();
                       OnOff = "On";
        }
        catch (Exception ex)
        {
            //MsgBox(ex.Message)
        }
    }

 public DataSet GetDs(string Crit)
    {
        DataSet ObjDs = new DataSet();
        try
        {
            Cmd = new SqlCommand(Crit, Cn);
            Cmd.CommandTimeout = 0;
            //Cmd.ExecuteNonQuery()
            Da = new SqlDataAdapter(Cmd);
            Da.Fill(ObjDs);
        }
        catch (Exception ex)
        {
            //MsgBox(ex.Message)
        }
        return ObjDs;
    }

this Code is in common class in App_Code..
First of all declare Your all variables in common class
then in constructor make connection(with connctionstring which is in web.config
then in get ds method
(This is what You want to learn)
create new dataset,new command in which You can pass query,connection
than new dataadapter andin that pass command(which have query and connection already!!!)
so You can Fill ds
and return ds
in You aspx.cs
page You have To just write
CommClass objCls=new CommClass();
ds=objCls.GetDs("Select * from EmployeeMst");

Enjoy...
HAPPY CoDING:)
 
Share this answer
 
v3

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