i have this procedure that fill my wingrid but i have an intermitent problem, if i ran my process 30 times only 2 or 3 times will throw the following error, any idea on what is wrong with my code or is a wingrid problem?
Error
not alway same error, but intermitent, all of them had results the store procedure
1) ************** Exception Text **************System.ArgumentException: An item with the same key has already been added. at System.ThrowHelper.ThrowArgumentException(ExceptionResource resource)
2) ************** Exception Text **************System.ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection.Parameter name: index
Code
Private Sub LoadResults(ByVal StrFromDate As String, ByVal StrPercentage As String) Dim DS As DataSet = New DataSet
Try QueryHst = False DS.Tables.Clear() Application.DoEvents()
DS.Tables.Add(Conn.GetData("exec sp_DataGetPriceError " & _ "@FromDate = '" + StrFromDate + "', " & _ "@Percentage = " + IIf(StrPercentage = 100, "1.0", "0." + Format(CInt(StrPercentage), "00").ToString)))
'Show the grid dgv1.ResetLayouts() dgv1.SetDataBinding(DS, "table1")
ShowAnimation(False)
Catch Ex As Exception
Conn.LogError(gUser.pUserID.ToString, My.Computer.Name.ToString, "Frm Price Error", "Load Results", "value change", "1", Ex.Message.ToString) End Try
End Sub
store procedure always return a value.
Image
The big red X you are seeing here means that an exception occurred in the OnPaint of a control. This is not specific to the grid, it happens with any control.
So the question is... why is an exception occurring when the grid is painting. It might help to see the details of the exception (by pushing the Details button on the error dialog).
But my guess is that it's the DoEvents call you have in your code. Using DoEvents is generally a bad idea and should be avoided unless absolutely neccessary. Even Microsoft's documentation advises against using it because it can change the order of events and cause unexpected results.
If I am wrong, and it's not the DoEvents, then my next guess would be that your application is using multiple threads.
yes my application is using multiple threads.
when the loaddata event is call is get assigned to a thread and when it finish the thread is aborted
Well, if the grid is bound to a data source and you are modifying that data source on another thread, then that pretty much solves the mystery here.
Neither the grid, nor the BindingManager in DotNet are thread-safe.There is a detailed discussion of this kind of this here:
Work with a dataset bound to a grid on a separate thread - Infragistics Community