0

I have this basic WinForms application user interface:

enter image description here

And I want to add the data both to the DataGridView and the sql table, when the "Gem" button is clicked. I have this following code:

private void Form2_Load(object sender, EventArgs e)
    {
        try
        {
            con = new SqlConnection();
            con.ConnectionString = @"Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\Produkt.mdf;Integrated Security=True";
            con.Open();
            //adap = new SqlDataAdapter("select SN, FName as 'Navn', MName as 'Vare nr', LName as 'Antal', Age from Produkt", con);
            string sql = "SELECT Navn, Varenr, Antal, Enhed, Priseksklmoms, Konto FROM ProduktTable";

            adap = new SqlDataAdapter(sql, con);
            ds = new System.Data.DataSet();
            adap.Fill(ds, "ProduktTable");
            dataGridView1.DataSource = ds.Tables["ProduktTable"];

        }

        catch (Exception ex)
        {
            MessageBox.Show("Error\n" + ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }

    }

private void button1_Click(object sender, EventArgs e)
    {
        string navn = textBox2.Text;
        int varenr = int.Parse(textBox3.Text);
        float antal = (float)Convert.ToDouble(textBox4.Text);
        string enhed = textBox5.Text;
        string konto = comboBox2.Text;
        float pris = (float)Convert.ToDouble(textBox6.Text);

        dataGridView1.Rows[0].Cells[0].Value = navn;
        dataGridView1.Rows[0].Cells[1].Value = varenr;

        string StrQuery;

        try
        {

            SqlCommand comm = new SqlCommand();
            comm.Connection = con;

            for (int i = 0; i < dataGridView1.Rows.Count; i++)
            {
                StrQuery = @"INSERT INTO tableName ProduktTable ("
                + dataGridView1.Rows[i].Cells["Varenr"].Value + ", "
                + dataGridView1.Rows[i].Cells["Antal"].Value + ");";
                comm.CommandText = StrQuery;
                comm.ExecuteNonQuery();                    
            }

        }
        catch (Exception ex)
        {
            MessageBox.Show("Error\n" + ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }

This is just an example with the purpose for storing the string "navn" and the integer "Varenr" in the DataGridView and the sql. When Im running the application and clicking on the button, following error occurs:

enter image description here

What's wrong with the procedure ?.

Thanks in advance

2
  • 2
    INSERT INTO tableName ProduktTable Why is the word tableName there? The parser thinks that's your table name, and therefore ProduktTable is a syntax error. Commented Jun 5, 2014 at 20:28
  • you dont have to mention tableName in insert statement Commented Jun 5, 2014 at 20:30

1 Answer 1

1

The format for an insert name doesn't require the words tableName. It wants the actual table name.

INSERT INTO tableName ProduktTable

should be

INSERT INTO ProduktTable

assuming Produ**K**tTable isn't a typo.

Sign up to request clarification or add additional context in comments.

1 Comment

@marc_s Okay. I wasn't sure if it was a typo or not. The problem should just be teh tableName then.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.