Murray Brown
asked on
ASP.net error
Hi
I am getting the following error on the code shown below in ASP.net
I am getting the following error on the code shown below in ASP.net
Warning 1 Function 'ExcelConnection' doesn't return a value on all code paths. A null reference exception could occur at run time when the result is used. C:\Users\User\Documents\Visual Studio 2010\Projects\Green Calendar\Green Calendar\Upload.aspx.vb 63 5 Green Calendar
post the upload.aspx back end code or excel connection function at least
If there is any 'If' condition OR 'switch' something like that you need to return the value in all conditions
In other words , the function need to return value in all of its condition. Surely in some condition it is missing - please check
Raj
In other words , the function need to return value in all of its condition. Surely in some condition it is missing - please check
Raj
In your ExcelConnection function you are not returning a valid value for the functions end points. If the function is boolean you need to make sure you return a true or false, an integer returns an integer etc.
ASKER
Sorry. Thought I had posted the code. Here iy is:
Imports System.Data.OleDb
Public Class WebForm1
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
End Sub
Protected Sub ButtonUpload_Click(ByVal sender As Object, ByVal e As EventArgs) Handles ButtonUpload.Click
Try
PanelUpload.Visible = True
PanelView.Visible = False
PanelImport.Visible = False
Catch ex As Exception
MsgBox(Err.Description)
End Try
End Sub
Protected Sub ButtonUploadFile_Click(ByV al sender As Object, ByVal e As EventArgs) Handles ButtonUploadFile.Click
If FileUploadExcel.HasFile Then
Try
' alter path for your project
FileUploadExcel.SaveAs(Ser ver.MapPat h("~/Excel Import.xls "))
LabelUpload.Text = "Upload File Name: " & _
FileUploadExcel.PostedFile .FileName & "<br>" & _
"Type: " & _
FileUploadExcel.PostedFile .ContentTy pe & _
" File Size: " & _
FileUploadExcel.PostedFile .ContentLe ngth & " kb<br>"
Catch ex As Exception
LabelUpload.Text = "Error: " & ex.Message.ToString
End Try
Else
LabelUpload.Text = "Please select a file to upload."
End If
End Sub
Protected Function ExcelConnection() As OleDbCommand
Try
' Connect to the Excel Spreadsheet
Dim xConnStr As String = "Provider=Microsoft.Jet.OL EDB.4.0;" & _
"Data Source=" & Server.MapPath("ExcelImpor t.xls") & ";" & _
"Extended Properties=Excel 8.0;"
' create your excel connection object using the connection string
Dim objXConn As New OleDbConnection(xConnStr)
objXConn.Open()
' use a SQL Select command to retrieve the data from the Excel Spreadsheet
' the "table name" is the name of the worksheet within the spreadsheet
' in this case, the worksheet name is "Members" and is expressed as: [Members$]
Dim objCommand As New OleDbCommand("SELECT * FROM [Members$]", objXConn)
Return objCommand
Catch ex As Exception
MsgBox(Err.Description)
End Try
End Function
Protected Sub ButtonView_Click(ByVal sender As Object, ByVal e As EventArgs) Handles ButtonView.Click
Try
'The ButtonView_Click event handler starts by hiding the PanelUpload and PanelImport Panels,
' while showing the PanelView interface (which is where the GridView is located).
PanelUpload.Visible = False
PanelView.Visible = True
PanelImport.Visible = False
'Next, a new OleDbDataAdapter object is created and its SelectCommand is assigned the OleDbCommand object returned by the ExcelConnection function.
' Create a new Adapter
Dim objDataAdapter As New OleDbDataAdapter()
' retrieve the Select command for the Spreadsheet
objDataAdapter.SelectComma nd = ExcelConnection()
'The purpose of this OleDbDataAdapter object is to populate a DataSet with the contents of the Excel query; this DataSet is then bound to the GridView.
' Create a DataSet
Dim objDataSet As New DataSet()
' Populate the DataSet with the spreadsheet worksheet data
objDataAdapter.Fill(objDat aSet)
' Bind the data to the GridView
GridViewExcel.DataSource = objDataSet.Tables(0).Defau ltView
GridViewExcel.DataBind()
Catch ex As Exception
MsgBox(Err.Description)
End Try
End Sub
End Class
Imports System.Data.OleDb
Public Class WebForm1
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
End Sub
Protected Sub ButtonUpload_Click(ByVal sender As Object, ByVal e As EventArgs) Handles ButtonUpload.Click
Try
PanelUpload.Visible = True
PanelView.Visible = False
PanelImport.Visible = False
Catch ex As Exception
MsgBox(Err.Description)
End Try
End Sub
Protected Sub ButtonUploadFile_Click(ByV
If FileUploadExcel.HasFile Then
Try
' alter path for your project
FileUploadExcel.SaveAs(Ser
LabelUpload.Text = "Upload File Name: " & _
FileUploadExcel.PostedFile
"Type: " & _
FileUploadExcel.PostedFile
" File Size: " & _
FileUploadExcel.PostedFile
Catch ex As Exception
LabelUpload.Text = "Error: " & ex.Message.ToString
End Try
Else
LabelUpload.Text = "Please select a file to upload."
End If
End Sub
Protected Function ExcelConnection() As OleDbCommand
Try
' Connect to the Excel Spreadsheet
Dim xConnStr As String = "Provider=Microsoft.Jet.OL
"Data Source=" & Server.MapPath("ExcelImpor
"Extended Properties=Excel 8.0;"
' create your excel connection object using the connection string
Dim objXConn As New OleDbConnection(xConnStr)
objXConn.Open()
' use a SQL Select command to retrieve the data from the Excel Spreadsheet
' the "table name" is the name of the worksheet within the spreadsheet
' in this case, the worksheet name is "Members" and is expressed as: [Members$]
Dim objCommand As New OleDbCommand("SELECT * FROM [Members$]", objXConn)
Return objCommand
Catch ex As Exception
MsgBox(Err.Description)
End Try
End Function
Protected Sub ButtonView_Click(ByVal sender As Object, ByVal e As EventArgs) Handles ButtonView.Click
Try
'The ButtonView_Click event handler starts by hiding the PanelUpload and PanelImport Panels,
' while showing the PanelView interface (which is where the GridView is located).
PanelUpload.Visible = False
PanelView.Visible = True
PanelImport.Visible = False
'Next, a new OleDbDataAdapter object is created and its SelectCommand is assigned the OleDbCommand object returned by the ExcelConnection function.
' Create a new Adapter
Dim objDataAdapter As New OleDbDataAdapter()
' retrieve the Select command for the Spreadsheet
objDataAdapter.SelectComma
'The purpose of this OleDbDataAdapter object is to populate a DataSet with the contents of the Excel query; this DataSet is then bound to the GridView.
' Create a DataSet
Dim objDataSet As New DataSet()
' Populate the DataSet with the spreadsheet worksheet data
objDataAdapter.Fill(objDat
' Bind the data to the GridView
GridViewExcel.DataSource = objDataSet.Tables(0).Defau
GridViewExcel.DataBind()
Catch ex As Exception
MsgBox(Err.Description)
End Try
End Sub
End Class
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks
my pleasure