Click here to Skip to main content
16,016,623 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am trying to write a code so that if the orderID matches a orderID in the database, the software selects that set of data and changes the 'status' of that line of data to 'in process'. Its a bit confusing but can anyone help? I'm new to C'#


if (result == DialogResult.Yes)
{
    OleDbDataAdapter da = new OleDbDataAdapter("Select * from [Customer Orders] Where [Order ID] = '" + PrdtID.Text + "'", MAcon);
    DataTable dtbl = new DataTable();
    da.Fill(dtbl);

    if (dtbl.Rows.Count == 1)
    {
        OleDbCommand cmd = new OleDbCommand("INSERT * into [Customer Orders]  ([Order Status])  VALUES (@[Order Status])", MAcon);
        cmd.Parameters.AddWithValue("@[Order Status]", Location.Text);

    MAcon.Open();
    cmd.ExecuteNonQuery();
    MAcon.Close();

    }


What I have tried:

if (result == DialogResult.Yes)
{
    OleDbDataAdapter da = new OleDbDataAdapter("Select * from [Customer Orders] Where [Order ID] = '" + PrdtID.Text + "'", MAcon);
    DataTable dtbl = new DataTable();
    da.Fill(dtbl);

    if (dtbl.Rows.Count == 1)
    {
        OleDbCommand cmd = new OleDbCommand("INSERT * into [Customer Orders]  ([Order Status])  VALUES (@[Order Status])", MAcon);
        cmd.Parameters.AddWithValue("@[Order Status]", Location.Text);

    MAcon.Open();
    cmd.ExecuteNonQuery();
    MAcon.Close();

    }
Posted
Updated 11-Apr-18 9:59am

1 solution

One problem you have is that you're concatenating the data from the textbox to the SQL query. This leaves you open to SQL injections and conversion problems. For more information, see SQL injection - Wikipedia[^]

You have used parameters in your INSERT statement so you should do the same with the select.

Another problem is that INSERT syntax does not contain asterisk. So it's simply
SQL
INSERT INTO TableName (column, column, ...) VALUES (value, value, ...).

For further examples, have a look at Properly executing database operations[^]
Even though the examples are using SqlClient, the idea is the same with OleDb... classes.
 
Share this answer
 
v4
Comments
Member 13765884 11-Apr-18 16:31pm    
when you say ' Another problem is that INSERT syntax does not contain asterisk. So it's simply' do you mean it does have an asterisk? because there is one.
Wendelius 11-Apr-18 17:01pm    
If you compare the INSERT statement in your code and the example I wrote, you'll notice the difference.

In other words while you have

...new OleDbCommand("INSERT * into [...

It should be

...new OleDbCommand("INSERT into [...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900