4 Replies - 1907 Views - Last Post: 16 February 2010 - 07:06 PM Rate Topic: -----

#1 Sue816  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 92
  • Joined: 22-April 09

Function not returning a value?

Posted 15 February 2010 - 02:43 PM

I have a page load procedure and then a function, and I want the function to extract the data for a selected customer from the datasource and create a customer object. The load procedure calls the function, and I thought it would change two labels to the address and city, but when I run the program, the labels do not change. I also have an error about the function not returning a value on all code paths. ?

Private selectedCustomer As Customer

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Try
            If Not IsPostBack Then
                ddlSelectedCustomer.DataBind()
            End If
            selectedCustomer = Me.GetSelectedCustomer
            lblAddress1.Text = selectedCustomer.Address
            lblAddress2.Text = selectedCustomer.City
        Catch ex As Exception
        End Try
    End Sub


    Private Function GetSelectedCustomer() As Customer
        Try
            Dim customerTable As DataView = CType(SqlDataSource1.Select( _
            DataSourceSelectArguments.Empty), DataView)
            customerTable.RowFilter = "CustomerID = ' " _
            & ddlSelectedCustomer.SelectedValue & " ' "
            Dim customerRow As DataRowView = customerTable(0)

            Dim customer As New Customer
            customer.CustomerID = customerRow("CustomerID").ToString
            customer.Address = customerRow("Address").ToString
            customer.City = customerRow("City").ToString
            Return customer
        Catch ex As Exception
        End Try
    End Function



Is This A Good Question/Topic? 0
  • +

Replies To: Function not returning a value?

#2 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1639
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: Function not returning a value?

Posted 15 February 2010 - 05:49 PM

FYI It's bad practice to swallow an exception the way you are doing there. If you're going to use a Try...Catch block then do something with the exception if it's thrown. The reason for the "not returning a value on all code paths" is because of your empty Catch block, either move your Return statement below the Catch block or return nothing in it like this

Private Function GetSelectedCustomer() As Customer
        Try
            Dim customerTable As DataView = CType(SqlDataSource1.Select( _
            DataSourceSelectArguments.Empty), DataView)
            customerTable.RowFilter = "CustomerID = ' " _
            & ddlSelectedCustomer.SelectedValue & " ' "
            Dim customerRow As DataRowView = customerTable(0)

            Dim customer As New Customer
            customer.CustomerID = customerRow("CustomerID").ToString
            customer.Address = customerRow("Address").ToString
            customer.City = customerRow("City").ToString
            Return customer
        Catch ex As Exception
            Return Nothing
        End Try
End Function



You also have an empty Try...Catch block in your load event, bad practice. In your load event you have ddlSelectedCustomer.DataBind(), yet you dont tell it what to bind to. For testing leave your function code the way it is and chance your load event to look like this (so we can see if an Exception is happening and you're swallowing it)

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Try
            If Not IsPostBack Then
                ddlSelectedCustomer.DataBind()
            End If
            selectedCustomer = Me.GetSelectedCustomer
            lblAddress1.Text = selectedCustomer.Address
            lblAddress2.Text = selectedCustomer.City
        Catch ex As Exception
            Response.Write(ex.ToString())
        End Try
End Sub


Was This Post Helpful? 1
  • +
  • -

#3 Sue816  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 92
  • Joined: 22-April 09

Re: Function not returning a value?

Posted 16 February 2010 - 06:35 PM

When you say that I don't tell ddlSelectedCustomer.Databind() what to bind to, what do you mean? I had thought that I was telling it to bind the selected customer in the drop down list, no? I think I'm missing that concept.

I removed the Try/Catches and I get Cannot perform '=' operation on System.Int32 and System.String. at productTable.Rowfilter. :cry2:
Was This Post Helpful? 0
  • +
  • -

#4 Sue816  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 92
  • Joined: 22-April 09

Re: Function not returning a value?

Posted 16 February 2010 - 06:52 PM

I got rid of the above error, and I now get - Index 0 is either negative or above rows count.
Was This Post Helpful? 0
  • +
  • -

#5 Sue816  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 92
  • Joined: 22-April 09

Re: Function not returning a value?

Posted 16 February 2010 - 07:06 PM

Got it working =) Finally. Tks =)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1