I created a DataGridView in VB.Net without any data source and kept it editable so that user can input the data. Created just 2 columns using built-in "Edit Columns" option in form designer.
Below is my code on click event, I would like to save/get the data as XML that is input by users into the datagridview.
Dim dt As DataTable = CType(DataGridView1.DataSource, DataTable)
Dim stream As Stream = New MemoryStream()
dt = CType(DataGridView1.DataSource, DataTable)
dt.WriteXml(stream) ''Giving Error at this point
Dim count As Integer = CInt(stream.Length)
Dim arr As Byte() = New Byte(count - 1) {}
stream.Seek(0, SeekOrigin.Begin)
stream.Read(arr, 0, count)
Dim utf As UnicodeEncoding = New UnicodeEncoding()
MsgBox(utf.GetString(arr).Trim())
Now, this gives an error on button click:
Object reference not set to an instance of an object.