Click here to Skip to main content
15,905,316 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi, I get a problem with "Unspecified Error : OleDbException was unhandled by user code".
I have a class for retriving datas from access database to datagridview. In rowpostpaint event of datagridview, it will call and get a returning value from a function. So, if i do something to refresh the datagridview repetely for a while, it will occure an error like this.

Here is codes in DataGridViewRowPostPaint:
<pre lang="vb">
Private Sub gvemployee_RowPostPaint(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewRowPostPaintEventArgs) Handles gvemployee.RowPostPaint
       'Try
       If Not e.IsLastVisibleRow Then
           If cancelselectedCell = 0 Then
               gvemployee.Rows(e.RowIndex).Cells(0).Value = e.RowIndex + 1
               Dim theID As Integer
               theID = gvemployee.Rows(e.RowIndex).DataBoundItem("emp_id")
               Dim dr As OleDbDataReader
               dr = getState(theID)
               If dr.Read Then
                   If dr("working_state") <> "" Then
                       If dr("working_state") = "ทำงาน" Then
                           gvemployee.Rows(e.RowIndex).Cells(4).Value = True
                       ElseIf dr("working_state") = "ลาป่วยครึ่งวัน" Then
                           gvemployee.Rows(e.RowIndex).Cells(5).Value = True
                       ElseIf dr("working_state") = "ลาป่วยเต็มวัน" Then
                           gvemployee.Rows(e.RowIndex).Cells(6).Value = True
                       ElseIf dr("working_state") = "ลากิจครึ่งวัน" Then
                           gvemployee.Rows(e.RowIndex).Cells(7).Value = True
                       ElseIf dr("working_state") = "ลากิจเต็มวัน" Then
                           gvemployee.Rows(e.RowIndex).Cells(8).Value = True
                       ElseIf dr("working_state") = "ขาด" Then
                           gvemployee.Rows(e.RowIndex).Cells(9).Value = True
                       Else
                           'gvemployee.Rows.Count
                           'gvemployee.Rows(e.RowIndex).Cells(0).Value = True
                       End If
                   End If
                   'Me.Cursor = System.Windows.Forms.Cursors.WaitCursor
                   Threading.Thread.Sleep(20)

               End If
           End If
       End If
       If e.IsLastVisibleRow Then
           Me.Cursor = System.Windows.Forms.Cursors.Default
       End If
       ''Catch ex As Exception
       '   'MsgBox(ex.Message)
       ''End Try
   End Sub


And here is codes in My function which is called from DataGridViewRowPostPaintEvent (the error most occur here):

VB
Function getState(ByVal newId As String) As OleDbDataReader
        'Try
        Dim dr As OleDbDataReader
        Dim cn As OleDbConnection = New OleDbConnection(MySTR)
        Dim cmd As OleDbCommand


        Threading.Thread.Sleep(50)
        Dim dt As DataTable = New DataTable

        With cn
            If .State = ConnectionState.Open Then .Close()
            .ConnectionString = MySTR
            .Open()
        End With
        cmd = New OleDbCommand("select * from tb_working_info  where emp_id=" & newId & " and working_date=datevalue('" & dtworkingInfo.Value.Date & "')", cn)
        dr = cmd.ExecuteReader()
        Return dr
        'Catch ex As Exception
        'MsgBox(ex.Message)
        'End Try
        Me.Cursor = System.Windows.Forms.Cursors.WaitCursor



    End Function




The error pic is here:
[See Error Picture here]

Thank you.
Posted

Why the Threading.Thread.Sleep(..)?
Are you casting the dtworkingInfo.Value.Date correctly to string?
Why close the connection in getState when it was just opened?
Try to close the connection directly when done: cmd.ExecuteReader(CommandBehavior.CloseConnection);
Try change this in your SQL: where emp_id='" & newId & "' and...

Try get some inspiration from examples of others. To be honest, your code doesn't look to solid. Have a look at the links below.

http://www.codeproject.com/KB/aspnet/EasyADODgrids.aspx[^]

http://www.codeproject.com/KB/database/databinding_tutorial.aspx[^]

http://www.codeproject.com/KB/XML/CreateXMLDB.aspx[^]

Good luck!
 
Share this answer
 
Comments
jack_th 8-Nov-10 8:56am    
I just guessed that, maybe because of looping in rowpostPaintEvent which is the cause of this error. So just I tried to make a sleep to delay each time of openning or closing the connection. However it still the same.
Now, I am trying to edit my code belong to your suggessions.
You're creating a new connection to the database, starting a DataReader, but you're never closing the connection when you're done with the DataReader.

It also appears as though you're doing a database query after the paint OF EVERY VISIBLE ROW of a DGV. WHY?? There's no good reason for this. You should be getting this data ahead of time, caching it and then doing whatever you need to do after the paint. All you're doing is dramatically slowing down the painting of the DGV.
 
Share this answer
 
Comments
fjdiewornncalwe 10-Nov-10 19:01pm    
Just what Dave said. You never close the connection, so when you try to open another one, the file is locked by the first connection. I also suggest you consider the 2nd part as well. You really, really should do that.
As from the image you've supplied it errors on the open of the connection object.

I would start by checking

1. the connection string via Connection Strings Website[^]
2. create a try catch block around the function and see what detail the error message is


post the full error message if you need more help
 
Share this answer
 
Comments
jack_th 8-Nov-10 9:20am    
Here is my connection string "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=payroll.mdb;" and I have to tell you more about the behavior of this error.
Behavior: After I have run the program and when I come into the page (Page which error occur),there is no error occur at fist time. And I still can do some activity with no error ,though the program to run through rowPostPaint event for a while. But if I still do this acctivity repeately longer, the error will occur.
Here are detial of the error:

System.Data.OleDb.OleDbException was unhandled by user code
ErrorCode=-2147467259
Message="Unspecified error"
Source="Microsoft JET Database Engine"
StackTrace:
at System.Data.OleDb.OleDbConnectionInternal..ctor(OleDbConnectionString constr, OleDbConnection connection)
at System.Data.OleDb.OleDbConnectionFactory.CreateConnection(DbConnectionOptions options, Object poolGroupProviderInfo, DbConnectionPool pool, DbConnection owningObject)
at System.Data.ProviderBase.DbConnectionFactory.CreateNonPooledConnection(DbConnection owningConnection, DbConnectionPoolGroup poolGroup)
at System.Data.ProviderBase.DbConnectionFactory.GetConnection(DbConnection owningConnection)
at System.Data.ProviderBase.DbConnectionClosed.OpenConnection(DbConnection outerConnection, DbConnectionFactory connectionFactory)
at System.Data.OleDb.OleDbConnection.Open()
at QSBGPayRoll.ManageWorkingInfo.getState(String newId) in E:\WORKSHOP\QSBGPayRoll\QSBGPayRoll\Management\ManageWorkingInfo.vb:line 386
at QSBGPayRoll.ManageWorkingInfo.gvemployee_RowPostPaint(Object sender, DataGridViewRowPostPaintEventArgs e) in E:\WORKSHOP\QSBGPayRoll\QSBGPayRoll\Management\ManageWorkingInfo.vb:line 337
at System.Windows.Forms.DataGridView.OnRowPostPaint(DataGridViewRowPostPaintEventArgs e)
at System.Windows.Forms.DataGridViewRow.Paint(Graphics graphics, Rectangle clipBounds, Rectangle rowBounds, Int32 rowIndex, DataGridViewElementStates rowState, Boolean isFirstDisplayedRow, Boolean isLastVisibleRow)
at System.Windows.Forms.DataGridView.PaintRows(Graphics g, Rectangle boundingRect, Rectangle clipRect, Boolean singleHorizontalBorderAdded)
at System.Windows.Forms.DataGridView.PaintGrid(Graphics g, Rectangle gridBounds, Rectangle clipRect, Boolean singleVerticalBorderAdded, Boolean singleHorizontalBorderAdded)
at System.Windows.Forms.DataGridView.OnPaint(PaintEventArgs e)
InnerException:
 
Share this answer
 

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