I was trying to export my datagridview data to excel and when i run it after i watched the tutorial this error appeared.
It says that "An unhandled exception of type 'System.NullReferenceException' occurred in NewOrderSystem_v1.4.exe Additional information: Object reference not set to an instance of an object."
I don't know what to do now and I am stuck for 2 weeks already
Can someone help me?
Here is the screenshot of the error
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim xlApp As Microsoft.Office.Interop.Excel.Application
Dim xlWorkbook As Microsoft.Office.Interop.Excel.Workbook
Dim xlWorksheet As Microsoft.Office.Interop.Excel.Worksheet
Dim misValue As Object = System.Reflection.Missing.Value
Dim i As Integer
Dim j As Integer
xlApp = New Microsoft.Office.Interop.Excel.Application
xlWorkbook = xlApp.Workbooks.Add(misValue)
xlWorksheet = xlWorkbook.Sheets("sheet1")
For i = 0 To DataGridView1.RowCount - 1
For j = 0 To DataGridView1.ColumnCount - 1
For k As Integer = 1 To DataGridView1.Columns.Count
xlWorksheet.Cells(1, k) = DataGridView1.Columns(k - 1).HeaderText
xlWorksheet.Cells(i + 2, j + 1) = DataGridView1(j, i).Value.ToString()
Next
Next
Next
xlWorksheet.SaveAs("C:\Users\Julian\Documents\Visual Studio 2013\Projects\NewOrderSystem_v1.2\NewOrderSystem.xlsx")
xlWorkbook.Close()
xlApp.Quit()
releaseObject(xlApp)
releaseObject(xlWorkbook)
releaseObject(xlWorksheet)
MsgBox("You can find the file in this location: C:\Users\Julian\Documents\Visual Studio 2013\Projects\NewOrderSystem_v1.2\NewOrderSystem.xlsx")
Dim res As MsgBoxResult
res = MsgBox("Process Completed, Would you like to open the file?", MsgBoxStyle.YesNo)
If (res = MsgBoxResult.Yes) Then
Process.Start("C:\Users\Julian\Documents\Visual Studio 2013\Projects\NewOrderSystem_v1.2\NewOrderSystem.xlsx")
End If
End Sub
Private Sub releaseObject(ByVal obj As Object)
Try
System.Runtime.InteropServices.Marshal.ReleaseComObject(obj)
obj = Nothing
Catch ex As Exception
obj = Nothing
Finally
GC.Collect()
End Try
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
'TODO: This line of code loads data into the 'Database1DataSet.Table1' table. You can move, or remove it, as needed.
Me.Table1TableAdapter.Fill(Me.Database1DataSet.Table1)
'TODO: This line of code loads data into the 'NewOrderSystemDataSet.NewOrderSystemTable' table. You can move, or remove it, as needed.
Me.NewOrderSystemTableTableAdapter.Fill(Me.NewOrderSystemDataSet.NewOrderSystemTable)
End Sub
End Class
And here is the code that I have
Thank you for all the people who will help
Added the screenshot of the null variable, so how can I make this not null?