Click here to Skip to main content
12,629,804 members (33,007 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: VB VB.NET
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]
Posted 5-Jan-13 2:46am
jomachi1.7K
Updated 5-Jan-13 3:02am
v3
Comments
Richard MacCutchan 5-Jan-13 7:50am
   
You need to give more details of exactly what happens. Or, you could step through your code with the debugger to try and see what is happening.
OriginalGriff 5-Jan-13 8:02am
   
DON'T SHOUT. Using all capitals is considered shouting on the internet, and rude (using all lower case is considered childish). Use proper capitalisation if you want to be taken seriously.
jomachi 5-Jan-13 8:15am
   
It gives me that error

cn.open()
Not set object reference to an object instance.
ShivKrSingh 5-Jan-13 8:19am
   
your connection string is not seems to be ok..
jomachi 5-Jan-13 8:23am
   
My friend OriginalGriff probably this the problem can you tell me how
can i fix it

1 solution

Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

Take out the line:
Me.Dispose()
from your
frmAddEdit_FormClosed
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)
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web02 | 2.8.161205.3 | Last Updated 5 Jan 2013
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100