473,383 Members | 1,862 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

update / insert button

trying to create an update/insert button
basically if the record exists in the database i want to do an update
if it doesnt exist i want to create a new record using insert
all using the same button

my code (which is a mess from playing around!) (this is a test page im
playing with)
ID in the database is auto increment
just cant seem to get anything to work
(id rather have it checking the database for the exisitence of an ID rather
than relying on the contents of the
id.text box)
(this code wont work as im unsure how to query the database first to check
the exisitence of an ID then
create a sql string accordingly!)
Dim Sql As String = ""
Dim sqlinsert As String = "INSERT INTO news (news) VALUES ('" +
TextBox2.Text.ToString + "')"
Dim sqlupdate As String = "UPDATE news SET news = '" +
TextBox2.Text.ToString + "' WHERE Id = ('" + TextBox1.Text.ToString + "')"
Dim strConn As String = "server=;uid=sa;pwd=;database="
Dim conn As New System.Data.SqlClient.SqlConnection(strConn)
Dim Cmd As New System.Data.SqlClient.SqlCommand(Sql, conn)
Dim objDR As System.Data.SqlClient.SqlDataReader
conn.Open()
objDR = Cmd.ExecuteReader(System.Data.CommandBehavior.Clos eConnection)
While objDR.Read()
If objDR.IsDBNull(0) Then
Sql = sqlinsert
Else : Sql = sqlupdate
End If
End While
Dim Cmd2 As New System.Data.SqlClient.SqlCommand(Sql, conn)
objDR = Cmd2.ExecuteReader(System.Data.CommandBehavior.Clo seConnection)
Nov 18 '05 #1
1 1813

Can you alternatively implement this business logic in a Stored Proc at DB level....That seems to be an effective solution!!!

Alternatively...you can write a conditional SQL query in your code ( and assign to var sqlInsertUpd) which handles the logic, and then execute the same using reader.

Regards
Avneesh
Nov 18 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Joe Fetters via .NET 247 | last post by:
Have googled and read the VS.NET documentation can't seem to getthe answer to the following. Environment: Framework 1.1 VB.NET WinForm Access database Using all automagic tools (DataAdapter...
25
by: Neo Geshel | last post by:
This works: <form> <asp:TextBox id="name" /> <%= name.ClientID %> </form> But this DOES NOT work: <form>
15
by: graham | last post by:
Hi all, <bitching and moaning section> I am asking for any help I can get here... I am at the end of my tether... I don;t consider myself a genius in any way whatsoever, but I do believe I have...
0
by: Erik | last post by:
Why isn't my update method getting called? Pasted below is an aspx from a 1.1 application I'm working on. It has two textboxes and a button for inserting data into the database, and a datagrid...
1
by: dipalichavan82 | last post by:
i have a form on which i m displaying data in datagridview.on same form i have update ,insert,delete button.when i click on insert or update btn new form opens with controls(textbox) in which user is...
11
by: SAL | last post by:
Hello, I have a Gridview control (.net 2.0) that I'm having trouble getting the Update button to fire any kind of event or preforming the update. The datatable is based on a join so I don't know...
4
by: Constantine AI | last post by:
I am working on an automatic Purchase Order Generator where with a click of a button will automatically assign a PONo to certain stock items (based on the SuppNo). The coding i have at present is as...
2
by: sirdavethebrave | last post by:
Hi guys - I have written a form, and a stored procedure to update the said form. It really is as simple as that. A user can go into the form, update some fields and hit the update button to...
4
by: justice750 | last post by:
Hi All, I am using a FormView control. The allows me to update records in the database. However, when a database field is null I can not update the field on the form. It works fine when the field...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.