Thanks for the suggestions. Here is an update that would explain everything in more detail. I have a datatable with values, the first column will always be a string and the rest (5 columns) will have numbers. I have used the code that I shared to make it work for a datagridview
but now I want to make it work for a datatable
. The way I though was best was to take each row into the postgresql server and store it until its needed later. It seems like a sqladapter
seems to be a better way to handle this and therefore I had some help trying to figure this out with the following code:
using (NpgsqlDataAdapter adapter = new NpgsqlDataAdapter("SELECT num1, num2, num3, num4, num5, num6 FROM " + Table + ";", Conn))
{
NpgsqlCommand insert = new NpgsqlCommand("INSERT INTO " + Table + " VALUES (@num1, @num2, @num3, @num4, @num5, @num6);", Conn);
insert.Parameters.Add("@num1", NpgsqlDbType.Text, 200, "1");
insert.Parameters.Add("@num2", NpgsqlDbType.Text, 50, "2");
insert.Parameters.Add("@num3", NpgsqlDbType.Text, 50, "3");
insert.Parameters.Add("@num4", NpgsqlDbType.Text, 50, "4");
insert.Parameters.Add("@num5", NpgsqlDbType.Text, 50, "5");
insert.Parameters.Add("@num6", NpgsqlDbType.Text, 50, "6");
adapter.InsertCommand = insert;
adapter.MissingSchemaAction = MissingSchemaAction.AddWithKey;
DataTable table = new DataTable();
// Generate the DataTable schema.
adapter.FillSchema(table, SchemaType.Source);
// Add the new rows to the DataTable, e.g.
DataRow row = table.NewRow();
//Where I´m unsure what to do. This is obivously not the way to write the code but would appreciate it if I get some help to write this the right way
row\["1"\] = row.Table.Rows\[0\]\[0\].ToString(); //Does not continue after this line
row\["2"\] = row.Table.Rows\[0\]\[1\].ToString();
row\["3"\] = row.Table.Rows\[0\]\[2\].ToString();
row\["4"\] = row.Table.Rows\[0\]\[3\].ToString();
row\["5"\] = row.Table.Rows\[0\]\[4\].ToString();
row\["6"\] = row.Table.Rows\[0\]\[5\].ToString();
table.Rows.Add(row);
// Save the changes.
adapter.Update(table);
}
}
The code seems to work until the actual strings/values need to be put inside the SQL server table(see inside of code to know where the code breaks out). If anyone knows how to make the code work or is willing to show whats missing I would gladly appreciate it.