Updating dataview in VB.net datagrid

  • Thread starter Thread starter Gaurav
  • Start date Start date
G

Gaurav

Hi,

i have just started working on VB.net and got stuck on the following:

1) i have created a view in Sql Server 2000 using 2 tables and both of
them doesnt have primary keys but i have used inner join on the 1
column in common and those tables , i am confused if i update the data
in datagrid will it update the data in the corresponding tables
underlying the view?

2)I have been trying to update the data in datagrid but database is
not being updated with the code i am using,is there any problem with
my code given below which is on the form closing event:

If dsRelatedAgency.HasChanges Then // dsRelatedAgency is dataset for
the grid


adapRelatedAgency.Update(dsRelatedAgency.GetChanges,
"relatedAgencyView")
//adapRelatedAgency is the adater used for fetching
// relatedAgencyView is the view i have created
End If

i want that any addition,modification or deletion changes to the data
in datagrid should be reflected in my tables.

can someone help if my code is wrong or any suggestions for the
changes in code.

Thanks

gaurav
 
Gaurav said:
Hi,

i have just started working on VB.net and got stuck on the following:

1) i have created a view in Sql Server 2000 using 2 tables and both of
them doesnt have primary keys but i have used inner join on the 1
column in common and those tables , i am confused if i update the data
in datagrid will it update the data in the corresponding tables
underlying the view?
Very very unlikely, instead, use a datarelation object
2)I have been trying to update the data in datagrid but database is
not being updated with the code i am using,is there any problem with
my code given below which is on the form closing event:

If dsRelatedAgency.HasChanges Then // dsRelatedAgency is dataset for
the grid

Unless you wrote some pretty incredible update logic, then it's not being
generate. You have two big problems. ONe is no primary key. IMHO, every
table should have a key no exceptions, but that's another story. You may
want to make an Identity or autoincrement field, just so the tools can
generate update statements for you...none of VS's stuff or the
commandbuilder cna do this without a key. When you configured your
dataadapter, it probably said "couldn't generat the following commands" and
update was one of them.

Calling update without valid update command won't do anything or throw an
exception.

You also have the join issue. It'd take some really smart update logic to
pull this off and it won't happen with autogenerated stuff in VS. You'll
have to write it yourself. ANd this is much more hassle than it's worth.
Take a loot at the DataRelation object
http://www.knowdotnet.com/articles/datarelation.html first

HTH,

Bill
 
Hi bill,

Thanks for the sugesstion,After your suggestion i have thought of
creating primary keys on the tables and using the command builder's
update command to update, will this solve my problem?

thanks in advance

gaurav
 
Hi bill,

i tried that on a single table by creating primary key on a table and by
binding data to text box controls.

when i edit some data in text box and click the update button it gives
throws the null expcetion error on the following line of code:

/DataAdap.Update(DataSet2.GetChanges,"dbo_agency") where dbo_agency is
the source table

the code on the button click event is as follows:

DataAdap.Update(DataSet2.GetChanges,"dbo_agency")
DataSet2.AcceptChanges()

and i have used databindings to bind the data to the text boxes for eg:
txt_title.DataBindings.Add("Text", DataSet2, "dbo_Agency.Title")

Is this databindigs with the text boxes is causing the problem or there
is some problem with my update code?

can give me the sample code for the solution?

Many Thanks

Gaurav Chawla
Student, Edith Cowan University,Perth
Master of Computer Science
 
Back
Top