0

我有一个搜索按钮,当我单击它时,会显示结果,但它有一个消息框,上面写着“从字符串转换为整数类型无效”。这是我的搜索代码:

Private Sub btnSearchExpenditure_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSearchExpenditure.Click
  Try
    If txtSearchExpenditure.Text = "" Then
        MsgBox("Please fill up search field!", MsgBoxStyle.Exclamation)
        Exit Sub
        Else
        modGlobalFunctions.Connection.Close()
        modGlobalFunctions.connectDatabase()
        RecordCount = modGlobalFunctions.loadDataGrid("SELECT * " & _
                      "FROM bims_expenditure " & _
                      "WHERE type_id='" & TypeId & "' " & _
                      "AND (code LIKE '%" & txtSearchExpenditure.Text & "%' " & _
                      "OR object_name LIKE '%" & txtSearchExpenditure.Text & "%' ) " & _
                      "ORDER BY code ASC", dgvExpenditureItems)
                 If RecordCount = 0 Then
                    txtCodeExpenditure.Clear()
                    txtNameExpenditure.Clear()
                    MsgBox("No records found!", MsgBoxStyle.Exclamation)
                End If
                With dgvExpenditureItems
                    .Font = gridFont.Font
                    .Columns(1).Visible = False
                    .Columns(4).Visible = False
                    .Columns(5).Visible = False
                    .Columns(6).Visible = False
                    .Columns(7).Visible = False

                    .Columns(0).HeaderText = ""
                    .Columns(2).HeaderText = "Code"
                    .Columns(3).HeaderText = "Name"

                    .Columns(0).Width = 0
                    .Columns(2).Width = 150
                    .Columns(3).Width = 540
                End With
                frmMain.recordsLabel.Visible = True
                frmMain.tssRecords.Text = RecordCount
                modGlobalFunctions.Connection.Close()
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message, "BIMS", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
    End Sub

我不知道怎么了。有什么建议么?提前致谢。上帝保佑!

4

1 回答 1

0

尝试转换RecordCountString这样:

frmMain.tssRecords.Text = Convert.ToString(RecordCount)

正如你所说type_id的表中的列bims_expenditureINT,所以你需要''从值中删除type_id(例如你的查询应该是WHERE type_id=" & TypeId & " "而不是WHERE type_id='" & TypeId & "' ")。

试试下面的代码:

Private Sub btnSearchExpenditure_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSearchExpenditure.Click
  Try
    If txtSearchExpenditure.Text = "" Then
        MsgBox("Please fill up search field!", MsgBoxStyle.Exclamation)
        Exit Sub
        Else
        modGlobalFunctions.Connection.Close()
        modGlobalFunctions.connectDatabase()
        RecordCount = modGlobalFunctions.loadDataGrid("SELECT * " & _
                      "FROM bims_expenditure " & _
                      "WHERE type_id=" & TypeId & " " & _
                      "AND (code LIKE '%" & txtSearchExpenditure.Text & "%' " & _
                      "OR object_name LIKE '%" & txtSearchExpenditure.Text & "%' ) " & _
                      "ORDER BY code ASC", dgvExpenditureItems)
                 If RecordCount = 0 Then
                    txtCodeExpenditure.Clear()
                    txtNameExpenditure.Clear()
                    MsgBox("No records found!", MsgBoxStyle.Exclamation)
                End If
                With dgvExpenditureItems
                    .Font = gridFont.Font
                    .Columns(1).Visible = False
                    .Columns(4).Visible = False
                    .Columns(5).Visible = False
                    .Columns(6).Visible = False
                    .Columns(7).Visible = False

                    .Columns(0).HeaderText = ""
                    .Columns(2).HeaderText = "Code"
                    .Columns(3).HeaderText = "Name"

                    .Columns(0).Width = 0
                    .Columns(2).Width = 150
                    .Columns(3).Width = 540
                End With
                frmMain.recordsLabel.Visible = True
                frmMain.tssRecords.Text = Convert.ToString(RecordCount)
                modGlobalFunctions.Connection.Close()
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message, "BIMS", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
    End Sub
于 2012-08-07T04:31:14.643 回答