Hi

Im new with this and tryng to learn thru reading any sources in internet, but i have no idea where to find a solution to my problem. Every time i save or update a new entry to my database it working find but when im trying to search the new entry, result is zero. But if im going to end the program then reopen again and search the new entry, and its there. I think i need to refresh my database after click the save or update button, but i don't know have. Please help me guys. Please see below for my codes. Thanks!!!

Imports System.Data.SqlClient
Public Class frmuser
    Dim db As New SqlConnection("Data Source=.\SQLEXPRESS;AttachDbFilename=C:----.mdf;Integrated Security=True;User Instance=True")
    Dim cmd As New SqlCommand

    Private Sub cmdback_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdback.Click
        frmmain.Show()
        cmdclear.PerformClick()
        Me.Hide()
    End Sub

    Private Sub cmdclear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdclear.Click
        txtusername.Clear()
        txtname.Clear()
        txtpassword.Clear()
        lblstatus.Text = ""
        txtusername.Enabled = True
        cmdsearch.Enabled = True
        txtname.Enabled = False
        txtpassword.Enabled = False
        cmdsave.Enabled = False
        cmdupdate.Enabled = False
        txtusername.Focus()
    End Sub

    Private Sub cmdsearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdsearch.Click
        If TbluserTableAdapter1.Checkusername(txtusername.Text) = Nothing Then
            lblstatus.Text = "New User"
            txtusername.Enabled = False
            cmdsearch.Enabled = False
            txtname.Enabled = True
            txtpassword.Enabled = True
            cmdsave.Enabled = True
            txtname.Focus()
        Else
            txtname.Text = TbluserTableAdapter1.ViewName(txtusername.Text)
            txtpassword.Text = TbluserTableAdapter1.ViewPassword(txtusername.Text)
            lblstatus.Text = "User Exist"
            txtusername.Enabled = False
            cmdsearch.Enabled = False
            txtname.Enabled = True
            txtpassword.Enabled = True
            cmdupdate.Enabled = True
        End If
    End Sub

    Private Sub cmdsave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdsave.Click
        If txtname.Text = "" Then
            MsgBox("Please enter valid name", MsgBoxStyle.Critical)
            txtname.Focus()
        ElseIf txtpassword.Text = "" Then
            MsgBox("Please enter valid password", MsgBoxStyle.Critical)
            txtpassword.Focus()
        Else
            If Not TbluserTableAdapter1.Checkusername(txtusername.Text) = Nothing Then
                MsgBox("User name is already taken", MsgBoxStyle.Critical)
                cmdclear.PerformClick()
            Else
                db.Open()
                cmd.CommandText = "insert into tbluser (username, name, password) values ('" & txtusername.Text & "', '" & txtname.Text & "', '" & txtpassword.Text & "')"
                cmd.ExecuteNonQuery()
                db.Close()
                MsgBox("User account is successfully created", MsgBoxStyle.Information)
                cmdclear.PerformClick()
            End If
        End If
    End Sub

    Private Sub frmuser_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        cmd.Connection = db
    End Sub

    Private Sub cmdupdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdupdate.Click
        If txtname.Text = "" Then
            MsgBox("Please enter valid name", MsgBoxStyle.Critical)
            txtname.Focus()
        ElseIf txtpassword.Text = "" Then
            MsgBox("Please enter valid password", MsgBoxStyle.Critical)
            txtpassword.Focus()
        Else
            db.Open()
            cmd.CommandText = "update tbluser set name = '" & txtname.Text & "' where username = '" & txtusername.Text & "' "
            cmd.ExecuteNonQuery()
            db.Close()
            MsgBox("User account is successfully updated", MsgBoxStyle.Information)
            cmdclear.PerformClick()
        End If
    End Sub
End Class

How far am I right, you are using a Typed Datasource to search the newest entry you did. Opened datasource never read the newly inserted or updated data. To do you must close and re-open the datasource. From my point of view use untyped Datasource to search as you did for inserting and updating data into the database.

This article has been dead for over six months. Start a new discussion instead.