更新vb oledb中的数据

时间:2016-01-07 02:03:58

标签: asp.net vb.net visual-studio oledb

我会直接前进。我有这些代码行。基本上它是关于基于用户输入的更新数据。但首先,文本框将从数据库中检索数据,然后用户可以自由更改文本框的值/文本,当用户单击按钮时,系统会将新值存储到数据库中。

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    Dim check As String = Session("user_id")

    Dim SqlSelect As String = "SELECT * FROM Worker Where user_id='" & check & "' "
    Dim con As dbConn = New dbConn()
    Dim cmd As New OleDbCommand(SqlSelect, con.oleconnection)
    Dim reader As OleDbDataReader

    Try
        con.open()
        reader = cmd.ExecuteReader()
        reader.Read()
        WorkerID_.Text = reader("WorkerID").ToString()
        WorkerName.Text = reader("WorkerName").ToString()
        DoB.Text = reader("DoB").ToString()
        Address.Text = reader("Address").ToString()
        Phone.Text = reader("Phone").ToString()
        Email.Text = reader("Email").ToString()
        Company.Text = reader("CompanyName").ToString()
        PassNum.Text = reader("PassportNum").ToString()
        PassExp.Text = reader("PassportExp").ToString()
        VisaExp.Text = reader("VisaExp").ToString()
    Finally
        reader.Close()
        con.close()
    End Try

End Sub

Protected Sub Update_Click(sender As Object, e As EventArgs)
    Dim con As dbConn = New dbConn()
    Dim SqlUpdate As String

    SqlUpdate = "UPDATE Worker SET "
    SqlUpdate &= "WorkerID = '" & WorkerID_.Text & "', "
    SqlUpdate &= "WorkerName = '" & WorkerName.Text & "', "
    SqlUpdate &= "Address = '" & Address.Text & "', "
    SqlUpdate &= "Email = '" & Email.Text & "', "
    SqlUpdate &= "CompanyName = '" & Company.Text & "', "
    SqlUpdate &= "PassportNum = '" & PassNum.Text & "' "
    SqlUpdate &= "Where user_id ='" & Session("user_id") & "'"

    Dim cmd As New OleDbCommand(SqlUpdate, con.oleconnection)
    Dim rad As OleDbDataReader

    Try
        con.open()
        rad = cmd.ExecuteReader()

    Finally
        rad.Close()
        con.close()
        Response.Redirect("~\Worker\Profile.aspx")
    End Try
End Sub

基于此代码,无法更新数据。 update_click中的textbox.text将在page_load(来自数据库的数据)中检索文本框的相同值,而不是用户输入的文本。

如果我删除了从page_load中的数据库中检索数据的代码,那么一切正常。我错过了我的代码吗?

1 个答案:

答案 0 :(得分:0)

If Not Page.IsPostBack Then
     //code
End If

inside page_load