• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 219
  • Last Modified:

How to set an unbound textbox to value of a datatable

In the below code the IsLoggedOn() function sets the two global variables gbAdmin and gbDeveloper to True/False.  I have two unbound textboxes that I want to set to the values based on who is logged in.

The below code generates the following error:  NullReferenceException Occurred on the me.txtUsername.Text line of code

What do I need to do to resolve?
Public Sub New()
        ' This call is required by the Windows Form Designer.
        ' Add any initialization after the InitializeComponent() call.
        If gbAdmin = True Then
            Me.txtUserName.Text = Me.BRDWorkslatesDataSet.tblApplicationParameters.Columns("AdminUserNameColumn").ToString
            Me.txtPassword.Text = Me.BRDWorkslatesDataSet.tblApplicationParameters.Columns("AdminPasswordColumn").ToString
        ElseIf gbDeveloper = True Then
            Me.txtUserName.Text = Me.BRDWorkslatesDataSet.tblApplicationParameters.Columns("DeveloperUserNameColumn").ToString
            Me.txtPassword.Text = Me.BRDWorkslatesDataSet.tblApplicationParameters.Columns("DeveloperPasswordColumn").ToString
        End If
    End Sub

Open in new window

  • 2
1 Solution
Bob LearnedCommented:
I would guess that either the 'AdminUserNameColumn' or the 'DeveloperUserNameColumn' is an invalid name.

KentDBerryAuthor Commented:

thx.  You are correct, so I changed the line to the following, and I now get "AdminUserName" returned as the value.  If I want to return the value of the data what would I have to do.

            Me.txtUserName.Text = Me.BRDWorkslatesDataSet.tblApplicationParameters.Columns("AdminUserName").ToString
            Me.txtPassword.Text = Me.BRDWorkslatesDataSet.tblApplicationParameters.Columns("AdminPassword").ToString

Open in new window

Bob LearnedCommented:
To get data from a DataTable, you need to get the DataRow that you want, and then get the data from that.

Simplified example:

Dim dt As DataTable = Me.DataSet1.Tables("Table1")
Dim dr As DataRow = dt.Rows(0)
Dim value As String = dr("Value").ToString()

In your case, you would need to use the BRDWorkslatesDataSet (DataSet), tblApplicationParameters (DataTable) and AdminUserName (column name).


Featured Post

[Webinar] Kill tickets & tabs using PowerShell

Are you tired of cycling through the same browser tabs everyday to close the same repetitive tickets? In this webinar JumpCloud will show how you can leverage RESTful APIs to build your own PowerShell modules to kill tickets & tabs using the PowerShell command Invoke-RestMethod.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now