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
[edit]SHOUTING removed - OriginalGriff[/edit]