0

No exceptions, everything gets executed but the update doesn't happen!

Everything works ok when I have just @JIR parameter but now I added @Paragon and the update doesn't do it's job. No exception whatsoever data passed is OK...

I don't see anything wrong with this query does anyone know what could possibly be going wrong?

private static void InsertJIR(FisDnevni racun)
        {
            using (OleDbConnection con = new OleDbConnection(RegistarBlagajna.Modul.VezaNaBazu.ConnectionString))
            {
                try
                {
                    con.Open();
                    OleDbCommand cmd = new OleDbCommand(@"
                    UPDATE FisDnevni
                    SET [JIR] = @JIR, 
                    [Paragon] = @Paragon                                
                    WHERE BrojRacuna = @BrojRacuna"
                    , con);
                    cmd.Parameters.AddWithValue("@JIR", racun.JIR.Substring(0,37));
                    cmd.Parameters.AddWithValue("@BrojRacuna", racun.BrojRacuna);
                    cmd.Parameters.AddWithValue("@Paragon", racun.Paragon);
                    cmd.ExecuteNonQuery();
                    con.Close();
                }
                catch (Exception)
                {
                    throw;
                }
            }
        }
7
  • Tag the dbms you're using. Commented May 18, 2017 at 9:17
  • 2
    Try using Add instead of AddWithValue. Also, try to add the parameters in the same order as they appear in the query. (this shouldn't matter but can't hurt). Also, OleDbCommand is also an IDisposable, you should use it inside a using statement. One last thing - why are you using try...catch when all you do in your catch is throw? Commented May 18, 2017 at 9:17
  • 2
    Well, you're using different order when declaring parameters, try place @Paragon in second order instead. Commented May 18, 2017 at 9:18
  • @TetsuyYamamoto hmm I think that was it lol, you can post it as answer if you want me to accept it Commented May 18, 2017 at 9:25
  • 1
    So I understand the problem is solved then. I'll put together an answer later on. If I forget to do it within the next 24 hours, feel free to answer your own question. Commented May 18, 2017 at 9:36

2 Answers 2

3

This is a good old problem - ensure the query parameters in OleDbParameter are declared in proper order like this:

using (OleDbConnection con = new OleDbConnection(RegistarBlagajna.Modul.VezaNaBazu.ConnectionString))
{
    try
    {
        con.Open();
        using (OleDbCommand cmd = new OleDbCommand(@"UPDATE FisDnevni SET [JIR] = @JIR, [Paragon] = @Paragon WHERE BrojRacuna = @BrojRacuna", con)
        {
             cmd.Parameters.AddWithValue("@JIR", racun.JIR.Substring(0,37));
             // this must be the second parameter instead of third one
             cmd.Parameters.AddWithValue("@Paragon", racun.Paragon);
             cmd.Parameters.AddWithValue("@BrojRacuna", racun.BrojRacuna);
             cmd.ExecuteNonQuery();
             con.Close();
        }
    }
    catch (Exception)
    {
        throw;
    }
}

Note that OLE DB .NET Provider doesn't recognize named parameters for OleDbCommand when CommandType is set to Text, but it apparently does recognize the parameter order, hence as long as they're passed in proper order, it'll accepted as query parameter.

Related issue:

how to update a table using oledb parameters?

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you I overlooked this, can't believe I wrote so many querys without paying attention to this and I got this far xD
Well, that's even better then the answer I was going to write, so +1.
0

maybe i've just never used it, but why is there an @ here?

OleDbCommand(@"

i also havent worked with C# in a while but is multi-line concatenation possible without a + or something?

thirdly why are you using [] on these column names? i don't see any special characters or spaces.

this post is sounding a lot more dikkish than i mean it to be, im not trying, just legit curious

1 Comment

The wonders of writing querys in C#, when I put the @ sign in front of query I don't need concatenation with +... I'm using [ ] for nothing in perticular but aesthetics :D and to distinguish the column names (even though [] helps if you use column name similar to system name for example "time")...

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.