0

Error: Invalid column name 'Id'.'

I've been trying to make an update function, the logic behind it I think is fine, but it keeps throwing me this error mentioned above.

internal static void Uredi(Zahtjev odabraniZapis)
        {
            DB.OpenConnection();
            string sql = $"UPDATE Zahtjev_za_nabavu SET Ponuda = '{odabraniZapis.Ponuda}', Opis_predmeta = '{odabraniZapis.Opis_predmeta}', Cijena = '{odabraniZapis.Cijena}', ID_zaposlenika = {odabraniZapis.ID_zaposlenika} WHERE ID_zahtjeva = {odabraniZapis.ID_zahtjeva};";
            DB.ExecuteCommand(sql);
            DB.CloseConnection();
        }

This above is the function that I call to Update existing data.

I get the row that i want to change through ID_zahtjeva .

And this is the function that calls the UPDATE function.


private void btnAzuriraj_Click(object sender, EventArgs e)
        {
            Zahtjev noviZahtjev = new Zahtjev(int.Parse(txtZahtjev.Text), txtPonuda.Text, txtOpis.Text, txtCijena.Text, int.Parse(txtZaposlenik.Text));

            var provjera = ZahtjeviRepository.GetZahtjevi(noviZahtjev.ID_zahtjeva);
            if(provjera == null)
            {
                ZahtjeviRepository.Kreiraj(noviZahtjev);
                this.Close();
            }
            else
            {
                ZahtjeviRepository.Uredi(noviZahtjev);
                this.Close();
            }
        }
Zoof
  • 55
  • 6
  • 2
    I assume that one of your placeholders does not contain what you think it contains. Please post the exact contents of `sql` *after* executing the `string sql = ...` line. – Heinzi May 30 '22 at 13:52
  • 6
    Oh, and *please* [use parameterized SQL](https://stackoverflow.com/q/35163361/87698). That's actually even more important than fixing your current problem. – Heinzi May 30 '22 at 13:53
  • Can you add the table columns in the question? – shehanpathi May 30 '22 at 13:54
  • Try to debug line by line, if sql string has the query correctly with the params; it should be working fine. – shehanpathi May 30 '22 at 13:58
  • @shehanpathirathna Yet a SQL like that could bite him/her in the ass in the future because of the SQL Injection – Cleptus May 30 '22 at 14:00
  • @shehanpathirathna table columns are ID_zahtjeva, Ponuda, Opis_predmeta, Cijena, ID_zaposlenika . If the name contains ID its int, the rest is string. – Zoof May 30 '22 at 14:05
  • I edited the post, you can now also see the function that calls the UPDATE function. – Zoof May 30 '22 at 14:10
  • Still we can't help here to much. Debug your code extract the content of the sql variable. Have a good look at it when it still seems correct try the sql in a database frontend of your choice. If the column name isn't mentioned in your sql but in your error you may take a look at broken triggers. – Ralf May 30 '22 at 14:14
  • Seems like your implementation is wrong. Can you try this way, By adding SQL params like in this article section "Without using ADO.NET"? https://qawithexperts.com/questions/436/how-can-i-run-raw-sql-query-in-c-with-and-without-adonet – shehanpathi May 30 '22 at 14:52
  • @Cleptus Yeah, I agree with you. – shehanpathi May 30 '22 at 14:53
  • I managed to fix it, thank you for all the info though! – Zoof May 30 '22 at 15:01

0 Answers0