Click here to Skip to main content
15,903,033 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello guys, I have problem with two querys Update and Insert using Inner Join.

I have three tables relation ship.

My application is C# Windows Forms connected with Access Database.

The follow fields are type Int:

Atados

Reorden

Pc_Atado

Vol


This is my query:

private void btnAct_Click(object sender, EventArgs e)
        {
            String sql = "UPDATE tLEADS INNER JOIN (tBUNDLES INNER JOIN tLOCALES ON tBUNDLES.IdBUNDLES = tLOCALES.IdLOCALES) ON tLEADS.IdLEADS = tBUNDLES.IdBUNDLES SET tLEADS Mnbr='" + txtMaq.Text + "', Rack=" + txtRack.Text + ", Nivel='" + txtNiv.Text + "', Localizacion='" + txtLoc.Text + "', Atados=" + txtAta.Text + ", Reorden=" + txtReor.Text + ", Pcs_Atado=" + txtPcs.Text + ", Vol=" + txtVol.Text + ", WHERE IdLEADS = '" + txtBusLead.Text + "'";
            try
            {
                con.Open();
                adapter = new OleDbDataAdapter(cmd);
                if (MessageBox.Show("Esta seguro que quiere actualizar el registro ??", "ALERTA: Actualizara un registro", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning) == DialogResult.OK)
                {
                    cmd.ExecuteNonQuery();
                    MessageBox.Show("Registro actualizado correctamente...!", "Registro Actualizado", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                }
                con.Close();
                CargaDg();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                con.Close();
            }
        }
    }
Let me know you comments please.


Dan

What I have tried:

private void btnAct_Click(object sender, EventArgs e)
        {
            String sql = "UPDATE tLEADS INNER JOIN (tBUNDLES INNER JOIN tLOCALES ON tBUNDLES.IdBUNDLES = tLOCALES.IdLOCALES) ON tLEADS.IdLEADS = tBUNDLES.IdBUNDLES SET tLEADS Mnbr='" + txtMaq.Text + "', Rack=" + txtRack.Text + ", Nivel='" + txtNiv.Text + "', Localizacion='" + txtLoc.Text + "', Atados=" + txtAta.Text + ", Reorden=" + txtReor.Text + ", Pcs_Atado=" + txtPcs.Text + ", Vol=" + txtVol.Text + ", WHERE IdLEADS = '" + txtBusLead.Text + "'";
            try
            {
                con.Open();
                adapter = new OleDbDataAdapter(cmd);
                if (MessageBox.Show("Esta seguro que quiere actualizar el registro ??", "ALERTA: Actualizara un registro", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning) == DialogResult.OK)
                {
                    cmd.ExecuteNonQuery();
                    MessageBox.Show("Registro actualizado correctamente...!", "Registro Actualizado", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                }
                con.Close();
                CargaDg();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                con.Close();
            }
        }
    }
Posted
Updated 28-Aug-18 20:22pm
Comments
Richard Deeming 30-Aug-18 11:46am    
Post the full details of the exception you're getting.

1 solution

Don't do it like that! Never concatenate strings to build a SQL command. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Always use Parameterized queries instead.

When you concatenate strings, you cause problems because SQL receives commands like:
SQL
SELECT * FROM MyTable WHERE StreetAddress = 'Baker's Wood'
The quote the user added terminates the string as far as SQL is concerned and you get problems. But it could be worse. If I come along and type this instead: "x';DROP TABLE MyTable;--" Then SQL receives a very different command:
SQL
SELECT * FROM MyTable WHERE StreetAddress = 'x';DROP TABLE MyTable;--'
Which SQL sees as three separate commands:
SQL
SELECT * FROM MyTable WHERE StreetAddress = 'x';
A perfectly valid SELECT
SQL
DROP TABLE MyTable;
A perfectly valid "delete the table" command
SQL
--'
And everything else is a comment.
So it does: selects any matching rows, deletes the table from the DB, and ignores anything else.

So ALWAYS use parameterized queries! Or be prepared to restore your DB from backup frequently. You do take backups regularly, don't you?

And when you have finished updating your entire app - because if you miss just one it'll be a problem - you may well find your other problem has gone at the same time.
 
Share this answer
 
Comments
Dani Solis 29-Aug-18 2:49am    
I'm sorry

I am a beginner and the truth is that
I do not know how to use it parameterized queries. What I am doing is just a school project.
And if the backups are made with a button of my application.

I trying with this modification, but I get an error:

//SQL STMT
String sql = @"UPDATE p SET p.IdLEADS=@IdLEADS, pIdBundles=@IdBUNDLES, pIdLOCALES=@IdLOCALES FROM tLEADS p INNER JOIN p INNER JOIN tBUNDLES c INNER JOIN tLOCALES e ON cIdBUNDLES = e.IdLOCALES ON p.IdLEADS = tbl.BUNDLES.IdBUNDLES WHERE IdLEADS = @IdLEADS";// (tBUNDLES INNER JOIN tLOCALES ON tBUNDLES.IdBUNDLES = tLOCALES.IdLOCALES) ON tLEADS.IdLEADS = tBUNDLES.IdBUNDLES SET tLEADS Mnbr='" + txtMaq.Text + "', Rack=" + txtRack.Text + ", Nivel='" + txtNiv.Text + "', Localizacion='" + txtLoc.Text + "', Atados=" + txtAta.Text + ", Reorden=" + txtReor.Text + ", Pcs_Atado=" + txtPcs.Text + ", Vol=" + txtVol.Text + ", WHERE IdLEADS = '" + txtBusLead.Text + "'";
cmd = new OleDbCommand(sql, con);
cmd.Parameters.AddWithValue("@Mnbr", txtMaq.Text);
cmd.Parameters.AddWithValue("@Atados", txtAta.Text);
cmd.Parameters.AddWithValue("@Reorden", txtReor.Text);
cmd.Parameters.AddWithValue("@Pcs_Atado", txtPcs.Text);
cmd.Parameters.AddWithValue("@Vol", txtVol.Text);
cmd.Parameters.AddWithValue("@Rack", txtRack.Text);
cmd.Parameters.AddWithValue("@Nivel", txtNiv.Text);
cmd.Parameters.AddWithValue("@Localizacion", txtLoc.Text);

Dan
OriginalGriff 29-Aug-18 3:27am    
If you don't know how to use parameterized queries, then learn - and fast.
Google "Bobby Tables" and don't assume it's a joke...
Then google "C# Access Parameterized queries" and start reading...
Dani Solis 29-Aug-18 13:33pm    
Yes, I understand but now I need resolved this problem it's a school project.

Therefore in the most humble way I ask for your help in solving the problem described "Syntax error"

Thanks

Dan

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