这段代码中的错误在哪里 [英] Where is the wrong in this code

查看:92
本文介绍了这段代码中的错误在哪里的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好

我有一个带有添加和编辑功能的表单。当我打开表单时,添加功能只能存储一次,当我按下保存按钮时我会出错。

这是代码:



Hi guys
I have a form with add and edit functions . When I open the form in add function can only store once the second time when I push the save button I get wrong.
Here is the code:

Imports System.Data.SqlClient

Public Class frmAddEdit
    Public frUpdate As Boolean
    
    Private Sub ButtonCancel_Click(ByVal sender As System.Object, ByVal e As EventArgs) Handles ButtonCancel.Click
        Close()
    End Sub

    Private Sub ButtonSave_Click(ByVal sender As System.Object, ByVal e As EventArgs) Handles ButtonSave.Click
        If txtLname.Text = "" Or txtFname.Text = "" Or txtFtname.Text = "" Or _
            txtBdate.Text = "" Or txtCity.Text = "" Or txtCompany.Text = "" Then
            MsgBox("....", MsgBoxStyle.Critical, "...")
        End If
        If frUpdate = True Then
            cn.Open()
            strCon = "UPDATE Staff SET Lastname = @Lastname, Firstname = @Firstname, Fathername = @Fathername, Birthday = @Birthday, City = @City, Phone = @Phone, Cellphone = @Cellphone" & _
                ",ASM = @ASM, Company = @Company, Team = @Team, Class = @Class, Release = @Release WHERE StaffID = @StaffID"
            Dim cmd As SqlCommand = New SqlCommand(strCon, cn)
            With cmd.Parameters
                .Add(New SqlParameter("@Lastname", txtLname.Text))
                .Add(New SqlParameter("@Firstname", txtFname.Text))
                .Add(New SqlParameter("@Fathername", txtFtname.Text))
                .Add(New SqlParameter("@Birthday", txtBdate.Text))
                .Add(New SqlParameter("@City", txtCity.Text))
                .Add(New SqlParameter("@Phone", txtPhone.Text))
                .Add(New SqlParameter("@Cellphone", txtCphone.Text))
                .Add(New SqlParameter("@ASM", txtASM.Text))
                .Add(New SqlParameter("@Company", txtCompany.Text))
                .Add(New SqlParameter("@Team", txtTeam.Text))
                .Add(New SqlParameter("@Class", txtClass.Text))
                .Add(New SqlParameter("@Release", txtRelease.Text))
                .Add(New SqlParameter("@StaffID", txtAA.Text))
            End With
            cmd.ExecuteNonQuery()
            cn.Close()
            MsgBox("...........", MsgBoxStyle.Information, "............")
            frmList.cmdRefresh.PerformClick()
            Exit Sub
        Else
            cn.Open()
            strCon = "INSERT INTO Staff(Lastname,Firstname,Fathername,Birthday,City,Phone,Cellphone,ASM,Company,Team,Class,Release)" & _
                                 "VALUES (@Lastname,@Firstname,@Fathername,@Birthday,@City,@Phone,@Cellphone,@ASM,@Company,@Team,@Class,@Release)"
            Dim cmd As SqlCommand = New SqlCommand(strCon, cn)
            With cmd.Parameters
                .Add(New SqlParameter("@Lastname", txtLname.Text))
                .Add(New SqlParameter("@Firstname", txtFname.Text))
                .Add(New SqlParameter("@Fathername", txtFtname.Text))
                .Add(New SqlParameter("@Birthday", txtBdate.Text))
                .Add(New SqlParameter("@City", txtCity.Text))
                .Add(New SqlParameter("@Phone", txtPhone.Text))
                .Add(New SqlParameter("@Cellphone", txtCphone.Text))
                .Add(New SqlParameter("@ASM", txtASM.Text))
                .Add(New SqlParameter("@Company", txtCompany.Text))
                .Add(New SqlParameter("@Team", txtTeam.Text))
                .Add(New SqlParameter("@Class", txtClass.Text))
                .Add(New SqlParameter("@Release", txtRelease.Text))
            End With
            cmd.ExecuteNonQuery()
            cn.Close()
            cn = Nothing
            MsgBox("..............", MsgBoxStyle.Information, "............")
            Call LoadID()
        End If
        frmList.cmdRefresh.PerformClick()
        Exit Sub
    End Sub

    Private Sub frmAddEdit_FormClosed(ByVal sender As Object, ByVal e As FormClosedEventArgs) Handles Me.FormClosed
        frUpdate = False
        Me.Dispose()
    End Sub

    Private Sub frmAddEdit_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Call LoadID()
    End Sub

    Private Sub LoadID()
        If frUpdate = False Then
            txtAA.Text = ""
            txtLname.Text = ""
            txtFname.Text = ""
            txtFtname.Text = ""
            txtBdate.Text = ""
            txtCity.Text = ""
            txtPhone.Text = ""
            txtCphone.Text = ""
            txtASM.Text = ""
            txtCompany.Text = ""
            txtTeam.Text = ""
            txtClass.Text = ""
            txtRelease.Text = ""
            IsConnected("Select * from Staff", False)
        End If
    End Sub
End Class





SHOUTING已删除 - OriginalGriff [/ edit]



[edit]SHOUTING removed - OriginalGriff[/edit]

推荐答案

取出行:

Take out the line:
Me.Dispose()

来自你的

frmAddEdit_FormClosed

处理程序 - 你不需要处理你的表单(并且不应该尝试使用Closed处理程序 - 该实例仍然有引用,可能是再次访问。



我很高兴唱出真正的问题不在这个代码中(除了我提到的Dispose)。检查显示此表单的代码 - 我怀疑您正在创建一个实例,在其上调用 ShowDialog ,然后在按钮为时再次尝试重新使用它按下。相反,每次按下按钮时创建一个新实例,调用 ShowDialog (和在完成时调用它如果你想要的,或使用使用阻止)

handler - you shouldn''t need to dispose your form (and shouldn''t try in the Closed handler - the instance still has references and could be accessed again.

I am guessing that the real problem is not in this code (apart from the Dispose I mentioned). Check the code where you show this form - I suspect that you are creating a single instance, calling ShowDialog on it, and then trying to re-use it again when the button is pressed. Instead, create a new instance each time the button is pressed, call ShowDialog (and Dispose it when you are finished if you want to, or use a using block instead)


这篇关于这段代码中的错误在哪里的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆