Question

i have one table having 3 rows and i am trying to insert data into first two rows using data table but it is inserting data like this , instead of inserting in first two row it is inserting in other rows

                 price  qty  total
                --------------------
                  5     10    -

                  5     12    -

                  -      -    50

                  -      -    60

but i wants

                 price  qty  total
                --------------------
                  5     10    50

                  5     12    60

i have used following code for selecting data from table than inserting data into table,it is showing data correctly but just inserting in wrong rows

 int sp;
public DataTable bind1()
{
    SqlConnection con = new SqlConnection("cnnection");
    con.Open();
    SqlCommand cmd;
    cmd = new SqlCommand("select * from [order]", con);      
    DataTable dt = new DataTable();
    SqlDataAdapter da = new SqlDataAdapter(cmd);
    da.Fill(dt);
    con.Close();
    return dt;
}
public DataTable bind2()
{
    SqlConnection con = new SqlConnection("cnnection");
    con.Open();
    SqlCommand cmd;
    cmd = new SqlCommand("insert into [order](total) values(@total)", con);
    cmd.Parameters.AddWithValue("@total", sp);
    DataTable dt = new DataTable();
    SqlDataAdapter da = new SqlDataAdapter(cmd);
    da.Fill(dt);
    con.Close();
    return dt;
}    
 public void gbind()
    {
        DataTable dt = new DataTable();
        dt = bind1();
        DataTable dt1 = new DataTable();
        DataColumn dc = new DataColumn("total");
        dt1.Columns.Add(dc);        
        foreach(DataRow drow in dt.Rows)
           {
            int s1 = Convert.ToInt16(drow["price"]);
            int s2 = Convert.ToInt16(drow["qty"]);
            int s3 = s1 * s2;
            DataRow dr = dt1.NewRow();
            dr["total"] = s3;
            dt1.Rows.Add(dr);
            }           
         foreach (DataRow row in dt1.Rows)
            {
             string s1 = row["total"].ToString();
             for (int i = 0; i < dt.Rows.Count; i++)
              {
                sp = Convert.ToInt16(s1);
                dt = bind2();
              }
            }

i have tried like this also but still same problem

public void gbind()
    {
        DataTable dt = new DataTable();
        dt = bind1();       
        foreach (DataRow drow in dt.Rows)
        {
            int s1 = Convert.ToInt16(drow["price"]);
            int s2 = Convert.ToInt16(drow["qty"]);
            int s3 = s1 * s2;                    
            drow["total"] = s3;
            sp = s3;
            dt = bind2();            
        }
     }

No correct solution

OTHER TIPS

In your forEach loop in the gbind method, I don't think you should be adding a new row for "total", it would need to be a column. And dt1.Rows.Add(dr) should be dt1.Columns.Add(dr). Try rewriting your code to add a column instead. Let me know if that helps any.

Comment these two lines in your code. Remove the code to add extra rows. You just need to update the column value.

foreach(DataRow drow in dt.Rows)
           {
            int s1 = Convert.ToInt16(drow["price"]);
            int s2 = Convert.ToInt16(drow["qty"]);
            int s3 = s1 * s2;
            //DataRow dr = dt1.NewRow();
            dr["total"] = s3;
            //dt1.Rows.Add(dr);
            }         
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top