Results 1 to 4 of 4

Thread: [RESOLVED] Member parameters always set to null...?

  1. #1

    Thread Starter
    New Member
    Join Date
    Jan 2006
    Posts
    2

    Resolved [RESOLVED] Member parameters always set to null...?

    Hello!

    I've an ASP web page in which a button click handler requires access to the page's member parameters. Every time the button routine runs however, the member parameters seem to be null (values = NOTHING).

    The code is appended below. Sorry for the length of the code, but I wanted to make sure I wasn't missing anything, and that my problem was clear.

    The page initializes fine, and the date is pulled correctly and displayed in the grid upon load... but whenever the button routine commences, dsEmployee is a null value (I checked with the debugger).

    I've tried prefacing the variable names with ME, but this changed nothing.

    Since I'm a newbie, I've a sneaking suspicion that the solution will be so obvious as to verge on the ridiculous... Any ideas?



    VB Code:
    1. Imports System.Data.SqlClient
    2. Imports System.Data
    3.  
    4.  
    5. Partial Class AddEmployee
    6.     Inherits System.Web.UI.Page
    7.  
    8.     Dim daEmployee As SqlDataAdapter
    9.     Dim dsEmployee As DataSet
    10.  
    11.     Const stConnection As String = "Data Source=.\sqlexpress;Initial Catalog=dbBatteries;Integrated Security=True"
    12.  
    13.     Const stSelect As String = "SELECT LastName, FirstName, Number, EmployeeID " + _
    14.                                 "FROM tblEmployee ORDER BY LastName"
    15.  
    16.     Const stTableName As String = "Employee"
    17.  
    18.  
    19.  
    20.  
    21.     Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    22.  
    23.         If Not Me.IsPostBack Then
    24.  
    25.             ' data connection
    26.             daEmployee = New SqlDataAdapter(stSelect, stConnection)
    27.  
    28.             ' data island
    29.             dsEmployee = New DataSet(stTableName)
    30.  
    31.             ' fill data island
    32.             daEmployee.Fill(dsEmployee, stTableName)
    33.  
    34.             ' bind data to control
    35.             grdEmployee.DataSource = dsEmployee.Tables(stTableName)
    36.             grdEmployee.DataBind()
    37.  
    38.         End If
    39.  
    40.     End Sub
    41.  
    42.     Protected Sub butInsert_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles butInsert.Click
    43.  
    44.         'daEmployee.InsertCommand.CommandText = stInsert
    45.         Dim drEmployee As DataRow
    46.  
    47.         ' create a datarow object (there exists no constructor)
    48.         drEmployee = dsEmployee.Tables(stTableName).NewRow()
    49.  
    50.         ' fill in its values
    51.         drEmployee.Item("LastName") = txtLastName.Text
    52.         drEmployee.Item("FirstName") = txtFirstName.Text
    53.         drEmployee.Item("Number") = CInt(txtNumber.Text)
    54.  
    55.         ' add it to the data island
    56.         dsEmployee.Tables(stTableName).Rows.Add(drEmployee)
    57.  
    58.     End Sub
    59.  
    60. End Class

  2. #2
    Frenzied Member Phill64's Avatar
    Join Date
    Jul 2005
    Location
    Queensland, Australia
    Posts
    1,201

    Re: Member parameters always set to null...?

    in ASP.NET, just like in classic ASP, objects are erased with every page refresh, thus since in yor load event you are not populating the objects if the page is a postback, they are empty.

    Either use these variables in your button click, or else remove that if statement.

  3. #3
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: Member parameters always set to null...?

    To preserve the dataset values, put it in viewstate.

  4. #4

    Thread Starter
    New Member
    Join Date
    Jan 2006
    Posts
    2

    Re: [RESOLVED] Member parameters always set to null...?

    Thanks guys! I've got it working now.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width