0

This is mycode please help me

private void button1_Click(object sender, EventArgs e)
    {
        OleDbConnection con = new OleDbConnection("Provider = Microsoft.Jet.OLEDB.4.0; Data Source=|DataDirectory|members list.mdb");
        OleDbCommand cmdoledb = new OleDbCommand();
        con.Open();
        OleDbCommand cmd = new OleDbCommand("INSERT INTO Members(name,family,age,Juncture scholarships) values('" + t1.Text + "','" + t2.Text + "','" + t3.Text + "','" + t4.Text + "')",con);
        cmd.ExecuteNonQuery();
        show();
        t1.Text = string.Empty;
        t2.Text = string.Empty;
        t3.Text = string.Empty;
        t4.Text = string.Empty;

    }
    private void show()
    {
        string d;
        OleDbConnection con;
        OleDbDataAdapter adap;
        DataSet ds = new DataSet();
        con = new OleDbConnection("Provider = Microsoft.Jet.OLEDB.4.0; Data Source=|DataDirectory|members list.mdb");
        d = "select * from members";
        adap = new OleDbDataAdapter(d, con);
        adap.Fill(ds, "members");
        dataGridView1.DataSource = ds.Tables["members"];
    }

When I use this code I get this error

An unhandled exception of type 'System.Data.OleDb.OleDbException' occurred in System.Data.dll

Additional information: Syntax error in INSERT INTO statement.

1
  • 3
    Note: Your query is wide open to SQL injection, so you don't even really control the syntax of what you're executing. Query parameters will be very helpful here. Commented Mar 6, 2017 at 14:46

1 Answer 1

2

2 things:

Use Parameters

When field name contains spaces use brackets

  OleDbCommand cmd = new OleDbCommand("INSERT INTO Members(name,family,age,[Juncture scholarships]) values(@name,@family,@age,@Js)",con);
    cmd.Parameters.AddWithValue("@name",t1.Text);
    cmd.Parameters.AddWithValue("@family",t2.Text);
    ...
    cmd.ExecuteNonQuery();
     ...
Sign up to request clarification or add additional context in comments.

Comments

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.