Friday 4 June 2021

FAQ--(ADO .Net)

BACK

 FAQ Lists:

1) ExecuteReader , ExecuteNonQuery, ExecuteScalar.

2) (DataAdapter--DataSet) and  (DataReader).

3) (DataTable) and (DataSet).

 

 

 1. A) DataAdapter:  Use


 protected void btn_adapter_Click(object sender, EventArgs e)
    {
        //Observation: SQL Data Adapter
        //1. Opne and Close of connection not required.

        SqlConnection con = new SqlConnection(WebConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);

        //SqlDataAdapter adp = new SqlDataAdapter("select * from i_ce_rl order by invoice_no", con);
        SqlDataAdapter adp = new SqlDataAdapter("SELECT name,marks FROM test1 where marks = (select min(marks) from test1) ", con);

        DataTable dt = new DataTable();
        adp.Fill(dt);

        ////Session["SID"] = dt;
        ////DataTable dttt = (DataTable)Session["SID"];  // It is possible
        ////lbl_inv1.Text = (dt.Rows[0]["date"]).ToString(); // Not required con.Open(); and con.close();

        gv1.DataSource = dt;
        gv1.DataBind();
    }

_______

1.B) Command cmd Use :

 protected void btn_cmd_Click(object sender, EventArgs e)
    {
        //Observation: SqlCommand
        //1. Opne and Close connection needs. Mandatory

        SqlConnection con = new SqlConnection(WebConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);

        con.Open();
        SqlCommand cmd = new SqlCommand("select * from i_ce_rl order by invoice_no", con);
        SqlDataReader dr = cmd.ExecuteReader();
        DataTable dt = new DataTable();

        if(dr.Read())
        {
            lbl_inv2.Text = dr["date"].ToString();
        }
        gv2.DataSource = dr;
        gv2.DataBind();

        con.Close();
    } 

 

1.C) Command cmd and DataAdapter Both Use :

protected void btn_cmd_adp_Click(object sender, EventArgs e)
    {
        SqlConnection con = new SqlConnection(WebConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);

        SqlCommand cmd = new SqlCommand("select * from i_ce_rl order by invoice_no", con);
        SqlDataAdapter adp = new SqlDataAdapter(cmd);
        DataTable dt = new DataTable();

        adp.Fill(dt);
        gv3.DataSource = dt;
        gv3.DataBind();

        //Now By using SqlDataAdapter assign the date column to the Label
        lbl_inv3.Text = Convert.ToString(dt.Rows[0]["date"]);

        //Now By using SqlDataReader assign the date column to the Label. Connection Open and Close required.
        con.Open();
        SqlDataReader dr = cmd.ExecuteReader();
        if(dr.Read())
        {
            lbl_inv4.Text = dr["date"].ToString();
        }
        con.Close();

    }

 

2. DataTable And DataSet Use:

protected void btn_dt_ds_Click(object sender, EventArgs e)
    {
        //Observation : (DataTable And DataSet )
        //1. DataTable can work with one table only But DataSet can work with multiple Tables, it is a collection of Tables of DB.
        //2. DataTable fetches only one TableRow at a time whereas DataSet can fetch multiple TableRows at a time.
        //3. In DataTable,  DataSource cannot be  serialized. But DataSet is serialized DataSource .That is why web services can always returns DataSet as the result but not the DataTables.
        //4. Dataset is a collection of tables, which is used in disconnected architecture. Generally to fill DataSet we use fill method of SqlDataAdapter. It can be used for manipulating the data remotely and finally updating the database with the modified data.
        // whereas DataTable is the collection of rows and columns in a table, which used in Connection architecture always needs a con.Open() and con.Close(); to communicates with the DataSource(DB).



        SqlConnection con = new SqlConnection(WebConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);

        SqlDataAdapter adp = new SqlDataAdapter("select * from i_ce_rl order by invoice_no", con);
        //using DataTable Below:
        DataTable dt = new DataTable();
        adp.Fill(dt);
        gv4.DataSource = dt;
        gv4.DataBind();

        //using DataSet Below:
        //DataSet ds = new DataSet();
        //adp.Fill(ds);
        //gv4.DataSource = ds.Tables[0];
        //gv4.DataBind();
    }

 

HOME BACK

No comments:

Post a Comment

FAQ--(3- Tier Project)

BACK   https://meeraacademy.com/3-tier-architecture-example-in-asp-net-c/ 1. Presentation Layer (UI – User Interface Layer) 2. Busine...