读书人

一个应用程序老是报错希望帮忙看看

发布时间: 2012-02-20 21:18:23 作者: rapoo

一个应用程序老是报错,希望各位大哥帮忙看看。
创建ds
Public Function ControlData() As DataSet
Try
ControlData = New DataSet
Dim ControlTable As DataTable = ControlData.Tables.Add("OSBTable")
Dim ControlDataColum As DataColumn
ControlDataColum = New DataColumn("SBID", Type.GetType("System.Guid"))
ControlDataColum.AllowDBNull = False
ControlTable.Columns.Add(ControlDataColum)
ControlDataColum = New DataColumn("ProdName", Type.GetType("System.String"))
ControlDataColum.AllowDBNull = False
ControlTable.Columns.Add(ControlDataColum)
ControlDataColum = New DataColumn("ProdID", Type.GetType("System.Guid"))
ControlDataColum.AllowDBNull = False
ControlTable.Columns.Add(ControlDataColum)
ControlDataColum = New DataColumn("SBDate", Type.GetType("System.DateTime"))
ControlDataColum.AllowDBNull = False
ControlTable.Columns.Add(ControlDataColum)
ControlDataColum = New DataColumn("UserID", Type.GetType("System.Guid"))
ControlDataColum.AllowDBNull = False
ControlTable.Columns.Add(ControlDataColum)
ControlDataColum = New DataColumn("UserName", Type.GetType("System.String"))
ControlDataColum.AllowDBNull = False
ControlTable.Columns.Add(ControlDataColum)
ControlDataColum = New DataColumn("ProdPrice", Type.GetType("System.Decimal"))
ControlDataColum.AllowDBNull = False
ControlTable.Columns.Add(ControlDataColum)
ControlDataColum = New DataColumn("ProdType", Type.GetType("System.String"))
ControlDataColum.AllowDBNull = False
ControlTable.Columns.Add(ControlDataColum)
ControlDataColum = New DataColumn("SBCode", Type.GetType("System.String"))
ControlDataColum.AllowDBNull = False
ControlTable.Columns.Add(ControlDataColum)
ControlDataColum = New DataColumn("ProdAUT", Type.GetType("System.Decimal"))
ControlDataColum.AllowDBNull = False
ControlTable.Columns.Add(ControlDataColum)
ControlDataColum = New DataColumn("ProdNuber", Type.GetType("System.UInt32"))
ControlDataColum.AllowDBNull = False
ControlTable.Columns.Add(ControlDataColum)
Return ControlData
Catch ex As Exception
Throw New System.Exception(ex.Message, _
ex.InnerException)
End Try
End Function

给ds附值
Public Function SBIDControl() As DataSet
Try
Dim SBIDControlDS As New DataSet
call ControlData()
SBIDControlDS = ControlData()
Dim n As Integer = 0
For n = 0 To DataGridView1.Rows.Count - 1
Dim SCControlRow As DataRow = SBIDControlDS.Tables("OSBTable").NewRow
SCControlRow.Item("SBID") = Guid.NewGuid
SCControlRow.Item("ProdName") = DataGridView1.Rows(n).Cells(ProdNameLC).Value
SCControlRow.Item("ProdID") = Guid.NewGuid
SCControlRow.Item("ProdType") = DataGridView1.Rows(n).Cells(ProdTypeLC).Value
SCControlRow.Item("ProdNuber") = Convert.ToDecimal(DataGridView1.Rows(n).Cells(ProdNuberLC).FormattedValue)


SCControlRow.Item("ProdPrice") = DataGridView1.Rows(n).Cells(ProdPriceLC).Value
SCControlRow.Item("ProdAUT") = Convert.ToDecimal(DataGridView1.Rows(n).Cells(ProdAUTLC).FormattedValue)
SCControlRow.Item("UserID") = Guid.NewGuid
SCControlRow.Item("UserName") = "TestName"
SCControlRow.Item("SBdate") = Date.Now.Date
SCControlRow.Item("SBCode") = "TEST1001"
SBIDControlDS.Tables("SCTable").Rows.Add(SCControlRow)
Next
Return SBIDControlDS
Return SBIDControl
Catch ex As Exception
Throw New System.Exception(ex.Message, _
ex.InnerException)
End Try
End Function

调用ds
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
DataGridView1.EndEdit()
Call Me.SBIDControl()
'此处添加验证代码
Dim Count As Integer = Me.SBIDControl.Tables("OSBTable").Rows.Count - 1
Dim SBCT As New OSBTableContrl
SBCT.AddOSBtable(Me.SBIDControl, Count)
End Sub

一运行就报"未将对象引用设置到对象的实例。"这个错误,我查了下很多人都说是ds附值的时候出问题,但是我又找不到是哪个位置出的错,小弟是个菜鸟希望各位大哥帮帮忙。

[解决办法]
单步调试找找

[解决办法]
找了半天,没看见楼主怎么把datatable加入dataset
ControlData.Tables.Add( datatable )
[解决办法]
设断点分步调试,一步步找究竟错在哪里,别着急!

读书人网 >VB Dotnet

热点推荐