Thursday, 1 May 2014

Fetching columns from table and inserting data to another column of same table

No comments
For selecting data
 
  public DataSet example1()
    {      
        con.Open();
        SqlCommand cmd;
        cmd = new SqlCommand("select * from [order]", con);
        DataSet dt = new DataSet();
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        da.Fill(dt);
        con.Close();
        return dt;
    }


For updating data
 
    public DataTable example2()
    {      
        con.Open();
        SqlCommand cmd;
        cmd = new SqlCommand("update [order] set sum=@sum where order_id=@order_id", con);
        cmd.Parameters.AddWithValue("@sum", sp);
        cmd.Parameters.AddWithValue("@order_id", sp1);
        DataTable dt = new DataTable();
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        da.Fill(dt);
        con.Close();
        return dt;
    }


 Adding using datatable

public void gbind()
    {
        DataSet ds = new DataSet();
        ds = example1();
        DataTable dt = new DataTable();
        dt = ds.Tables[0].Clone();
        for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
        {
            DataRow drow = dt.NewRow();
            drow["order_id"] = ds.Tables[0].Rows[i]["order_id"];
            drow["product_id"] = ds.Tables[0].Rows[i]["product_id"];
            drow["quantity"] = ds.Tables[0].Rows[i]["quantity"];
            drow["sum"] = Convert.ToString(Convert.ToUInt32(ds.Tables[0].Rows[i]["product_id"]) * Convert.ToUInt32(ds.Tables[0].Rows[i]["quantity"]));
            dt.Rows.Add(drow);
        }
        foreach (DataRow drr in dt.Rows)
        {
            sp1 = Convert.ToInt32(drr["order_id"]);
             sp = Convert.ToInt32(drr["sum"]);
             dt = example2();            
        }   
    }
  
    protected void Button1_Click(object sender, EventArgs e)
    {
        gbind();
    }