Hello. My code Adds a New Row from a Source Table called "objTablaOrigen" To a final table called "objTablaDestino". This tables are passed ByRef because they come from a DataSet respectively. objRowNuevo is The Row that will be added to "objTablaDestino" The data from "objRow" is well copied to the "objRowNuevo" (that is the new Row with type of objTablaDestino).
'The problem occurs when the instruction Add is executed. What happens The new row is added as well, but this generates an Exception with the next message:
"Object reference not set to an instance of an object.] Err.Trace[ at System.Data.DataTable.InsertRow(DataRow row, Int32 proposedID, Int32 pos)
at System.Data.DataTable.AddRow(DataRow row, Int32 proposedID)
at System.Data.DataRowCollection.Add(DataRow row) ... etc."
I don’t understand why generates this message.
Can you help me please to understand what causes this exception
I have discarded that the cause is the Data in the source row because i have parsed all the content of the source row to a string type and temporaly saved in the "strtemp" variable.
Private
Sub InsertaRow(ByRef objTablaOrigen As DataTable, _ ByRef objTablaDestino As DataTable, _ ByRef objRow As DataRow)Try
Dim objRowNuevo As DataRow
objRowNuevo = objTablaDestino.NewRow
Dim objColumna As DataColumn
Dim CInfoUS As CultureInfo = New CultureInfo("en-US", True)
Dim objDateTime As DateTime
Dim strTemp As String
For Each objColumna In objTablaDestino.Columns If objTablaOrigen.Columns.Contains(objColumna.ColumnName) ThenstrTemp = Convert.ToString(objRow(objColumna.ColumnName), CInfoUS)
objRowNuevo(objColumna.ColumnName) = strTemp
End If Next'Making a Debug, and Watching the "objRowNuevo" object , i don't see any difference on the "objRow" 'object (that is the source) and the "objRowNuevo" object (that is the new row).
objTablaDestino.Rows.Add(objRowNuevo)
'The rare is that this line executes well, and if i watch the "objTablaDestino" object i can see the new row as 'added. However, the exception is throwed.
'And the final rare problem is: When this objRow is like "added" to the final DataTable that is part of a 'DataSet that is connected to a DataGrid, The Data Grid shows the new Row Added, but , the rare is that 'when i click on this new row in the dataGrid, the Application collapses and closes.
'The question is: why
' I hope you can help me with this problem. Thanks
Catch
ConError As ExceptionclsError.ErrorHandler(0, ConError, "clsPedidosD", "InsertaRow")
End TryEnd Sub

Problem With DataRow