Sunday, February 23, 2014

Logic error using foreach data column and data rows in datatable to insert into sql server

Wait a minute, I'm confused ... you still need to loop through the DataColumns, yet you marked the post from @Ashish as the answer when that code is not looping through any columns!?!?!



My code was incorrect also ... everything should have been put inside the inner loop through the DataColumns (but I think you got that figured out now anyway):




foreach (DataRow dr in dt.Rows) {
foreach (DataColumn dc in dt.Columns) {
if (dc.ColumnName.ToString() == "Oracle" || dc.ColumnName.ToString() == "F1") {
} else {
insertStatement = "INSERT INTO Allocation (OracleCostCenter, OracleGL, Allocation, FiscalYear, Addedby) VALUES ('";
insertStatement += dr["Oracle"].ToString().Trim() + "','" + dc.ColumnName.ToString() + "','" + dr[dc.ColumnName.ToString()];
insertStatement += "'," + DropDownList1.SelectedValue + ",'" + addedby1.Text + "')\n";
SqlCommand sqlCommand = new SqlCommand(insertStatement, conn);
sqlCommand.ExecuteNonQuery();
}
}
}







~~Bonnie DeWitt [C# MVP]


http://ift.tt/1bPbIRj


No comments:

Post a Comment