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

Posted on 2014-09-08
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
  • 4
  • 2
LVL 40

Accepted Solution

Jacques Bourgeois (James Burger) earned 500 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 ?
The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

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

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

Suggested Solutions

Everyone has problem when going to load data into Data warehouse (EDW). They all need to confirm that data quality is good but they don't no how to proceed. Microsoft has provided new task within SSIS 2008 called "Data Profiler Task". It solve th…
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Via a live example, show how to shrink a transaction log file down to a reasonable size.

810 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