none
想请教有关message box出现fail message,用户不能save data成功的问题 RRS feed

  • 问题

  • 我正在篇写一个有关textbox的代码,如果 textbox值是single的话就会出现fail的 error message并且无法save 完成(insert into)的代码

    我遇到的问题是当user收到error message信息后,user还是能save data 成功。

    以下是我的代码

    检查textbox是否是single 的值

    Private Sub textbox_control()
            Dim a As Integer
            a = CInt((TextBox7.Text))
            If CSng(TextBox7.Text) = a Then
                MessageBox.Show("PASS")

    RETURN

            Else

                MessageBox.Show("FAIL")
                TextBox1.Text = ""
                TextBox2.Text = ""
                TextBox3.Text = ""
                TextBox4.Text = ""

            End If
        End Sub

    以下是我的save 的代码

    If Not cnn.State = ConnectionState.Open Then
                cnn.Open()
            End If
            cmd.Connection = cnn

            If PictureBox1.Image IsNot Nothing Then
                Dim DataPic_Update As Byte() = ImageToBytes(PictureBox1.Image, ImageFormat.Png)
                cmd.CommandText = "insert into Table1(Picture, F1, F2, F3)" &
                                "values (@picture, @F1, @F2, @F3)"

                cmd.Parameters.AddWithValue("@picture", DataPic_Update)
            Else
                cmd.CommandText = "insert into Table1(F1, F2, F3)" &
                                "values (@F1, @F2, @F3)"
            End If

            textbox_control()

            cmd.Parameters.AddWithValue("@F1", Me.TextBox1.Text)
            cmd.Parameters.AddWithValue("@F2", Me.TextBox2.Text)
            cmd.Parameters.AddWithValue("@F3", Me.TextBox3.Text)


            cmd.ExecuteNonQuery()
            RefreshData()
            MessageBox.Show("Record Is Added")

            Me.Hide()
            Dim frm = New Form5
            frm.Show()

        End Sub

    2020年5月30日 12:25

答案

  • Hi christing,

    考虑增加一个 bool变量用来控制是否执行save进database的操作。

        Dim control = False
        Private Sub textbox_control()
            Dim a As Integer
            a = CInt((TextBox7.Text))
            If CSng(TextBox7.Text) = a Then
                MessageBox.Show("PASS")
            control = True
            Else
    
                MessageBox.Show("FAIL")
                TextBox1.Text = ""
                TextBox2.Text = ""
                TextBox3.Text = ""
                TextBox4.Text = ""
                control = False
            End If
        End Sub
    
            'save时
            textbox_control()
            If control Then
                If Not cnn.State = ConnectionState.Open Then
                    cnn.Open()
                End If
                ...
            End If

    希望能够帮助你解决问题。

    Best Regards,

    Xingyu Zhao


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.



    2020年6月1日 3:03
    版主