I have searched far and wide but have not been able to solve this problem. I am still in the early learning stage and this is the most complex issue I've come across so far. I have a form that when submitted needs to either update fields in a table or insert new records. I originally created a form to insert records which worked fine. I now have to add in the ability to update. What I have is not even close and I now that. I have tried many things but I'm basically stumped. I've pasted it below:
Dim sParamValue as String = String.Empty
Dim sKeyName as String = String.Empty
Dim conNCIGFDNN As SqlConnection
Dim strInsert As String
Dim strUpdate As String
Dim cmdInsert As SqlCommand
Dim cmdUpdate As SqlCommand
For i As Integer = 0 to Request.Form.Keys.Count - 1
sKeyName = Request.Form.Keys(i)
sParamValue = Request.Form(sKeyName)
Next
Dim UserID As String = Request.Form("UserID")
Dim OrgName As String = Request.Form("OrgName")
Dim ContactName As String = Request.Form("ContactName")
Dim Position As String = Request.Form("Position")
Dim email As String = Request.Form("email")
Dim Address1 As String = Request.Form("Address1")
Dim Address2 As String = Request.Form("Address2")
Dim City As String = Request.Form("City")
Dim ST As String = Request.Form("ST")
Dim RespStates As String = Request.Form("RespStates")
Dim Zip As String = Request.Form("Zip")
Dim Phone As String = Request.Form("Phone")
Dim Fax As String = Request.Form("Fax")
Dim Website As String = Request.Form("Website")
If (dnn_KB_XMod_Index_VC500UserID = dnn_Users.UserID) Then
conNCIGFDNN = New SqlConnection("Server=server;UID=uid;PWD=pass;database=dn")
strUpdate = "UPDATE dnn_KB_XMod_Index_VC500 (UserID, OrgName, ContactName, Position, email, Address1, Address2, City, ST, RespStates, Zip, Phone, Fax, Website) Values ('" & UserID & "','" & OrgName & "','" & ContactName & "','" & Position & "','" & email & "','" & Address1 & "','" & Address2 & "','" & City & "','" & ST & "','" & RespStates & "','" & Zip & "','" & Phone & "','" & Fax & "','" & Website & "')"
cmdUpdate = New SqlCommand(strUpdate, conNCIGFDNN)
conNCIGFDNN.Open()
cmdUpdate.ExecuteNonQuery()
conNCIGFDNN.Close()
Else
conNCIGFDNN = New SqlConnection("Server=server;UID=uid;PWD=pass;database=db")
strInsert = "Insert dnn_KB_XMod_Index_VC500 (UserID, OrgName, ContactName, Position, email, Address1, Address2, City, ST, RespStates, Zip, Phone, Fax, Website) Values ('" & UserID & "','" & OrgName & "','" & ContactName & "','" & Position & "','" & email & "','" & Address1 & "','" & Address2 & "','" & City & "','" & ST & "','" & RespStates & "','" & Zip & "','" & Phone & "','" & Fax & "','" & Website & "')"
cmdInsert = New SqlCommand(strInsert, conNCIGFDNN)
conNCIGFDNN.Open()
cmdInsert.ExecuteNonQuery()
conNCIGFDNN.Close()
End If
Any help is very appreciated as I'm sort of 'dead in the water' here.
Software/Hardware used:
ASKED:
February 28, 2007 3:41 PM
UPDATED:
February 28, 2007 9:38 PM
I just noticed that you also had the update syntax wrong. Update is:
UPDATE table
SET field1=value1,
field2=value2,
…
fieldN=valueN
WHERE keyField=keyValue
Thank you, SheldonLinker! Your suggestions helped clear things up for me. I was not sure how to structure the UPDATE statement as I’d seen a couple different ways on various sites. It looks like I was definitely not going about this the best way. I’ll apply these suggestions tomorrow.
Again, thanks for your help.
Thank you, SheldonLinker! Your suggestions helped clear things up for me. I was not sure how to structure the UPDATE statement as I’d seen a couple different ways on various sites. It looks like I was definitely not going about this the best way. I’ll apply these suggestions tomorrow.
Again, thanks for your help.