Jump to content
  • Advertisement
Sign in to follow this  
H0bbes

[.net] Edit data

This topic is 4999 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Anyone know something that can provide easier access to the data of data base. i want to do something like that when i move to the next row of data that is editing i want to show all data in the correct textbox. however i don't want to do txBox1.text= ..... for all textbox, i want a component that get the data directly from the data base. Anyone know one or some good sugestion to do that? I'm using vb.net

Share this post


Link to post
Share on other sites
Advertisement
What you want is databinding. -Directly- to the database does not exist in .NET. Microsoft chose to use the disconnected way of editing data.

Briefly: Get the data into a DataSet using a DataAdapter, a Command and a Connection object. Bind textboxes and other controls to columns of datatables in the dataset (DataSource/DataMember/DataField properties). Use a CurrencyManager to navigate through the Dataset. Call Update on the DataAdapter to synchronize with the Database.

Mind you: this is not 'easy'. Many aspects like concurrency and locking should be considered. Take a look at samples in the MSDN to learn this stuff.

Cheers

Share this post


Link to post
Share on other sites
Visual Studio 2005 will have direct support for this kind of data binding. In the examples I've seen, you can simply drag the columns from the Server Explorer and it will handle the back and forth stuff.
Until then, I've used a simple method to avoid having to type textBox1.Text = ... for every field. In my Form_Load method, I set up a hashtable with each field name from the data base bound to a control like this:

FieldControlHash["FirstName"] = textBox1;
FieldControlHash["BirthDate"] = dateTimePicker1;
//...etc.


To copy data into the controls, I run through all the columns in the DataTable returned from my query, match them with the FieldControlHash, and then call a function like this for each control:

void SetValue(object InputControl, object Value)
{
if (InputControl is TextBox)
{
((TextBox)InputControl).Text = Value.ToString();
}
else if (InputControl is DateTimePicker)
{
((DateTimePicker)InputControl).Value = (DateTime)Value;
}
}


To copy date back from the controls, I create a GetValue function that does the exact opposite. From there, you use Update like ernow suggested or write your own function that generates the SQL.
This way is harder than DataBinding like ernow suggested, but I use it because a lot of the GUI controls I use do not support data binding. With this method, I can just add another line to my Get/Set Value functions and have support for a new control.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!