'append date column to datatable and add days based on another column
I need to append a column to datatable in c# , like the folowing , (endofregistration) is the column that should be added , it should be datetime.now.adddays(value from another column example [5]) . my code is able to perform right only for the first row when in debug mode , then all the values of the appended column are taking the values of the last value[4]. example :
endofregistreation | id | price | orderdate |days
28/02/2022 1 50 18/02/2022 10 ----> this is the correct result
endofregistreation | id | price | orderdate |days
18/02/2023 1 50 18/02/2022 365 ----> this is the correct result
after running my code, all the rows values will be calculated based on the last value read from days column which is not correct...
below is my code:
using (SqlCommand cmdbindertables = new SqlCommand("SELECT from the table", con))
{
cmdbindertables.CommandType = CommandType.Text;
using (SqlDataAdapter sda = new SqlDataAdapter(cmdbindertables))
{
sda.Fill(dtperiodlenght);
}
DateTime invDate = DateTime.Now;
foreach (DataRow row in dtperiodlenght.Rows)
{
need to set value to New SADERLEX STORE Columns
row["CustomerID"] = Session["uid"].ToString();
row["OrderDate"] = invDate.ToString("dd/MM/yyyy");
}
for (int x = 0; x < dtperiodlenght.Rows.Count; x++)
for (int j = 0; j < dtperiodlenght.Columns.Count; j++)
{
object o = dtperiodlenght.Rows[x].ItemArray[j];
if you want to get the string
string s = Convert.ToString(o = dtperiodlenght.Rows[x].ItemArray[j]);
string K = Convert.ToString(o = dtperiodlenght.Rows[x].ItemArray[9]);
thedaystocount.Text = K;
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
