在数据库中编辑记录时保留ID

时间:2015-12-04 18:52:22

标签: asp.net vb.net webforms

我正在尝试编辑我的数据库中的一个团队,我得到了所有相关信息,但当我点击“确定”时,所有变量都被重置,这是预期的行为,但我需要按顺序保留ID确保我提交编辑。我想知道是否有人知道如何做到这一点,我使用各种搜索术语搜索,所有这些都是我不使用的MVC。

以下相关代码。

Public Class 
Inherits System.Web.UI.Page

Private f_conAdministrator As OleDb.OleDbConnection ' Import System.Data.OleDB

' --------------------------------------------------------------------------------
' Form Variables
' --------------------------------------------------------------------------------
Private f_blnResult As Boolean      ' Don't use DialogResult since it triggers a cascade close
Private f_intTeamID As Integer

' --------------------------------------------------------------------------------
' Name: Page_Load
' Abstract: Handles the Page load event
' --------------------------------------------------------------------------------
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

    Try

        Dim blnResult As Boolean = False

        ' First time the page is loaded?
        If Page.IsPostBack = False Then

            ' Load the exiting Team data
            blnResult = LoadTeam()

            ' Did it work?
            If blnResult = False Then

                ' No, warn the user ...
                SendMessageToClient("Unable to load team information from the database\n" & "The form will now close.")

                ' Close the form

                ' False = don't generate exception.  This is a planned redirect.
                Response.Redirect("./WManageTeams.aspx", False)

            End If

        End If

    Catch excError As Exception

        ' Log and display error message
        WriteLog(excError)

    End Try

End Sub



' --------------------------------------------------------------------------------
' Name: LoadTeam
' Abstract: Get the team information from the database and populate the
'           form field with it
' --------------------------------------------------------------------------------
Private Function LoadTeam() As Boolean

    Dim blnResult As Boolean = False

    Try

        Dim udtTeam As udtTeamType = New udtTeamType

        ' Which team do we edit?
        f_intTeamID = Val(Request.QueryString("intTeamID"))
        udtTeam.intTeamID = f_intTeamID

        ' Open DB connection
        If OpenDatabaseConnectionMSAccess() = True Then

            ' Do it
            blnResult = GetTeamInformationFromDatabase(udtTeam)

            ' Did it work?
            If blnResult = True Then

                ' Yes
                txtTeam.Text = udtTeam.strTeam
                txtMascot.Text = udtTeam.strMascot

                ' Set focus to Team name and select existing text
                txtTeam.Focus()

            End If

            ' Close the conection
            CloseDatabaseConnection()

        End If

    Catch excError As Exception

        ' Log and display error message
        WriteLog(excError)

    End Try

    Return blnResult

End Function



' --------------------------------------------------------------------------------
' Name: btnOK_Click
' Abstract: If the data is good then save the changes
' --------------------------------------------------------------------------------
Private Sub btnOK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOk.Click

    Try

        ' Is the data OK?
        If IsValidData() = True Then

            ' Open a connection
            If OpenDatabaseConnectionMSAccess() = True Then

                ' Yes, save
                If SaveData() = True Then

                    ' If the save was successful then ...

                    ' Success
                    f_blnResult = True

                End If

                ' Close the Connection
                CloseDatabaseConnection()

            End If

            ' Did it work?
            If f_blnResult = True Then

                ' Yes, redirect.  False = don't generate exception.  This is a planned redirect.
                Response.Redirect("./WManageTeams.aspx?intTeamID=" & f_intTeamID, False)

            End If

        End If

    Catch excError As Exception

        ' Log and display error message
        WriteLog(excError)

    End Try

End Sub


' --------------------------------------------------------------------------------
' Name: GetTeamInformationFromDatabase
' Abstract: Get data for the specified team from the database
' --------------------------------------------------------------------------------
Public Function GetTeamInformationFromDatabase(ByRef udtTeam As udtTeamType) As Boolean

    Dim blnResult As Boolean = False

    Try

        Dim strSelect As String = ""
        Dim cmdSelect As New OleDb.OleDbCommand
        Dim drTTeams As OleDb.OleDbDataReader

        ' Build the select string
        strSelect = "SELECT *" & _
                    " FROM TTeams" & _
                    " WHERE intTeamID = " & udtTeam.intTeamID

        ' Retrieve the record
        cmdSelect = New OleDb.OleDbCommand(strSelect, f_conAdministrator)
        drTTeams = cmdSelect.ExecuteReader

        ' Read (there should be 1 and only 1 row)
        drTTeams.Read()
        With drTTeams
            udtTeam.strTeam = .Item("strTeam")
            udtTeam.strMascot = .Item("strMascot")
        End With

        ' Clean up
        drTTeams.Close()

        ' Success
        blnResult = True

    Catch excError As Exception

        ' Log and display error message
        WriteLog(excError)

    End Try

    Return blnResult

End Function

2 个答案:

答案 0 :(得分:0)

与加载数据时的第一个请求一样,您也可以从PostBack请求中的请求参数中读取ID:

f_intTeamID = Val(Request.QueryString("intTeamID"))

仅为特定请求创建为请求提供服务的实例,然后将其丢弃。当用户单击OK按钮后新请求到达服务器时,将创建一个新实例。这就是变量值消失的原因。

由于您可能需要每个请求中的ID,您可以将语句添加到Page_Load方法,以便在需要时设置变量。

答案 1 :(得分:0)

您可以将其保存在会话变量中,如下所示: -

f_intTeamID = Val(Request.QueryString("intTeamID"))
Session("f_intTeamID") = f_intTeamID 

下次回发页面时,请从会话中检索。