Click here to Skip to main content
15,885,767 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
When I try to update the data base no errors occur that cause the program to crash. However, the database does not update. When I go into the output debug box it says the following:
Exception thrown: 'System.InvalidOperationException' in System.Data.dll


The message box's output (the one that outputs row_affected - the result of execute nonquery) does display 1 which is the correct number of rows it should affect.
(Sorry if I haven't formatted this correctly as this is my first time using the site)
Thanks.

What I have tried:

Try

    Dim rows_affected As Integer

    Using Conn As New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|\UserInfo.mdb")
        Using cmd As New OleDbCommand("UPDATE table1 SET TotalCorrect =  @TotCorr , TotalAnswered = @TotAnswered , TotalAcc = @TotAcc WHERE username = @userName", Conn)


            cmd.Parameters.Add("@TotCorr", SqlDbType.Int).Value = USerInfoDataArray(0, 0)
            cmd.Parameters.Add("@TotAnswered", SqlDbType.Int).Value = USerInfoDataArray(0, 1)
            cmd.Parameters.Add("@TotAcc", SqlDbType.Int).Value = USerInfoDataArray(0, 2)
            cmd.Parameters.Add("@username", SqlDbType.Text).Value = LoginForm.txtUsername.Text

            Conn.Open()
            rows_affected = cmd.ExecuteNonQuery()
            MessageBox.Show(rows_affected)
        End Using
    End Using
Catch ex As OleDbException
    MessageBox.Show(ex.ToString())
End Try
Posted
Comments
CHill60 5-Jun-20 10:16am    
When you step through your code in the debugger is it the Conn.Open line that throws that exception or the cmd.ExcecuteNonQuery?
Is there definitely data in that USerInfoDataArray in is there text in that text box - it does look like one of the array elements may be null
DuskRope 5-Jun-20 10:42am    
Hi, Thanks for your response.
It turns out as I step through the code the exception is thrown well before this point in the program - in a part of the program that works fine. I also have looked in the arrays elements and they are fully populated with none null values. The text box it is referring to also has a value in.
phil.o 5-Jun-20 10:59am    
If "the part of the program that works fine" is raising an exception, it might not be as fine as you think. Just sayin' :)
CHill60 5-Jun-20 10:59am    
As with all things - start at the beginning ... fix that exception that is thrown earlier - it clearly is not working fine at all.
If there are values in all of the parameters, then there must not be a row in the database that you are connecting to with a user name matching the one in the text box.
The connection string does not look correct - are those vertical bars actually part of your connection string?
It is easy to get caught out looking at (say) the database in your published folder when the debugger is connecting to a database in the debug folder - check your "DataDirectory" next
DuskRope 5-Jun-20 11:08am    
Thanks Chill60.
I didn't even realise that there was a separate database file in the debug file (I only started coding as part of an A level and had to teach myself all of the database things due to schools/colleges being shut.) I just needed to open it up and allow the file through windows - I have some weird security thing with access where it wont allow the file unless I run it as admin first. The updates are now being made to the database.
Thanks again.

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