Saving data from DataGridView using sqlDataAdapter

luxe

New member
Joined
Sep 29, 2011
Messages
2
Programming Experience
5-10
Hello: I have a windows app created in vb.net 2005.
On my form is a datagridview and I need to save data to my sql database.

This is the first time I'm working with the dgv and so I wanted to know,
if I use the sqldataAdapter, do I still have to loop through each record to save?
In my save right now, I'm trying to get the changes the user made.....and then
try to update the dataadapter; however, it's not happy on this line:

data_adapter.Update(changes)

Maybe I'm not doing it correctly...or I need to be looping through each record?

Thanks,
Luxe



VB.NET:
[SIZE=2]changes = ds.GetChanges()
[SIZE=2][COLOR=#0000ff]If [/COLOR][/SIZE][SIZE=2][COLOR=#000000]changes [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]IsNot[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]Nothing [/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]Then 
[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]bolSucess = oService.UpdateJobs_test(changes)
[/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] if[/SIZE][/SIZE]

changes is a dataset.
VB.NET:
[SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] sql [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]String[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] = [/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]"select jobid, jobcreatedby as loginname,jobcreateddate,attachedfilesid, assignedto,assigneddate,AssignedBy,jobduedate,rush,cus.name,signreads,program,hardspec,comments,jobopeneddate,jobstatus,Jobbomstatus,job.custId,TerritoryID, SalesPersonID, Commentsdd from tbljob job,tblcustomer cus where cus.custid= job.custid and jobcommited ='True'"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]

[/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] data_adapter [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] SqlDataAdapter
[/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] myDS [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] DataSet
[/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]Try
[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]data_adapter = [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] SqlDataAdapter(sql, sConnectionString)
data_adapter.Fill(myDS, [/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]"tbljob"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2])
data_adapter.TableMappings.Add([/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]"Table"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2], [/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]"tbljob"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2])[/SIZE][SIZE=2]
data_adapter.Update(changes)
[/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]Catch
[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]MsgBox([/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]"UpdateJobs_test error."[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2])
[/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]Try
[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]Return[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]True
[/COLOR][/SIZE][/COLOR][/SIZE]
 
First up, the application is neither happy nor unhappy. It just does what it does and tells you if there's a problem. If you mean that an exception is thrown then you need to examine that exception to learn what the issue was. In your code, you catch the exception and simply ignore it, displaying a generic error message. Unless you don't care what the actual error was, don't ignore the contents of the exception. It tells you what happened and where.

Apart from that, that second code snippet doesn't really make sense anyway. You create a data adapter with a SELECT command, use that adapter to populate a brand new DataSet, then immediately use the same data adapter to try to save a completely different DataSet. How is the adapter supposed to save changes when it only has SQL code to retrieve data? Also, why is it retrieving data at all if the purpose is to save?
 
Back
Top