“对象引用未设置为对象的实例"退出程序时 [英] "Object reference not set to an instance of an object" when exiting program

查看:75
本文介绍了“对象引用未设置为对象的实例"退出程序时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的编码员,
请就错误对象引用未设置为对象的实例"向我提供帮助
源代码是:

Dear Coders,
Please Help me regarding the error "Object reference not set to an instance of an object"
The Source code is:

  1  Imports System.Data.SqlClient
  2  
  3  Public Class Form3
  4      Dim con As New SqlConnection
  5      Dim myconstring As String = "Data Source=ETA-TESTSRVR;Initial Catalog=Procurement;Integrated Security=True"
  6  
  7      Private Sub E2BindingNavigatorSaveItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles E2BindingNavigatorSaveItem.Click
  8  
  9          Me.Validate()
 10          Me.E2BindingSource.EndEdit()
 11          Me.TableAdapterManager.UpdateAll(Me.ProcurementDataSet)
 12  
 13      End Sub
 14  
 15      Private Sub Form3_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
 16  
 17          'TODO: This line of code loads data into the 'ProcurementDataSet.E1' table. You can move, or remove it, as needed.
 18          Me.E1TableAdapter.FillBy1(Me.ProcurementDataSet.E1)
 19  
 20          'TODO: This line of code loads data into the 'ProcurementDataSet.E2' table. You can move, or remove it, as needed.
 21          Me.E2TableAdapter.Fill(Me.ProcurementDataSet.E2)
 22  
 23      End Sub
 24  
 25      Private Sub Mat_Sub_NoComboBox_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Mat_Sub_NoComboBox.SelectedIndexChanged
 26  
 27          con.ConnectionString = myconstring
 28  
 29          'Automated Serial Number
 30          Dim da As New SqlDataAdapter("SELECT max(sl_no)+1 as max FROM e1 where project_id='" + Project_IDTextBox.Text + "'", con)
 31          Dim dt As New DataTable
 32          da.Fill(dt)
 33          Sl_NoTextBox.Text = dt.Rows(0).Item("max").ToString
 34          If Sl_NoTextBox.Text = "" Then
 35              Sl_NoTextBox.Text = 1
 36          End If
 37  
 38          'Fetching Data to Textbox
 39          Dim da1 As New SqlDataAdapter("SELECT Project_ID,Trade,Fix,Material_Description FROM Mat_e1_prj where mat_sub_no='" + Mat_Sub_NoComboBox.SelectedValue.ToString + "'", con)
 40          Dim dt1 As New DataTable
 41          da1.Fill(dt1)
 42          Project_IDTextBox.Text = dt1.Rows(0).Item("Project_ID").ToString
 43          TradeTextBox.Text = dt1.Rows(0).Item("Trade").ToString
 44          FixTextBox.Text = dt1.Rows(0).Item("fix").ToString
 45          DescriptionTextBox.Text = dt1.Rows(0).Item("Material_Description").ToString
 46          
 47      End Sub
 48  
 49  End Class

推荐答案

啊!
那么您需要查看Mat_Sub_NoComboBox.SelectedValue.ToString-由于它位于SelectedIndexChanged methoid中,最可能的原因是未选择任何内容.检查方法顶部的Mat_Sub_NoComboBox.SelectedValue值-如果它是Nothing,则您需要忽略该事件或执行其他操作!
Ah!
then you need to look at Mat_Sub_NoComboBox.SelectedValue.ToString - since it is in the SelectedIndexChanged methoid, the most likely reason is that there is nothing selected. Check the value of Mat_Sub_NoComboBox.SelectedValue at the top of your method - if it is Nothing then you either need to ignore the event, or do something different!


这篇关于“对象引用未设置为对象的实例"退出程序时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆