Results 1 to 2 of 2

Thread: can not populate textboxes with data from an access database

  1. #1

    Thread Starter
    New Member
    Join Date
    May 2011
    Location
    Birmingham England
    Posts
    4

    can not populate textboxes with data from an access database

    Hi,

    I have a login form for people to login and get taken to another page. The page they are taken to has a label on it. The logged in user has his, her name put on the label automatically using code. The name of the label is "UserNameLoggedInLabel"

    What I am trying to achieve is:

    what ever name is on the label to search my database for that user and put there details in to the TextBoxes on my form.

    So in my access database I have a table called "tblContacts" which has 4 fields

    identifier,

    UserName,

    UserPassword,

    UserTimer,

    On my form I have 3 TextBoxes I do not want to show "identifier in the "TextBoxes"

    TextBox2

    TextBox3

    TextBox4

    On a button click I have inserted this code I am stuck with the SQL I can get it to populate my 3 TextBoxes with the data in my access database. But it only populates my TextBoxes with the data from the first row. I have tried and tried but I need a good solid example of how to achieve this can anyone help me with this line of code I need.

    Thanks in advance for any help.


    Code:
    dbProvider = "PROVIDER=Microsoft.Jet.OLEDB.4.0;"
            dbSource = "Data Source = " + AppDomain.CurrentDomain.BaseDirectory + "Database1.mdb"
            con.ConnectionString = dbProvider & dbSource
            con.Open()
            If UserNameLoggedInLabel.Text = "" Then
    
            End If
    
    
            sql = "SELECT UserName FROM tblContacts Where UserName='" & UserNameLoggedInLabel.Text & "'"
            da = New OleDb.OleDbDataAdapter(sql, con)
            da.Fill(ds, "database1")
            MaxRows = ds.Tables("database1").Rows.Count
            inc = -1
            TextBox2.Text = CStr(ds.Tables("database1").Rows(0).Item(1))
            TextBox3.Text = CStr(ds.Tables("database1").Rows(0).Item(2))
            TextBox4.Text = CStr(ds.Tables("database1").Rows(0).Item(3))

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: can not populate textboxes with data from an access database

    You're only selecting one column so how are you going to populate three TextBoxes?

    Also, you're always taking the first row from the DataTable so, if you never clear that DataTable, why would the first row ever change? You would surely need the last row, wouldn't you?

    That said, why use a DataTable at all? Do you need to keep that data? I don't think so. You should just be using a data reader. Follow the CodeBank link in my signature and check out my thread on Retrieving & Saving Data and use the example with a data reader as a basis.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Tags for this Thread

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