Solved

ASP.net error

Posted on 2011-03-13
7
369 Views
Last Modified: 2012-05-11
Hi

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

Open in new window

0
Comment
Question by:murbro
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 
LVL 7

Expert Comment

by:wdfdo1986
ID: 35121408
post the upload.aspx back end code or excel connection function at least
0
 
LVL 23

Expert Comment

by:Rajkumar Gs
ID: 35121453
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
0
 
LVL 6

Expert Comment

by:Ramone_Hamilton
ID: 35121725
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.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:murbro
ID: 35121895
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(ByVal sender As Object, ByVal e As EventArgs) Handles ButtonUploadFile.Click
        If FileUploadExcel.HasFile Then
            Try
                ' alter path for your project
                FileUploadExcel.SaveAs(Server.MapPath("~/ExcelImport.xls"))
                LabelUpload.Text = "Upload File Name: " & _
                    FileUploadExcel.PostedFile.FileName & "<br>" & _
                    "Type: " & _
                    FileUploadExcel.PostedFile.ContentType & _
                    " File Size: " & _
                    FileUploadExcel.PostedFile.ContentLength & " 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.OLEDB.4.0;" & _
                  "Data Source=" & Server.MapPath("ExcelImport.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.SelectCommand = 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(objDataSet)

            ' Bind the data to the GridView
            GridViewExcel.DataSource = objDataSet.Tables(0).DefaultView
            GridViewExcel.DataBind()
        Catch ex As Exception

            MsgBox(Err.Description)

        End Try

    End Sub



End Class
0
 
LVL 23

Accepted Solution

by:
Rajkumar Gs earned 500 total points
ID: 35122117
try return null in the function's try catch

Protected Function ExcelConnection() As OleDbCommand
.....................
Catch ex As Exception
            MsgBox(Err.Description)
            return null
End Try
End Function

Open in new window

0
 

Author Closing Comment

by:murbro
ID: 35122279
Thanks
0
 
LVL 23

Expert Comment

by:Rajkumar Gs
ID: 35122338
my pleasure
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article discusses the ASP.NET AJAX ModalPopupExtender control. In this article we will show how to use the ModalPopupExtender control, how to display/show/call the ASP.NET AJAX ModalPopupExtender control from javascript, how to show/display/cal…
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

735 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question