Windows Develop Bookmark and Share   
 index > Windows Forms Data Controls and Databinding > Datatable merge throw System.NullReferenceException Object reference not set to an instance of an object
 

Datatable merge throw System.NullReferenceException Object reference not set to an instance of an object

hello, hope i post in the right forum.

I've a small app to read data from XML and import into SQL 2005.
My idea is use type dataset, first read data from SQL db into datatable1, create another datatable2 read data from xml, merge datatable2 into datatable1, then update datatable1 back to SQL. Bellow is code:
------------------------------------------------------------
Dim ta As New TypedDataSetTableAdapter
Dim dt1 As TypedDatasetDataTable = ta.GetData

Dim filepath As String = "c:\prjinfo.xml"
Dim xmlSchema As String = "c:\prjinfo.xsd"
Dim xds As New DataSet
xds.ReadXmlSchema(xmlSchema)
xds.ReadXml(filepath)

Dim dt2 As DataTable
dt2 = xds.Tables.Item(0).Copy()

If Not dt1 Is Nothing And Not dt2 Is Nothing Then
dt1.Merge(dt2)
End If

Dim dtChanges AsTypedDatasetDataTable= CType(dt1.GetChanges, TypedDatasetDataTable)
If NotdtChangesIs Nothing Then
ta.Update(dtChanges)
End If
----------------------------------------------------------------------
The problem is every time when runing into dt1.merge(dt2), system throwSystem.NullReferenceException: Object reference not set to an instance of an object.
no matter I use or not use new for dt1, dt2 or both, same result. from debugging, i can see data in dt1 and dt2.

anybody help?

Ay
Ay Xu  Friday, July 31, 2009 2:24 AM

Hi Ay Xu,

Could you please provide the schema and data of dt1 and dt2? I cannot reproduce your error without them.

You can check if the root cause of your issue is the primary key column names from:
https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=257651&wa=wsignin1.0.

Let me know if this helps.
Aland Li


Please mark the replies as answers if they help and unmark if they don't. This can be beneficial to other community members reading the thread.
Aland Li  Monday, August 03, 2009 9:08 AM

Hi,

We are changing the issue type to “General Discussion�because you have not followed up with the necessary information. If you have more time to look at the issue and provide more information, please feel free to change the issue type back to “Question�by opening the Options list at the top of the post window, and changing the type. If the issue is resolved, we will appreciate it if you can share the solution so that the answer can be found and used by other community members having similar questions.

Aland Li


Please mark the replies as answers if they help and unmark if they don't. This can be beneficial to other community members reading the thread.
Aland Li  Friday, August 07, 2009 12:26 PM

You can use google to search for other answers

Custom Search

More Threads

• Binding multiple sources to 1 datagridview
• BindingSource.Filter and Bitwise Operation
• binding rtf formatted text in ms sql 2000
• Some problem about columns of DataGrid
• Column Expressions
• MonthCalendar
• datagridviewbuttoncolumn text missing problem
• Cell Validating Event
• Insert New Row Into Existing DataTable While Looping
• add new record in the binding datasource using BindingManagerBase.AddNew() method