Error Occurs:"Object reference not set to an instance of an object."
-
Hi, I met problem when run my application. I select a supplier from the combox. First time when I selected, the SupplierName and SupShortName displayed out nicely in the textbox. However, when I select another Supplier from the same combox, it pop out this error to me: "Object reference not set to an instance of an object." Below is the attached code. Private Sub Main_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Dim drApSupplier As SqlDataReader Dim cmdApSupplier As SqlCommand cmdApSupplier = New SqlCommand("LookupApSupplier", SqlConnection1) cmdApSupplier.CommandType = CommandType.StoredProcedure SqlConnection1.Open() drApSupplier = cmdApSupplier.ExecuteReader() While (drApSupplier.Read()) lstApSupplier.Items.Add(drApSupplier("Supplier")) End While lstApSupplier.Items.Add("Please Select Supplier") lstApSupplier.SelectedIndex = lstApSupplier.Items.Count - 1 End Sub Private Sub lstApSupplier_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lstApSupplier.SelectedIndexChanged Dim strApSupplier As String = Trim(CStr(lstApSupplier.SelectedItem)) Dim drApSupplier As SqlDataReader If Not (strApSupplier) = "Please Select Supplier" Then Try Dim cmdApSupplier As SqlCommand cmdApSupplier = New SqlCommand("LookupSupplier", SqlConnection2) cmdApSupplier.CommandType = CommandType.StoredProcedure Dim sqlParam As New SqlParameter sqlParam = cmdApSupplier.Parameters.Add("@Supplier", SqlDbType.Char, 7) sqlParam.Value = strApSupplier SqlConnection2.Open() drApSupplier = cmdApSupplier.ExecuteReader() drApSupplier.Read() txtSupplierName.Text = Trim(CStr(drApSupplier("SupplierName"))) txtSupShortName.Text = Trim(CStr(drApSupplier("SupShortName"))) Catch ex As Exception MessageBox.Show(ex.Message & " " & ex.StackTrace & " " & ex.Source) Finally drApSupplier = Nothing SqlConnection2 = Nothing End Try Else Exit Sub End If End Sub Please help. Thanks in advance, Chiari
-
Hi, I met problem when run my application. I select a supplier from the combox. First time when I selected, the SupplierName and SupShortName displayed out nicely in the textbox. However, when I select another Supplier from the same combox, it pop out this error to me: "Object reference not set to an instance of an object." Below is the attached code. Private Sub Main_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Dim drApSupplier As SqlDataReader Dim cmdApSupplier As SqlCommand cmdApSupplier = New SqlCommand("LookupApSupplier", SqlConnection1) cmdApSupplier.CommandType = CommandType.StoredProcedure SqlConnection1.Open() drApSupplier = cmdApSupplier.ExecuteReader() While (drApSupplier.Read()) lstApSupplier.Items.Add(drApSupplier("Supplier")) End While lstApSupplier.Items.Add("Please Select Supplier") lstApSupplier.SelectedIndex = lstApSupplier.Items.Count - 1 End Sub Private Sub lstApSupplier_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lstApSupplier.SelectedIndexChanged Dim strApSupplier As String = Trim(CStr(lstApSupplier.SelectedItem)) Dim drApSupplier As SqlDataReader If Not (strApSupplier) = "Please Select Supplier" Then Try Dim cmdApSupplier As SqlCommand cmdApSupplier = New SqlCommand("LookupSupplier", SqlConnection2) cmdApSupplier.CommandType = CommandType.StoredProcedure Dim sqlParam As New SqlParameter sqlParam = cmdApSupplier.Parameters.Add("@Supplier", SqlDbType.Char, 7) sqlParam.Value = strApSupplier SqlConnection2.Open() drApSupplier = cmdApSupplier.ExecuteReader() drApSupplier.Read() txtSupplierName.Text = Trim(CStr(drApSupplier("SupplierName"))) txtSupShortName.Text = Trim(CStr(drApSupplier("SupShortName"))) Catch ex As Exception MessageBox.Show(ex.Message & " " & ex.StackTrace & " " & ex.Source) Finally drApSupplier = Nothing SqlConnection2 = Nothing End Try Else Exit Sub End If End Sub Please help. Thanks in advance, Chiari
Put If(!IsPostback) condition in page load event.So that when selectedindex changed event occurs it doesn't load the dropdown box again.