I have a sql database called Facturen (dutch for invoice) in the database there is a databasetable called Facturen (dutch for invoices).
Also I have a C# form (winforms) with a datagridwiew
some textboxes
and some labels
.
Also I have a Button
and on the click event the following code.
using (SqlConnection conn = new SqlConnection(@"Data Source=MACTARIS\MSSQL2014;Initial Catalog=Factuur;Persist Security Info=True;User ID=sa;Password=*********")) ;
{
conn.Open();
foreach (DataGridViewRow row in dataGrid.Rows)
{
if (!row.IsNewRow)
{
using (SqlCommand cmd = new SqlCommand("INSERT INTO Facturen (Klant, Adres, Postcode, Plaats, BTWnummer, Datum, Betalingswijze, BTWpercentage, Artikel, Aantal, Eenheidsprijs, TotaalexBTW, Totaalbedrag, Totaal_ex_BTW, BTW, Tebetalen) VALUES(@Klant,@Adres,@Postcode,@Plaats, @BTWnummer, @Datum, @Betalingswijze, @BTWpercentage, @Artikel, @Aantal, @Eenheidsprijs, @TotaalexBTW, @Korting, @Totaalbedrag, @Totaal_ex_BTW, @BTW, @Tebetalen)", conn))
{
cmd.Parameters.AddWithValue("@Klant", lblKlant.Text);
cmd.Parameters.AddWithValue("@Adres", lblAdres.Text);
cmd.Parameters.AddWithValue("@Postcode", lblPostcode.Text);
cmd.Parameters.AddWithValue("@Plaats", lblPlaats.Text);
cmd.Parameters.AddWithValue("@BTWnummer", lblBTWnummer.Text);
cmd.Parameters.AddWithValue("@Datum", txtDatum.Text);
cmd.Parameters.AddWithValue("@Betalingswijze", cboBetalingswijze.Text);
cmd.Parameters.AddWithValue("@BTWpercentage", cboPercentage.Text);
cmd.Parameters.AddWithValue("@Artikel", row.Cells[0].Value);
cmd.Parameters.AddWithValue("@Aantal", row.Cells[1].Value);
cmd.Parameters.AddWithValue("@Eenheidsprijs", row.Cells[2].Value);
cmd.Parameters.AddWithValue("@TotaalexBTW", row.Cells[3].Value);
cmd.Parameters.AddWithValue("@BTW", row.Cells[4].Value);
cmd.Parameters.AddWithValue("@Korting", row.Cells[5].Value);
cmd.Parameters.AddWithValue("@Totaalbedrag", row.Cells[6].Value);
cmd.Parameters.AddWithValue("@Totaal_ex_BTW", txtTotaalexBTW.Text);
cmd.Parameters.AddWithValue("@Tebetalen", txtTebetalen.Text);
cmd.ExecuteNonQuery();
}
}
}
}
conn.Close();
}
Now when I click the Button
to save to the database I get the following error Additional information: Invalid object name 'Facturen'.
I'm 100% sure that the databasetable is called Facturen and I'm 100% sure that there is a database.