?
Solved

Updating a database from a datagrid

Posted on 2007-08-06
7
Medium Priority
?
221 Views
Last Modified: 2010-04-23
Hi all,

I am using VB.NET (VS 2003), with SQL Server 2000.

I have a datagrid which is populated through a dataset with information from a SQL Server table. The user is to be able to add rows to the existing data and also change some of the data that has already been pulled out of SQL Server.

My question is, what is the best way to go about updating the database with all changes? When should the update occur? What code should I use?

Any comments would be great.
0
Comment
Question by:FMabey
  • 4
  • 3
7 Comments
 
LVL 18

Expert Comment

by:Ajay Sharma
ID: 19636947
Do it in three way

1) fille the datagrid with the specified table.

2) create some text fields and a button to add the field's values to the dataTable , and then also insert to the database. use this :
datatableCust.rows.add()


3) use an Update column in the datagrid to update the existing details.
0
 
LVL 3

Author Comment

by:FMabey
ID: 19636950
I have found this way of updating the database with any changes:
DataAdapter.Update(DataSet.GetChanges())

Can anyone see any problems with this?
0
 
LVL 18

Expert Comment

by:Ajay Sharma
ID: 19636965
see this link

http://authors.aspalliance.com/aspxtreme/sys/Data/common/DataAdapterClassUpdate.aspx

here
public DataSet updateData ( DataSet myDataSet,
      string connString, string query ) {
   OleDbConnection myConn = new OleDbConnection ( connString );
   OleDbDataAdapter adapter = new OleDbDataAdapter ( );
   adapter.SelectCommand = new OleDbCommand ( query, myConn );
   OleDbCommandBuilder custCB = new OleDbCommandBuilder ( adapter );
   DataSet myDataSet = new DataSet ( );

us used to update the database.


u need OleDbCommandBuilder to update the dataset
0
NEW Veeam Backup for Microsoft Office 365 1.5

With Office 365, it’s your data and your responsibility to protect it. NEW Veeam Backup for Microsoft Office 365 eliminates the risk of losing access to your Office 365 data.

 
LVL 3

Author Comment

by:FMabey
ID: 19636972
Why can I not use the code I have posted? It seems to work fine for what I want to do....
0
 
LVL 18

Expert Comment

by:Ajay Sharma
ID: 19636986
k it means u got some full fleash code to fullfill ur requirement.

If it is working fine then carry on.

Good luck
0
 
LVL 3

Author Comment

by:FMabey
ID: 19636994
What I wondered was if this is the correct way to go about things or is it (unsafe?!) not a great method.

How do people usually update their data in these situations?
0
 
LVL 18

Accepted Solution

by:
Ajay Sharma earned 750 total points
ID: 19637011
Its not unsafe. It is provided by .net itself.

When using dataset with datagrid it is normally used.

It depends, some people (like me) uses the sql update command because of we got a full control over data.
For exmaple, to run the validation on the new data or the updated data. or to run multiple tasks based on the
new values.
0

Featured Post

Veeam and MySQL: How to Perform Backup & Recovery

MySQL and the MariaDB variant are among the most used databases in Linux environments, and many critical applications support their data on them. Watch this recorded webinar to find out how Veeam Backup & Replication allows you to get consistent backups of MySQL databases.

Question has a verified solution.

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

Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Microsoft Access has a limit of 255 columns in a single table; SQL Server allows tables with over 255 columns, but reading that data is not necessarily simple.  The final solution for this task involved creating a custom text parser and then reading…
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.
Suggested Courses

807 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