Click here to Skip to main content
15,888,325 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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".

VB
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




VB
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.


VB
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")


[Modified: just removed the extra pre tags that were throwing off the formatting]
Posted
Updated 9-Apr-10 7:36am
v2

1 solution

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
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900