Abraham Olatubosun

Abraham Olatubosun

  • NA
  • 471
  • 114.8k

I want to find column name in data table if exist do get the

May 7 2016 6:54 PM
I want to update this table "tbl_TempReport" base on the matching field name found it DataTable (dt), this is my code :
 
DataTable dt = GetDatafromDatabase(); //===== returns a DataTable
string SQLT = "SELECT * FROM tbl_TempReport";
string colname;
if (cn.State == ConnectionState.Closed)
{
cn.Open();
}
SqlCommand cmdT = new SqlCommand(SQLT, cn);
SqlDataReader rt = cmdT.ExecuteReader();
while (rt.Read())
{
// For each row, print the values of each column.
foreach (DataColumn column in dt.Columns)
{
foreach (DataRow row in dt.Rows)
{
colname = column.ColumnName.ToString();
if (colname == "ID")
{
// break;
rt.NextResult();
}
else
{
rt.Read();
//if (rt["code"].ToString() == colname)
//{
//insert into tbl_TempReport to match the appropriate column
string SQLP = "UPDATE tbl_TemReport SET VALUEP = @VALUEP WHERE code = @code";
SqlCommand cmdp = new SqlCommand(SQLP, cn);
cmdp.Parameters.AddWithValue("@VALUEP", SqlDbType.Int).Value = row[column].ToString();
cmdp.Parameters.AddWithValue("@code", SqlDbType.NVarChar).Value = rt["code"].ToString();
cmdp.ExecuteNonQuery();
//}
//ListBox1.Items.Add(row[column.ColumnName].ToString());
}
//======
}
}
}
 
 

i will appreciate your help as the is module in my project due to be submitted on Monday.

Thank you and God blessing

 

Answers (1)