Click here to Skip to main content
15,851,090 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
With refernce to below mentioned solution I have converted code from C# to VB but now I am stuck in Object reference error please help so i can convert my excel to csv

What I have tried:

Convert .xlsx & .xls to .csv[^]

Dim Upath As String = "~/Uploads/" + CDate(Today.Date).ToString("yyyyMMdd")
Dim excelPath As String = Server.MapPath(Upath + "/BKP/") + Path.GetFileName(FileUpload1.PostedFile.FileName)
FileUpload1.SaveAs(excelPath)
Dim csvPath As String = Server.MapPath(Upath) + "/" + Path.GetFileNameWithoutExtension(FileUpload1.PostedFile.FileName) + ".csv"
Dim stream As FileStream = File.Open(excelPath, FileMode.Open, FileAccess.Read)
Dim excelReader As IExcelDataReader = ExcelReaderFactory.CreateBinaryReader(stream)
'Dim excelReader As IExcelDataReader = ExcelReaderFactory.CreateOpenXmlReader(stream)
excelReader.IsFirstRowAsColumnNames = True
Dim result As DataSet = excelReader.AsDataSet()
Dim csvdata As String = ""
Dim row_no As Integer = 0
Dim DT As DataTable
DT = result.Tables(0) ''HERE I AM OBJECT REFERENCE ERROR
GridView1.DataSource = DT
excelReader.Close()
Posted
Updated 12-May-23 5:10am
v2

Please post your question in the forum at the end of the article, so that author can help you.
 
Share this answer
 
Comments
Hemil Gandhi 14-Apr-20 11:27am    
have already done that
Richard MacCutchan 14-Apr-20 11:51am    
If you want anyone to help here you need to explain what your problem is.
Hemil Gandhi 14-Apr-20 12:06pm    
question updated as i have tried multiple solutions , now i am stuck with object reference error.
Richard MacCutchan 14-Apr-20 12:23pm    
You have declared result but not created any Tables inside it (I assume). So the reference Tables(0) is most likely null. You can check that with the debugger.
Hemil Gandhi 15-Apr-20 10:17am    
Sir sorry to bother you but i am beginner, so i dont get your solution. please elaborate it.
Dim DT As New DataTable


Try this, perhaps it helps.
 
Share this answer
 
Comments
Richard Deeming 12-May-23 10:15am    
Read the question again:
Dim DT As DataTable
DT = result.Tables(0) ''HERE I AM OBJECT REFERENCE ERROR

The DT variable is assigned on the line immediately after it is declared. Setting it to a new DataTable instance only to immediately throw that instance away isn't going to help.

The problem is clearly that either result is Nothing, or it doesn't contain any tables.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900