我已经阅读了很多论坛。实际上,他们都没有回答我的问题。第二次运行该过程时会触发此问题。我第一次运行程序时没有问题
我从主菜单调用windows窗体:
Private Sub ProjectsToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles ProjectsToolStripMenuItem.Click
SqlStatement1 = "Select TOP(1) fiscalyear From " & ConfigTable & " Order By fiscalyear DESC"
FiscalYear = GetData(SqlStatement1, 0)
frmCPProjectInfo.ShowDialog()
End Sub
表单的frmCPProjectInfo在sub:
之后加载事件调用 SqlStatement = "Select p.projtitle, p.projectid, d.deptid, p.deptrank, p.account, SUBSTRING(p.account, 6, 4) As last4 From (Select dept01.*, f.funddesc, f.fund_num From (Select * From "
SqlStatement &= DepartmentsTable & " Where deptid In (Select deptid From " & WorkAreaTable & " Where userlogin = '" & CurrentUser & "')) dept01 LEFT OUTER JOIN " & FundsTable
SqlStatement &= " f On dept01.fund = f.fund) d LEFT OUTER JOIN " & ProjectInfoTable & " p On p.deptid = d.deptid Where p.FiscalYear = " & FiscalYear & " Order by deptid, projectid"
frmCPSelectProject.ShowDialog()
LoadAll()
cmdDeleteProject.Image = ListOfImages.Images(0)
ToolTip1.SetToolTip(cmdDeleteProject, "Delete Project")
在这里,LoadAll()显示项目。
在表格frmCPSelectProject下面加载事件调用sub:
Sub DisplayAll()
Dim sqlConn As New SqlConnection(connStringCIP)
Dim sqlCom As New SqlCommand
Dim drCIP As SqlDataReader
Dim dtCIP As DataTable = New DataTable
sqlConn.Open()
sqlCom = New SqlCommand(SqlStatement, sqlConn)
drCIP = sqlCom.ExecuteReader()
DataGridView1.DataSource = ""
dtCIP.Load(drCIP)
If dtCIP.Rows.Count = 0 Then
MessageBox.Show("There is no record to show")
NoRec = True
dtCIP.Dispose()
drCIP.Dispose()
sqlCom.Dispose()
sqlConn.Close()
sqlConn.Dispose()
Me.Close()
Exit Sub
End If
DataGridView1.DataSource = dtCIP
DataGridView1.Columns(0).Width = 300
DataGridView1.Columns(1).Width = 85
DataGridView1.Columns(2).Width = 85
DataGridView1.Columns(3).Width = 85
DataGridView1.Columns(4).Width = 140
DataGridView1.Columns(5).Width = 250
dtCIP.Dispose()
drCIP.Dispose()
sqlCom.Dispose()
sqlConn.Close()
sqlConn.Dispose()
End Sub
然后,我选择项目执行以下例程:
Private Sub cmdContinue_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdContinue.Click
SelectID(DataGridView1.CurrentCell.RowIndex)
End Sub
Sub SelectID(ByVal NewInd)
Dim DeptID As String
Dim DeptRank As String
ProjectID = DataGridView1.Rows(RowInd).Cells(1).Value
DeptID = Trim(Str(DataGridView1.Rows(RowInd).Cells(2).Value))
DeptRank = Trim(Str(DataGridView1.Rows(RowInd).Cells(3).Value))
Select Case Order
Case "projtitle"
SearchBy = "projtitle"
SearchItem = DataGridView1.Rows(RowInd).Cells(0).Value
Case "account"
SearchBy = "account"
SearchItem = DataGridView1.Rows(RowInd).Cells(4).Value
End Select
Me.Close()
End Sub
之后,例程LoadAll()(见上文)执行。
问题是,如果我关闭表格frmCPProjectInfo返回主菜单然后再次运行该过程,我收到一条错误消息:
未处理的类型' System.NullReferenceException'发生在System.Windows.Forms.dll中 附加信息:对象引用未设置为对象的实例。
程序停在线上 DataGridView1.Columns(0).Width = 300(参见上面的DisplayAll())。 有人能帮助我吗? 提前致谢