i've create a program that contain a 4 textbox and three action button. the textbox are used to fill user name, date of birth, hobbies and number. while the action button are submit upadte and clear...all of this are working perfectly..i also manage to store all the data entered by user in sql server 2000 db. the data also will display itself when the program started. the problem is now when i want to update the data i had to close te program first and start it back then the updated data will appear. what i want is the data will be outomatically updated when i click the update button. i really seek help here i hope my problem is clear enough...plzzzz oo want more thing i want to add a delete button that will outomatically delete the data...below is the program i'have being working on...i hope anyone out there reading this threat can help me solve the problem...im just a nwbz... i will apreciate alot any help...thanks in advance.....


this is use to update the data
Private Sub Update_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim varCommand As New SqlCommand

Dim varAdapter As SqlDataAdapter

Try
varConnection = New SqlConnection("Server=(local);user id=sa;password=;Initial Catalog=ROP1") 'STEP 1
varConnection.Open()
varQuery = "UPDATE tbl_rop SET Name_User ='" & Trim(TextBox1.Text) & "', Date_of_birth= '" & Trim(TextBox2.Text) & "' , Hobbies='" & Trim(TextBox3.Text) & "' , Phone_Number ='" & Trim(TextBox4.Text) & "' where Name_User = '" & Trim(TextBox1.Text) & "'"
varCommand = New SqlCommand(varQuery, varConnection)
varCommand.CommandTimeout = 9999 'STEP 4
varCommand.ExecuteNonQuery() 'STEP 5

varCommand.Dispose() 'STEP 6
varConnection.Close() 'STEP 7

Catch ex As Exception
varConnection.Open()
'varConnection = New SqlConnection("Server=(local);user id=sa;password=;Initial Catalog=ROP1")

Dim sql As String
sql = "SELECT * FROM tbl_rop"
'varCommand = New SqlCommand(sql, varConnection)
varAdapter = New SqlDataAdapter(varCommand)


varAdapter.Fill(varDataSet)
TextBox1.Text = varDataSet.Tables(0).Rows(0).Item("Name_User")
TextBox2.Text = varDataSet.Tables(0).Rows(0).Item("Date_of_birth")
TextBox3.Text = varDataSet.Tables(0).Rows(0).Item("Hobbies")
TextBox4.Text = varDataSet.Tables(0).Rows(0).Item("Phone_Number")

varConnection.Close()

End Try

End Sub

this is used to delete the data which nothing seem to be working help mw find the problem


Private Sub btnDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

MsgBox("are you sure want to delete?, important information will be erase. ")
varConnection = New SqlConnection("Server=(local);user id=sa;password=;Initial Catalog=ROP1") 'STEP 1
varConnection.Open() 'STEP 2
varQuery = "DELETE FROM tbl_rop WHERE Name_User ='" & TextBox1.Text & "', Date_of_birth= '" & TextBox2.Text & "' , Hobbies='" & TextBox3.Text & "' , Phone_Number ='" & TextBox4.Text & "'" 'STEP 3
varCommand = New SqlCommand(varQuery, varConnection)

varCommand.ExecuteNonQuery()

varCommand.Dispose() 'STEP 6
varConnection.Close() 'STEP 7

End Sub

display data on datagrid, refresh (called again) after doing update or delete.

display data on datagrid, refresh (called again) after doing update or delete.

hows that i dont really understand?

i just give a suggest to use datagrid to display data after update or delete.
- how to make a latest data appear after update it using an upadate button?
-> Select the last data and show it.

thanks for the advice but i just want to use simple code (like the above program) so i realy appreciate if u can guide me with code i created early

no...showing data in datagrid is not difficult.
this code needed 1 datagrid named dgROP.
This prosedure to show data in datagrid :

Private Sub ShowDataGrid()
        'Dim varConnection As SqlvarConnectionection
        Dim cmdROP As New SqlCommand
        Dim daROP As New SqlDataAdapter
        Dim dsROP As New DataSet
        Dim dtROP As New DataTable

        'varConnection = GetvarConnectionect()
        Try
            cmdROP = varConnection.CreateCommand
            cmdROP.CommandText = "SELECT * FROM tbl_rop"
            daROP.SelectCommand = cmdROP
            daROP.Fill(dsROP, "tbl_rop")
            dgROP.DataSource = dsROP
            dgROP.DataMember = "tbl_rop"
            dgROP.ReadOnly = True
        Catch ex As Exception
            MsgBox("Error: " & ex.Source & ": " & ex.Message, MsgBoxStyle.OKOnly, "varConnectionection Error !!")
        End Try
    End Sub

Call this procedure after you update or delete. the newest data will shown on datagrid.
Private Sub Update_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
.....
ShowDataGrid()
End Sub

suppose i dont want to use data grid and actually i want the data that have being updated to appear in the same textbox were user entered the same data(in my program user can search their data i create next & previous button)... can i just simplify it with the code that i made before?

for examples this code below

Private Sub Update_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Dim varCommand As New SqlCommand

        Dim varAdapter As SqlDataAdapter

        Try
            varConnection = New SqlConnection("Server=(local);user id=sa;password=;Initial Catalog=ROP1") 'STEP 1
            varConnection.Open() 'STEP 2
            varQuery = "UPDATE tbl_rop SET Name_User ='" & Trim(TextBox1.Text) & "', Date_of_birth= '" & Trim(TextBox2.Text) & "' , Hobbies='" & Trim(TextBox3.Text) & "' , Phone_Number ='" & Trim(TextBox4.Text) & "' where Name_User = '" & Trim(TextBox1.Text) & "'" 'STEP 3
            varCommand = New SqlCommand(varQuery, varConnection)
            varCommand.CommandTimeout = 9999 'STEP 4
            varCommand.ExecuteNonQuery() 'STEP 5

            varCommand.Dispose() 'STEP 6
            varConnection.Close() 'STEP 7


        Catch ex As Exception


            
        End Try

should i add any more connection so the data can referesh outomatically?

varConnection = New SqlConnection("Server=(local);user id=sa;password=;Initial Catalog=ROP1")
varConnection.Open()
sql = "SELECT * FROM tbl_rop"
varCommand = New SqlCommand(sql, varConnection)
varAdapter = New SqlDataAdapter(varCommand)
varAdapter.Fill(varDataSet)

TextBox1.Text = varDataSet.Tables(0).Rows(0).Item("Name_User")
TextBox2.Text = varDataSet.Tables(0).Rows(0).Item("Date_of_birth")
TextBox3.Text = varDataSet.Tables(0).Rows(0).Item("Hobbies")
TextBox4.Text = varDataSet.Tables(0).Rows(0).Item("Phone_Number")

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.