Hi every 1 i am trying to register an employee but for some reason my record is inserted twice. if i enter all details of employee and when i click save..after that if i chk my employee list i could see same record entered twice.here is my code.
Private Sub btnSaveEmp_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSaveEmp.Click

Dim Con As New SqlConnection("Data Source=SAAD-PC\SQLEXPRESS;Initial Catalog=photoshoot;Integrated Security=True")

Dim cmd As New SqlCommand
Con.Open()
cmd.Connection = Con
cmd.CommandText = "INSERT INTO staff(FirstName,LastName,Address,Post_Code,Telephone,Email,UserName,Password,Confirm_Password,UserType) VALUES(@FirstName,@LastName,@Address,@Post_Code,@Telephone,@Email,@UserName,@Password,@Confirm_Password,@UserType)"
cmd.Parameters.Add("@FirstName", SqlDbType.VarChar).Value = txtEmpFirstName.Text
cmd.Parameters.Add("@LastName", SqlDbType.VarChar).Value = txtEmpLastName.Text
cmd.Parameters.Add("@Address", SqlDbType.VarChar).Value = txtEmpAddress.Text
cmd.Parameters.Add("@Post_Code", SqlDbType.VarChar).Value = txtEmpPostCode.Text
cmd.Parameters.Add("@Telephone", SqlDbType.Int).Value = txtEmpTelephone.Text
cmd.Parameters.Add("@Email", SqlDbType.VarChar).Value = txtEmpEmail.Text
cmd.Parameters.Add("@UserName", SqlDbType.VarChar).Value = txtEmpUsername.Text
cmd.Parameters.Add("@Password", SqlDbType.VarChar).Value = txtPassword.Text
cmd.Parameters.Add("@Confirm_Password", SqlDbType.VarChar).Value = txtEmpConfPass.Text
cmd.Parameters.Add("@UserType", SqlDbType.VarChar).Value = cmbEmpUserTypeReg.Text
If txtPassword.Text = txtEmpConfPass.Text Then
MsgBox("Record Successfully Saved")

Else
MsgBox("Both Password Fields Must Match Please Enter Password Again", MsgBoxStyle.Critical)
End If


cmd.ExecuteNonQuery()

Con.Close()
End Sub

Also 1 more thing i would like to ask is after i registe employee when i click the employee list which shows me data in a gridview in another form. i cant c the employee which i just added i have complete stop the project and re run it than i can see the employee list. i also used refresh in the form load event but still no luck

In your code i can't see any possibility to add record twice. So i think you must post your full code for all the controls your use in your form. Just only this forms code... Ok..

hello !
as king said that there is no error in your code , the possibility is this that you are using this code in a way that it is executed twice thats why you got two records inserted. and the reason that you insert the record and the most recent inserted record is not shown in grid , is that you are not updating your datasource of your grid , the better way is to make sub() of your grid populating code , and call it just after your insertion code.or you can use binding source , and use this property

bindingsource.movelast()

hope this will gives you an idea .

Regards

hi waqas is it possible if you can insert your code in my code for updating the data gridview. bcz i have tried but its not helping. i also use the refresh methods but of no use..could u plz help thnx

i am refreshing the form while it gets loaded

Private Sub EmployeeSearch_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Me.Refresh()
'TODO: This line of code loads data into the 'EmpSearchDataSet1.staff' table. You can move, or remove it, as needed.
Me.StaffTableAdapter.Fill(Me.EmpSearchDataSet1.staff)

End Sub

hello !
use this sub

Sub myshow()
        Try
            Dim mycon As New SqlConnection("connection string")
            mycon.Open()
            Dim da As New SqlDataAdapter("select * from table", mycon)
            Dim dt As New DataTable
            da.Fill(dt)
            datagrid1.datasource = dt
            mycon.Close()
        Catch ex As Exception
            MsgBox(Err.Description)
        End Try
    End Sub

this is simple code to populate grid , call this sub after your insert sub , at the insert button click , this will reload your grid , the basic logic is that , after insert a new record , you have to get new records from your db into you datatable so that we can see newly inserted record. hope this will solve your prob .

Regards

thanks alot it solved the problem but im having 1 more trouble with the same page. below is my code. if i done enter anything in the fields and click the save button i get an error message(failed to convert the parameter of string to In32). if i fill all fields than it works fine..plz help

Private Sub btnSaveEmp_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSaveEmp.Click

Dim Con As New SqlConnection("Data Source=SAAD-PC\SQLEXPRESS;Initial Catalog=photoshoot;Integrated Security=True")

Dim cmd As New SqlCommand
Con.Open()
cmd.Connection = Con
cmd.CommandText = "INSERT INTO staff(FirstName,LastName,Address,Post_Code,Telephone,Email,UserName,Password,Confirm_Password,UserType) VALUES(@FirstName,@LastName,@Address,@Post_Code,@Telephone,@Email,@UserName,@Password,@Confirm_Password,@UserType)"
cmd.Parameters.Add("@FirstName", SqlDbType.VarChar).Value = txtEmpFirstName.Text

cmd.Parameters.Add("@LastName", SqlDbType.VarChar).Value = txtEmpLastName.Text
cmd.Parameters.Add("@Address", SqlDbType.VarChar).Value = txtEmpAddress.Text
cmd.Parameters.Add("@Post_Code", SqlDbType.VarChar).Value = txtEmpPostCode.Text
cmd.Parameters.Add("@Telephone", SqlDbType.Int).Value = txtEmpTelephone.Text
cmd.Parameters.Add("@Email", SqlDbType.VarChar).Value = txtEmpEmail.Text
cmd.Parameters.Add("@UserName", SqlDbType.VarChar).Value = txtEmpUsername.Text
cmd.Parameters.Add("@Password", SqlDbType.VarChar).Value = txtPassword.Text
cmd.Parameters.Add("@Confirm_Password", SqlDbType.VarChar).Value = txtEmpConfPass.Text
cmd.Parameters.Add("@UserType", SqlDbType.VarChar).Value = cmbEmpUserTypeReg.Text
If txtEmpFirstName.Text = "" Or txtEmpLastName.Text = "" Or txtEmpAddress.Text = "" Or txtEmpPostCode.Text = "" Or txtEmpTelephone.Text = "" Or txtEmpEmail.Text = "" Or txtEmpUsername.Text = "" Or txtPassword.Text = "" Or txtEmpConfPass.Text = "" Then
MsgBox("Please Fill All Required Fields")
ElseIf txtPassword.Text = txtEmpConfPass.Text Then
MsgBox("Record Successfully Saved")
ElseIf MsgBox("Both Password Fields Must Match Please Enter Password Again", MsgBoxStyle.Critical) Then


End If


cmd.ExecuteNonQuery()

Con.Close()
End Sub

please change this line

cmd.Parameters.Add("@Telephone", SqlDbType.Int).Value = txtEmpTelephone.Text

with this

cmd.Parameters.Add("@Telephone", SqlDbType.Int).Value = val( txtEmpTelephone.Text)

hope this will solve your prob , if yes then please mark this thread solved.

This question has already been answered. Start a new discussion instead.