user2132038
user2132038

Reputation: 45

Data entered in a form doesn't get insert in the table

I cannot see the added data in the data table this is the code:

I'm using Visual Studio 2010 Express.

private void button1_Click(object sender, EventArgs e)
{
    string t1 = textBox1.Text;

    SqlCeConnection conn =
       new SqlCeConnection(@"Data Source=|DataDirectory|\Database1.sdf");

    conn.Open();

    SqlCeCommand cmdInsert = conn.CreateCommand();
    cmdInsert.CommandText = "INSERT TO table_name (Column1) VALUES (t1)";

    cmdInsert.ExecuteNonQuery();

    conn.Close();
}

It doesn't insert into data table after clicking on the button, it gives me an error on

cmdInsert.ExecuteNonQuery();

it debugs it, but when I click on the button, it shows me an error saying

SqlCeException was unhandled. There was an error parsing the query. [ Token line number = 1,Token line offset = 8,Token in error = TO ]

Upvotes: 2

Views: 162

Answers (5)

kashif
kashif

Reputation: 3834

string t1 = textBox1.Text;
            SqlCeConnection conn = new SqlCeConnection(@"Data Source=|DataDirectory|\Database1.sdf");
            conn.Open();
            SqlCeCommand cmdInsert = conn.CreateCommand();
            cmdInsert.CommandText = "INSERT into table_name (Column1) VALUES ('" + t1 + "')";
            cmdInsert.ExecuteNonQuery();
            conn.Close();

Upvotes: 1

Miroslav Bajtoš
Miroslav Bajtoš

Reputation: 10785

There are two problems with your code:

  • Syntax error in SQL statement - you should write INSERT INTO instead of INSERT TO.
  • You cannot use t1 directly in the SQL string. Although you could concatenate strings as suggested in other comments, it's better to use parametrized command instead.

Here is the corrected version:

SqlCeCommand cmdInsert = conn.CreateCommand();
cmdInsert.CommandText = "INSERT INTO table_name (Column1) VALUES (@t1)";
cmdInsert.Parameters.AddWithValue("@t1", t1);
cmdInsert.ExecuteNonQuery();

See Why do we need SqlCeCommand.Parameters.AddWithValue() to Insert a value? for more details on command parameters.

Upvotes: 3

Shane Andrade
Shane Andrade

Reputation: 2675

You need to pass the value of t1, probably with a parameter.

private void button1_Click(object sender, EventArgs e)
{
    string t1 = textBox1.Text;
    SqlCeConnection conn =
       new SqlCeConnection(@"Data Source=|DataDirectory|\Database1.sdf");
    conn.Open();
    SqlCeCommand cmdInsert = conn.CreateCommand();
    cmdInsert.CommandText = "INSERT INTO table_name (Column1) VALUES (@t1)";
    var parameter = cmdInsert.CreateParameter();
    parameter.Value = t1;
    parameter.ParameterName = "@t1";

    cmdInsert.Parameters.Add(parameter);

    cmdInsert.ExecuteNonQuery();
    conn.Close();
}

Upvotes: 0

Andomar
Andomar

Reputation: 238048

Try:

cmdInsert.Parameters.AddWithValue("@t1", textBox1.Text);
cmdInsert.CommandText = "insert INTO table_name (Column1) VALUES (@t1)";

Upvotes: 7

Davor Zlotrg
Davor Zlotrg

Reputation: 6050

Your sql query is wrong.

Instead of

cmdInsert.CommandText = "INSERT TO table_name (Column1) VALUES (t1)";

There should be

cmdInsert.CommandText = "INSERT INTO table_name (Column1) VALUES (t1)";

Upvotes: 1

Related Questions