2016-01-07 60 views
0

我要直挺身前。我有這些代碼行。基本上是基於用戶輸入更新數據。但首先,文本框將從數據庫中檢索數據,然後用戶可以自由更改文本框的值/文本,並且當用戶單擊按鈕時,系統會將新值存儲到數據庫。更新vb中的數據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數據庫中檢索數據的代碼。我錯過了我的代碼中的東西嗎?

+0

我已經解決了這個問題。我忘了檢查它是否回傳。 – procatmer

回答

0

If Not Page.IsPostBack Then 
    //code 
End If 

內的Page_Load