Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

Hello everyone.

 

I am familiar with how to get the user input from a textbox and send that information to a database. What is the syntax if I wanted to do the opposite. That is, retrieve a particular value from a database record and write that value in a textbox. Thank you for your time.

Posted

Do you mean you want to have a textbox match the value in a database field, or you want to peform some action and make the textbox the value?

 

You can do it without a dataset, but I havn't tried in months since I AM using a dataset, so I'll go that route:

 

You can do it in code, but thats a lot longer. I'd say just open up Server Explorer, get a new connection. If its not SQL Server (like Jet/MDB/Access) click back and select the Jet method and locate it on your hard drive.

 

Once you fill in the options, you'll see the database connection in the Server Explorer. Drag the table you want to use over to the form you're using. The table and connection will create icons and all the proper connection strings. At this point, I'd give them more meaningful names.

 

I'll use cnConnection and daAdatper for the example. Also dsDataset for the dataset.

 

in the public declaration:

 

private dsDataset as Dataset

 

then create a procedure:

 

   Private Sub FillDataset()
       dsDataset = New DataSet
       daDataset.fill(dsDataset, "Tablename")
   End Sub

 

From here, you have two options. One is to automatically set the binding.

 

In public Declaration:

 

    Public cmCustNav As CurrencyManager
'CurrencyManager basically manages which record you're on

 

Then link it to the textbox in a procedure (button, form load, etc):

 

Textbox1.DataBindings.Add("Text", .dsDataset.Table("tableName"), "CustomerStatus")
           'Link a curreny manager to ease code writing
           cmCustNav = Me.BindingContext(.dsDataset.Table("tableName"))

Now, no matter what, textbox1 will show you what is in the current index of the database.

 

You can navigate to get the right data basically like this:

 

Private Sub btnCustNavFirst_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
       cmCustNav.Position = 0
   End Sub
   Private Sub btnCustNavPrev_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
       cmCustNav.Position -= 1
   End Sub
   Private Sub btnCustNavNext_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
       cmCustNav.Position += 1
   End Sub

 

Another way to do it, is saying you have a dataset populated with data, put this in a procedure:

 

textbox1.text = dsDataSet.Table("Tablename").row(Index).item("name")[code=visualbasic]

 

or something like that. I can't say off hand since I'm not using that at the moment. Hope that at least helped some

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...