将更改保存到数据库后,将数据保存到页面 [英] Saving data to the page after changes are saved to the Database

查看:102
本文介绍了将更改保存到数据库后,将数据保存到页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使保存的数据在保存后保留在页面上.

最好的方法是什么?什么是正确的语法?

在我的第一个示例中,我在类中创建了一个子,然后在页面后面的代码和存储过程中创建了一个私人子,该存储过程将根据员工ID将数据拉回,但是当我单击保存"时数据不会保留在页面上".

I am trying to make saved data stay on the page after saving.

What''s the best way? and what the correct syntax?

in my first example, I created a sub within my class then a private sub on the code behind page and a stored procedure that will pull the data back in according to employee ID but the data is not staying on the page when I click "Save".

Public Sub New(ByVal employeeId As Integer)
       Dim conn As SqlConnection = Nothing
       Dim cmd As SqlCommand = Nothing
       Dim rd As SqlDataReader = Nothing

       Try
           Dim connStr As String
           connStr = ConfigurationManager.ConnectionStrings("TestConnectionString").ConnectionString
           conn = New SqlConnection("GetEmployeeData")
           conn.Open()
           rd = cmd.ExecuteReader()

           With cmd
               .CommandType = CommandType.StoredProcedure
               .Parameters.Add("@employee_id", SqlDbType.Int)
               .Parameters("@employee_is").Value = employeeId
               .Connection = conn
           End With

           conn.Open()
           rd = cmd.ExecuteReader()
           While (rd.Read())
               _employee_id = employeeId


               If IsDBNull(rd("personEmployee")) = True Then
                   _personEmployee = ""
               Else
                   _personEmployee = "" & CType(rd("personEmployee"), String)

               End If

               _lastName = CType(rd("lastName"), String)
               _firstName = CType(rd("firstName"), String)

               If IsDBNull(rd("middleName")) = True Then
                   _middleName = ""
               Else
                   _middleName = "" & CType(rd("middleName"), String)
               End If









Private Sub FillPage(ByVal employeeId As Integer)
        Try
            Dim per As editpersonnel = New editpersonnel(employeeId)

            txtPersonEmployee.Text = per.personEmployee
            txtLastName.Text = per.lastName
            txtFirstName.Text = per.firstName
            txtMiddleName.Text = per.middleName
            txtPrimaryEmail.Text = per.primaryEmail
            txtTelephone.Text = per.telephone
            txtExt.Text = per.ext
            txtMobile.Text = per.mobile
            txtPager.Text = per.pager

            If txtActualStart.Text.Length > 0 Then
                per.startDate = txtActualStart.Text
            Else
                per.startDate = Nothing
            End If






在第二个示例中,我创建了一个存储过程,并创建了一个子控件来填充页面中后面的代码,但是页面上显示的数据正是我在代码中所拥有的.







In the second example, I created a stored procedure, and a sub to fill the page in the code behind but the data that appears on the page is what I have in code.


Private Sub PopulateFormFields()
        Dim conn As SqlConnection = Nothing
        Dim cmd As SqlCommand = Nothing
        Dim rd As SqlDataReader = Nothing

        Try
            Dim connStr As String
            connStr = ConfigurationManager.ConnectionStrings("TestConnectionString").ConnectionString
            conn = New SqlConnection("IUpdateEmployee")
            conn.Open()
            rd = cmd.ExecuteReader()

            lblRecordNumber.Text = employee_id.ToString()

            If IsDBNull(rd("@personEmployee")) = True Then

            End If
            txtLastName.Text = ("@lastName")
            txtFirstName.Text = ("@firstName")
            txtMiddleName.Text = ("@middleName")
            txtPrimaryEmail.Text = ("@primaryemail")
            txtTelephone.Text = ("@telephone")



[修改:刚刚删除了多余的格式化前的pre标签]



[Modified: just removed the extra pre tags that were throwing off the formatting]

推荐答案

我喜欢做这样的事情(伪代码):

I like to do something like this (pseudo code):

Public Class Employee

   Private _id As Integer
   Private _name as String

   '' Declare an Event for when the Update is Complete
   Public Event OnUpdateComplete(ByVal sender As Employee)

   Public ReadOnly Property _ID() As Integer
     Get
       Return _id
     End Get
   End Propoerty

   Public Property Name() As String
     Get
       Return _name
     End Get
     Set (ByVal value As String)
       _name = value
     End Set
   End Property

   Public Sub New(ByVal id As Integer)
     Load(id)
   End Sub

   Private Sub Load(ByVal id As Integer)

       	
     Try
       Dim connStr As String
       connStr = ConfigurationManager.ConnectionStrings("TestConnectionString").ConnectionString
       conn = New SqlConnection("GetEmployeeData")
       conn.Open()
       rd = cmd.ExecuteReader()

       With cmd
         .CommandType = CommandType.StoredProcedure
         .Parameters.Add("@employee_id", SqlDbType.Int)
         .Parameters("@employee_is").Value = id
         .Connection = conn
       End With

       conn.Open()
       rd = cmd.ExecuteReader()
       While (rd.Read())
         _id = employeeId


         If IsDBNull(rd("personEmployee")) = True Then
             _name = ""
         Else
             _name = "" & CType(rd("personEmployee"), String)

         End If

      ...
     End Try

   End Sub

   Public Sub Update()

     Dim connStr As String
     connStr = ConfigurationManager.ConnectionStrings("TestConnectionString").ConnectionString
     conn = New SqlConnection("GetEmployeeData")
     conn.Open()
     
     '' Call stored procedure that updates the record and then returns that record
     '' -- UPDATE Employee
     '' -- SET Name = @Name
     '' -- WHERE ID = @ID
     ''
     '' -- SELECT ID, Name
     '' -- FROM Employee
     '' -- WHERE ID = @ID
     rd = cmd.ExecuteReader()
     ...

     RaiseEvent OnUpdateComplete(Me)

     
   End Sub

End Class


...Form Code
'' Declare a Form Level variable WithEvents so you can handle
'' the page PageFill() method
Private WithEvents _rep As Employee = Nothing

'' Get the rep Data Somewhere
  _rep = New Employee(Integer.Parse(txtID.Text))

Private Sub btnUpdate_Click(...)...

  _rep.Name = txtName.Text

  '' Call the update method. When complete, the
  '' OnUpdateComplete Event is fired.
  _rep.Update()  

End Sub

Private Sub _rep_OnUpdateComplete(ByVal sender As Object) Handles _rep.OnUpdateComplete
  
  FillPage(DirectCast(sender, Employee)

End Sub

Private Sub FillPage(ByVal emp As Employee)

  '' Fill in controls here using emp
  txtID.Text = emp.ID
  txtName.Text = emp.Name

End Sub


这篇关于将更改保存到数据库后,将数据保存到页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆