连接必须有效并打开

问题描述 投票:0回答:1

这里是我的真实代码。调试时出现错误,连接必须有效并打开。

[C0行的错误

testNamebox.Text = cmd.ExecuteScalar()
vb.net
1个回答
1
投票

很难从您的问题中确切指出您所犯的错误,但这是一个猜测。

在您的代码中,您有:

Private Sub ButtonX1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
        Try
            Dim totalResultEng As Int16 = 0
            Dim totalResultFr As Int16 = 0
            Dim time As Int16 = 0
            frmSavingData.Show()
            For i As Integer = 0 To Me.DataGridView1.RowCount - 1
                If Me.DataGridView1.Rows(i).Cells(2).Value = "Polynucléaires neutrophiles" Or Me.DataGridView1.Rows(i).Cells(2).Value = "Polynucléaires éosinophiles" Or _
                    Me.DataGridView1.Rows(i).Cells(2).Value = "Polynucléaires basophiles" Or Me.DataGridView1.Rows(i).Cells(2).Value = "Lymphocytes" Or _
                   Me.DataGridView1.Rows(i).Cells(2).Value = "Monocytes" Then
                    totalResultEng = totalResultEng + Val(Me.DataGridView1.Rows(i).Cells(4).Value)
                    totalResultFr = totalResultFr + Val(Me.DataGridView1.Rows(i).Cells(3).Value)
                    time = time + 1
                End If
            Next
            If totalResultEng <> 100 And time = 5 Then
                MsgBox("Please Check English Resultn Of Test Hg !", MsgBoxStyle.OkOnly, "Total Result")
                Exit Sub
            End If
            If totalResultFr <> 100 And time = 5 Then
                MsgBox("Please Check French Result Of Test Hg !", MsgBoxStyle.OkOnly, "Total Result")
                Exit Sub
            End If
            Dim cmd As MySqlCommand, bold As Integer = 0, checkprint As Int16 = 0
            Dim testNamebox As New RichTextBox
            testNamebox.Width = 500
            testNamebox.Height = 50
            For i As Integer = 0 To Me.DataGridView1.RowCount - 1
                Dim fr_result As String
                Dim eng_result As String
                cmd = New MySqlCommand("select testName from Testdetail where testid =" & Me.DataGridView1.Rows(i).Cells(2).Tag, Setting.cn)
                ' cmd = New MySqlCommand("select testName from Testdetail where testid ='" & Me.DataGridView1.Rows(i).Cells(2).Tag & "'", Setting.cn)
                testNamebox.Text = cmd.ExecuteScalar()
                If testNamebox.Lines.GetUpperBound(0) > 0 Then  'control number of lines in RichTextBox
                    testNamebox.Text = Me.DataGridView1(3, i).Value.ToString
                    If testNamebox.Lines.GetUpperBound(0) > 0 Then
                        If IsNothing(Me.DataGridView1(4, i).Value) Then eng_result = "" Else eng_result = Me.DataGridView1(4, i).Value.ToString
                        If IsNothing(Me.DataGridView1(3, i).Value) Then fr_result = "" Else fr_result = Me.DataGridView1(3, i).Value.ToString
                    Else
                        If IsNothing(Me.DataGridView1(4, i).Value) Then eng_result = "" Else eng_result = "" + vbCrLf + Me.DataGridView1(4, i).Value.ToString
                        If IsNothing(Me.DataGridView1(3, i).Value) Then fr_result = "" Else fr_result = "" + vbCrLf + Me.DataGridView1(3, i).Value.ToString
                    End If
                Else
                    If IsNothing(Me.DataGridView1(4, i).Value) Then eng_result = "" Else eng_result = Me.DataGridView1(4, i).Value.ToString
                    If IsNothing(Me.DataGridView1(3, i).Value) Then fr_result = "" Else fr_result = Me.DataGridView1(3, i).Value.ToString
                End If

                If Me.DataGridView1(7, i).Value = True Then bold = 1
                If Me.DataGridView1(8, i).Value = True Then checkprint = 1
                cmd = New MySqlCommand("Update invoicedetail set EngResult='" & _
                                            eng_result.ToString.Replace("\", "\\").Replace("'", "''") & "', FrResult='" & fr_result.ToString.Replace("\", "\\").Replace("'", "''") & _
                                            "',Bold='" & bold & "',checkprint='" & checkprint & "' where testid='" & Me.DataGridView1.Rows(i).Cells(2).Tag & "' and invoiceid='" & Me.DataGridView1.Rows(i).Cells(0).Tag & "'", Setting.cn)
                cmd.ExecuteNonQuery()
                If Me.DataGridView1.Rows(i).Cells(0).Value <> "" And Me.DataGridView1.Rows(i).Cells(8).Value = False Then
                    cmd = New MySqlCommand("Update invoicedetail set checkprint=0 where  TestID = '" & Me.DataGridView1.Rows(i).Cells(2).Tag & "' and invoiceid='" & Me.DataGridView1.Rows(i).Cells(0).Tag & "'", Setting.cn)
                    cmd.ExecuteNonQuery()
                ElseIf Me.DataGridView1.Rows(i).Cells(0).Value <> "" And Me.DataGridView1.Rows(i).Cells(8).Value = True Then
                    cmd = New MySqlCommand("Update invoicedetail set checkprint=1 where  TestID = '" & Me.DataGridView1.Rows(i).Cells(2).Tag & "' and invoiceid='" & Me.DataGridView1.Rows(i).Cells(0).Tag & "'", Setting.cn)
                    cmd.ExecuteNonQuery()
                End If
                cmd.Dispose()
                bold = 0
                checkprint = 0
                frmSavingData.Refresh()
            Next
            cmd = New MySqlCommand("Update invoicedetail set  EngObservation=" & IIf(txtEnglishObservation.Text <> "", "'" & txtEnglishObservation.Rtf.Replace("'", "''").Replace("\", "\\") & "'", "NULL") & _
                                           ", FrObservation=" & IIf(txtFrenchObservation.Text <> "", "'" & txtFrenchObservation.Rtf.Replace("'", "''").Replace("\", "\\") & "'", "NULL") & _
                                           " where testid='" & TestIDforupdate & "' and invoiceid='" & InvoiceNo & "'", Setting.cn) 'TestID
            cmd.ExecuteNonQuery()
            cmd.Dispose()
            '' MsgBox("Save successed!")
            'Me.Close()
            frmSavingData.Dispose()
        Catch ex As Exception
            frmSavingData.Dispose()
            MsgBox(ex.Message)
        End Try
    End Sub

我假设Setting.cn是您的连接字符串。如果是这样,看来您永远不会打开或关闭连接。

尝试一下:

cmd = New MySqlCommand("select testName from Testdetail where testid =" & Me.DataGridView1.Rows(i).Cells(2).Tag, Setting.cn)
testNamebox.Text = cmd.ExecuteScalar()

0
投票

您能尝试这个吗?

select testName from Testdetail where testid ='" & Me.DataGridView1.Rows(i).Cells(2).Tag & "'", Setting.cn)

您的值在处理sql时应包含在单引号中

© www.soinside.com 2019 - 2024. All rights reserved.