In my windows form application I have some datagridview
that fills using calculations that the program do.
now I want to design a report based on datagridviews
data
by searching I figured it out that to do so I have to put my data into a database
table and then get them from database
to put them into a dataset
in my C# program so I can access that data to design a report.
but this way has it's own problem such as my datagridviews
have different type of data like string or int and problem is how to put them into a table
for example I convert a datagrid to a datatable like this:
private DataTable GetDTFromDGV(DataGridView dgv)
{
var dt = new DataTable();
foreach (DataGridViewColumn column in dgv.Columns)
{
if (column.Visible)
{
dt.Columns.Add();
if (column.Name != "")
{
dt.Columns[column.Index].ColumnName = column.Name;
}
}
}
object[] CellValue = new object[dgv.Columns.Count];
foreach (DataGridViewRow row in dgv.Rows)
{
for (int i = 0; i < row.Cells.Count; i++)
{
CellValue[i] = row.Cells[i].Value;
}
dt.Rows.Add(CellValue);
}
return dt;
}
and this is my datatable:
now I want to insert it into a database like this and I get syntax error:
Syntax error in INSERT INTO statement.
using (OleDbCommand Cmd = new OleDbCommand())
{
Cmd.Connection = Connection;
Cmd.CommandText = "Delete * From rAnimalPerformance";
Cmd.ExecuteNonQuery();
foreach (DataRow Dr in cAnimalPerformancedt.Rows)
{
Cmd.CommandText = @"Insert Into rAnimalPerformance (Val, Desc)
Values ("
+ "'" + Dr["Val"] + "', '"
+ Dr["Desc"] + "')";
Cmd.ExecuteNonQuery();
}
}
this the only way? for situation like this what is the best way to design a report