Solved

Insert and update

Posted on 2013-06-21
5
362 Views
Last Modified: 2013-06-21
Hi,

I have one record in a table and two records in a gridview as follows.

Id (PK) Name  Points
1         abc        10

and in gridView control
Id  Name     Points
1   abc           20
2   xyz           30

I want to insert and update records from the gridview to the table. In this scenario, Id=1 is already exists in the table so I want to update it and Id=2 is not in the table so I want insert it. i.e. Two operations in one execution. How can I deal this scenario?

Please help me.

Thanks

ayha
0
Comment
Question by:ayha1999
  • 3
  • 2
5 Comments
 
LVL 13

Expert Comment

by:Jesus Rodriguez
ID: 39266500
For each Rw as GridviewRow in Gridview1.rows
 ID=rw.cell(0).value
 dim qry as string="select id from table_name where id='" & ID & "'"
 dim t as new datatable
 dim adt as new sqldataadapter(Qry,YourSqlConn)
 t.clear()
 adt.fill(t)
 if t.rows.count>0 then
   'update
     qry="update table set name='" & rw.cell(1).value &"',Points='" & rw.cell(2).value &"' where ID='" & ID &"'"
 else
  'Insert
   qry="INSERT INTO TABLE(NAME,POINTS) VALUES('" & rw.cell(1).value &"','" & rw.cell(2).value &"')"
end
 dim k as new datatable
 dim adk as new sqldataadapter(Qry,YourSqlConn)
k.clear()
 adk.fill(k)
Next
0
 
LVL 13

Accepted Solution

by:
Jesus Rodriguez earned 500 total points
ID: 39266511
Also you can use the merge command with sql. Look at here

http://technet.microsoft.com/en-us/library/bb510625.aspx
0
 
LVL 7

Author Comment

by:ayha1999
ID: 39266515
In the scenario here, insert and update should happen same time because id=1 is already in the table so need to update and id=2 is not there so need to insert. I think in the above code it either insert or update.

Thanks
0
 
LVL 7

Author Comment

by:ayha1999
ID: 39266559
Could you please give me a sample for the scenario I posted?

Thanks

ayha
0
 
LVL 7

Author Closing Comment

by:ayha1999
ID: 39266894
Thanks
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Image(2) 3 26
C# DataGridView_RowsAdded event not firing 1 39
Create XML 5 33
Showdialog 8 20
Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
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…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

705 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