2

The Dropdownlist data binding using throw common function. In this have a problem in passing dropdownlist.

Error: Object cannot be set an instance of object

The code is below

Aspx Page Code:

<asp:DropDownList ID="ddlagent1" CssClass="ParameterTextBox" runat="server" AutoPostBack="false"></asp:DropDownList>

Protected Sub chkcity_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles chkcity.CheckedChanged
 ldstOption = //fill values here
 lobjGeneral.loadComboBox1(Me.ddlagent1, ldstOption.Tables(0), "No Data found")
End Sub

General Class:

Public Sub loadComboBox1(ByVal cbo As DropDownList, ByVal dset As DataTable, ByVal msgstr As String, _
                Optional ByVal visible As Boolean = True, _
                Optional ByVal enabled As Boolean = True, _
                Optional ByVal tno As Integer = 0, _
                Optional ByVal dmem As Integer = 2, _
                Optional ByVal vmem As Integer = 1)

        Try
            If (dset.Rows.Count > 0) Then
                cbo.DataSource = dset
                cbo.DataTextField = dset.Columns(dmem).Caption
                cbo.DataValueField = dset.Columns(vmem).Caption
                cbo.DataBind()
                cbo.SelectedIndex = 0
                cbo.Visible = visible
                cbo.Enabled = enabled
            Else
                dset = Nothing
                cbo.Text = msgstr
            End If
        Catch ex As Exception
            'Call CreateErrorLogFile("General", "loadComboBox", Err.Description)
        End Try
    End Sub
7
  • On which line are you getting the exception? May 17, 2011 at 8:46
  • @rewinder: lobjGeneral.loadComboBox1(Me.ddlagent1, ldstOption.Tables(0), "No Data found") May 17, 2011 at 8:49
  • dataset has data and before run this line me.ddlagent1 as dropdownlist object. After run that line its change to null object and also its not calling the function May 17, 2011 at 8:52
  • Can you show where you are defining the lobjGeneral variable?
    – patmortech
    May 17, 2011 at 9:03
  • 1
    Top of the class: Dim lobjGeneral As New clsGeneral May 17, 2011 at 9:05

1 Answer 1

2

Your method should have to get Dropdownlist reference

Public Sub loadComboBox1(ByRef cbo As DropDownList, ......
4
  • You need to declare your dropdownlist variable as reference type instead value type. May 17, 2011 at 8:54
  • you mean same function work in page load, but not working on dropdown selection changed? May 17, 2011 at 8:57
  • did you change ByVal to ByRef ? ByRef cbo As DropDownList? May 17, 2011 at 9:03
  • @Muhammad: Its Ok i finish it. In page load end i set nothing to lobjGeneral object. that is problem !! May 17, 2011 at 10:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.