-2

I keep getting this exception over and over. I've tried separating my query into two separate queries, that didn't work. I've checked to make sure the db connection is closed elsewhere before it's opened during this method, it's definitely closed before the function is called and opened before any queries.

Below iss the code for the function. I've set breakpoints and the query itself is fine. The code is the exact same that I used previously for updating a PIN function, with just the query string changed, so I don't know why it's causing issues:

Code:

public void transferMoney(string senderIban, decimal senderBalance, string receiverIban, decimal transferAmount,string messageOptional)
{
    //myBankAccount.AccountPin = updatedPin;
    DataTable dtUser = new DataTable();
    sqlconnConnection.Open();

    string strQuery2 = @"UPDATE Accounts SET Balance = Balance + " + Convert.ToDecimal(transferAmount) + " WHERE GUID = '" + receiverIban + "';"
                        + "UPDATE Accounts SET Balance = Balance - " + Convert.ToDecimal(transferAmount) + " WHERE GUID = '" + senderIban + "';";
    // example of a Paramaterised SQL statement.
    SQLiteCommand sqlcomCommand2 = new SQLiteCommand(strQuery2, sqlconnConnection);
    SQLiteDataAdapter sqldatadptAdapter = new SQLiteDataAdapter(sqlcomCommand2);  // local SQL data Adaptor                


    try
    {
        // sqldatadptAdapter.Fill(dtUser);
        sqlcomCommand2.ExecuteNonQuery();
    }
    catch (Exception ex)
    {
        // Exception will the "thrown" when there was a problem
        throw new Exception($"UPDATE WAS unsuccessful:\n{ex.Message}");
    }
    finally
    {
        sqlconnConnection.Close();

    }
Mark Rotteveel
  • 90,369
  • 161
  • 124
  • 175
  • [What are good ways to prevent SQL injection?](https://stackoverflow.com/questions/14376473/what-are-good-ways-to-prevent-sql-injection) • [SqlCommand Parameters Add vs. AddWithValue](https://stackoverflow.com/questions/21110001/sqlcommand-parameters-add-vs-addwithvalue) –  Aug 26 '21 at 11:37
  • Which ADO.NET provider? Or what library from what vendor? Via an ODBC driver? Which versions? Who is using the file at the same time? What connection string and/or ODBC.INI settings? Local or remote? What Operating System? What .NET implementation? What platform? What is sqlconnConnection? What about the sqldatadptAdapter not used in the code provided? Does the database already opened elsewhere in the current project before calling this method? Fell free to add any details and code that can help to help. –  Aug 26 '21 at 11:40

1 Answers1

-2

Maybe you have a DB browser opened? Or you have accessed the DB some other way. This error only occurs when DB is modified or used elsewhere. If you can't find it, I'd suggest restarting PC just in case there something hidden :)

P.S. Posting this as answer as I cannot comment under the question for technical reasons :)

David Oganov
  • 658
  • 1
  • 7
  • 18
  • 1
    "*P.S. Posting this as answer as I cannot comment under the question for technical reasons :)*" => Why? –  Aug 26 '21 at 11:45
  • Don't have enough reputation yet. 50+ reputation required to use the feature. So I cannot start a new thread, but can answer in an existing thread under my answer. Hopefully soon will be able to freely write comments :) – David Oganov Aug 26 '21 at 11:57
  • 1
    [Why do I need 50 reputation to comment? What can I do instead?](https://meta.stackexchange.com/questions/214173/why-do-i-need-50-reputation-to-comment-what-can-i-do-instead) –  Aug 26 '21 at 13:12
  • 1
    Yeah, this basically says that you can do nothing about that – David Oganov Aug 26 '21 at 18:08
  • Just ask or answer a few questions and get points: this is the Stack Overflow game to help and be helped, if not what else? –  Aug 26 '21 at 18:11
  • That's what I'm doing :) – David Oganov Aug 26 '21 at 19:04