2014-02-13 187 views
0

我有一个错误,无法删除(在我已经复制的代码的末尾)。我读了它,并且明白了说的是什么,但是我在该行中使用的两个对象既是实例化的,也是之前使用的。我使用了断点,在我看来,它们都有值,它们与null不同。例如,修改或删除操作就像他们应该执行的一样(我没有在这里复制它们)。你可以帮我吗。谢谢!未将对象引用设置为对象的实例VB

Public Class Form1 
    Dim dataSet As DataSet 
    Dim dataAdapterStudenti As New System.Data.SqlClient.SqlDataAdapter 
    Dim dataAdapterSectii As New SqlClient.SqlDataAdapter 

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load 
     Try 
      con.Open() 
      dataSet = New DataSet 

      Dim insertStudenti As New SqlClient.SqlCommand 
      insertStudenti.CommandType = CommandType.Text 
      insertStudenti.CommandText = "insert into studenti (cods,grupa,nume,datan,nrmatricol) values (@cods,@grupa,@nume,@datan,@nrmatricol)" 
      insertStudenti.Connection = con 
      insertStudenti.Parameters.Clear() 
      p = insertStudenti.Parameters.Add("@cods", SqlDbType.Int, 4, "cods") 
      p.SourceVersion = DataRowVersion.Current 
      p = insertStudenti.Parameters.Add("@nrmatricol", SqlDbType.Int, 4, "nrmatricol") 
      p.SourceVersion = DataRowVersion.Current 
      p = insertStudenti.Parameters.Add("@nume", SqlDbType.VarChar, 40, "nume") 
      p.SourceVersion = DataRowVersion.Current 
      p = insertStudenti.Parameters.Add("@grupa", SqlDbType.Int, 4, "grupa") 
      p.SourceVersion = DataRowVersion.Current 
      p = insertStudenti.Parameters.Add("@datan", SqlDbType.DateTime, 4, "datan") 
      p.SourceVersion = DataRowVersion.Current 
      dataAdapterStudenti.InsertCommand = insertStudenti 

      dataAdapterStudenti.Fill(DataSet, "studenti") 
      DataSet.Tables("studenti").Constraints.Clear() 
      DataSet.Tables("studenti").Constraints.Add("PK_nrmatricol", DataSet.Tables("studenti").Columns("nrmatricol"), True) 

     Catch ex As Exception 
      MessageBox.Show(ex.Message) 
     End Try 
    End Sub 

    Private Sub btnSave_Click(sender As Object, e As EventArgs) Handles btnSave.Click 
     If (Verifica() And EsteNrMatricolUnic()) Then 
      Dim dr As DataRow 
      dr = dataSet.Tables("studenti").NewRow() 
      dr("cods") = CInt(txtCodS.Text) 
      dr("nrmatricol") = CInt(txtNrMatricol.Text) 
      dr("nume") = txtNume.Text 
      dr("grupa") = txtGrupa.Text 
      dr("datan") = pickerDataNasterii.Value 
      Try 
       dataSet.Tables("stdenti").Rows.Add(dr) 'here is the error 
       dataAdapterStudenti.Update(dataSet, "studenti") 
      Catch ex As Exception 
       MessageBox.Show(ex.Message) 
      End Try 
     End If 
    End Sub 
End Class 

回答

2

我怀疑该错误的原因是一个简单的拼写错误(stdenti与studenti):

dataSet.Tables("studenti").Rows.Add(dr) 'here is the error 

应该工作。如果您向dataSet.Tables("invalidname")提供了无效的表名,则它将返回空值(在VB.NET中为Nothing)。之后致电Rows会导致您遇到的例外情况。

这就是说,NullReferenceException指出你试图在一个具有null(Nothing)值的引用上调用成员(方法,属性,...)。有关详细信息,请参阅此post

+0

我正在看它过去三天。这太疯狂了 :)))))) – Tamango

相关问题