Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17


instead of binding to textboxes how do I link textboxes to data rows or data column in sqlserver

Posted on 2014-09-08
Medium Priority
Last Modified: 2014-09-09
I discovered that once you problematically bind data to a textbox you can't undo to bind to a new dataset. Someone suggested a better way I hope I describe it right and that is to set text boxes to data rows from a datatable.

This is how I databinded

txtPlayerID.DataBindings.Add("Text", ds.Tables(0), "PlayerID")
        txtFname.DataBindings.Add("Text", ds.Tables(0), "FirstName")
        txtLname.DataBindings.Add("Text", ds.Tables(0), "Lastname")
        txtCollege.DataBindings.Add("Text", ds.Tables(0), "College")
        txtDraftTeam.DataBindings.Add("Text", ds.Tables(0), "DraftTeamID")
        txtHeight.DataBindings.Add("Text", ds.Tables(0), "Height")
        txtHighschool.DataBindings.Add("Text", ds.Tables(0), "HighSchool")
        txtJersey.DataBindings.Add("Text", ds.Tables(0), "Jersey")
        txtNCAAID.DataBindings.Add("Text", ds.Tables(0), "NCAAID")
        txtNcaaTeam.DataBindings.Add("Text", ds.Tables(0), "CollTeamID")
        txtOverall.DataBindings.Add("Text", ds.Tables(0), "OverallPos")
        txtPlayerNote.DataBindings.Add("Text", ds.Tables(0), "BioHTML")
        txtPosDrafted.DataBindings.Add("Text", ds.Tables(0), "DraftPos")
        txtPosition.DataBindings.Add("Text", ds.Tables(0), "Position")
        txtRDDrafted.DataBindings.Add("Text", ds.Tables(0), "Round")
        txtSLUG.DataBindings.Add("Text", ds.Tables(0), "Slug")
        txtStatus.DataBindings.Add("Text", ds.Tables(0), "Status")
        txtTeamID.DataBindings.Add("Text", ds.Tables(0), "TeamID")
        dtp1.DataBindings.Add("Text", ds.Tables(0), "Born")
        txtWeight.DataBindings.Add("Text", ds.Tables(0), "Weight")

        txtYearDrafted.DataBindings.Add("Text", ds.Tables(0), "DraftYear")
        txtYearsinCollege.DataBindings.Add("Text", ds.Tables(0), "YrColl")

Open in new window

 is there a sample I could get using datatables and datarows or columns.
Question by:powerztom
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
LVL 40

Accepted Solution

Jacques Bourgeois (James Burger) earned 2000 total points
ID: 40311170
DataBinding is not the only way to display and retrieve information from a Form. Yes, its simplifies the process for simple things, but you lose a lot of control over the process.

I use DataBinding a lot with grids and ComboBox controls, but when comes the time to display one record at a time in a form, I prefer to do it myself, which gives me a lot of control over what happens between the interface and the data.

I usually have a Form level variable that is a pointer to the current row, let's call it CurrentRow.

When the user moves to a new row, no matter how, I call a method that first saves the content of the CurrentRow to the table, something like:

ds.Tables(CurrentRow).Item("Lastname") = txtLname.Text
ds.Tables(CurrentRow).Item("College") = txtCollege.Text

I then change the value of CurrentRow to reflect the move. If the user requested the following row, I would thus go:

CurrentRow += 1

I then fill the form with the value of the new CurrentRow:

txtLname.Text= ds.Tables(CurrentRow).Item("Lastname")
txtCollege.Text = ds.Tables(CurrentRow).Item("College")

You need to write more code, but you gain complete control, know exactly what is happening and when it is, and can more easily intervene if you need to validate the data or modify it in any way before committing it to the table.

This being said, if you still want to use DataBinding and want to use a DataTable instead of a DataSet:

txtLname.DataBindings.Add("Text", YourTable(0), "Lastname")
txtCollege.DataBindings.Add("Text",YourTable(0), "College")

As for the DataRow and the column, they are already there. (0) is the first row of the table in that code. Simply change the value if you want another row. "LastName" and "College" are the columns.

Author Closing Comment

ID: 40311937
Hey thanks for the detailed explanation. It helped me out in understanding manipulation for data.Cheers

Author Comment

ID: 40312037
Yo James I defined Currentrow as a integer

and tried to incorporated
ds.Tables(CurrentRow).Item("Lastname") = txtLname.Text
ds.Tables(CurrentRow).Item("College") = txtCollege.Text
txtLname.Text= ds.Tables(CurrentRow).Item("Lastname")
txtCollege.Text = ds.Tables(CurrentRow).Item("College")

but I got a error line under item I just need to fill datagridview with different datasets or data tables (different queries) and have textboxes display each move on the datagridview. Maybe I 'm missing something ?
How to Create Failover DNS Record Sets in Route 53

Route 53 has the ability to easily configure DNS record sets specifically for failover scenarios. These failover record sets can be configured to failover to full-blown deployments in other regions or to a static HTML page that informs your customers of the issue.

LVL 40
ID: 40312203
When you are asking a question about an error, it is necessary that you tell us what the error is and exactly on which line you get the error. Since a lot of things can cause an error, it is usually impossible for us to help you correctly without that information.

Depending on how your project is set, as well as the type of data that is in the table, you might need to convert the result of calling Item to String in order to use it in the Text property:

txtLname.Text= CStr(ds.Tables(CurrentRow).Item("Lastname"))

But since you did not specify what the error is, it can be something else.

And you never told us about the grid.

Since you want to follow the DataGridView, do not fill your TextBoxes from the DataTable, do it from the grid.

Depending on different factors, information in the grid and in the table can be slightly different. This could confuse the user.

If the grid is not sorted the same way as the DataTable, CurrentRow becomes useless, because what is row 5 in the grid might not be the same information that you have in row 5 of the table.

The following might be more appropriate than trying to get the data from the DataTable:

txtLname.Text= yourGrid.CurrentRow.Cells("Lastname")  or
txtLname.Text=CStr(yourGrid.CurrentRow.Cells("Lastname")) if it does not work

Depending on how the grid was filled in, you might also not be able to retrieve the information it contains through the name of the field. If this is the case, then you will have to use the index (position) of the column instead:


Author Comment

ID: 40312297
Never mind Dude I got it Thanks

Author Comment

ID: 40312303
Thanks again James for Input I think I got it.

Featured Post

Moving data to the cloud? Find out if you’re ready

Before moving to the cloud, it is important to carefully define your db needs, plan for the migration & understand prod. environment. This wp explains how to define what you need from a cloud provider, plan for the migration & what putting a cloud solution into practice entails.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

688 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question