Solved

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

Posted on 2014-09-08
6
289 Views
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.
0
Comment
Question by:powerztom
  • 4
  • 2
6 Comments
 
LVL 40

Accepted Solution

by:
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.
0
 

Author Closing Comment

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

Author Comment

by:powerztom
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
and
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 ?
0
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
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:

yourGrid.CurrentRow.Cells(3)
0
 

Author Comment

by:powerztom
ID: 40312297
Never mind Dude I got it Thanks
0
 

Author Comment

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

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed

762 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now