Click here to Skip to main content
15,904,288 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am trying to issue an Update after a INSERT SQL command. The purpose behind the update is to add the auto number ID field to a year and store it as text.

Here is part of my code after the INSERT SQL with SELECT SCOPE_IDENTITY() statement (which works fine):


identity = Integer.Parse(SQLCmd.ExecuteScalar().ToString())
objConnection.Close()
Dim txtAssessmentID As String
' Set Assessment ID
txtAssessmentID = identity.ToString
txtAssessmentID = Year(Today) & "-" & txtAssessmentID.PadLeft(5, "0"c)
strSQL = "UPDATE tblAssessment SET assessmentID = @assessmentID WHERE ID = " & identity
SQLCmd = New SqlCommand() 'The SQL Command
objConnection.Open() 'Open the connection
SQLCmd.Connection = objConnection 'Sets the Connection to use with the SQL Command
SQLCmd.CommandText = strSQL 'Sets the SQL String
SQLCmd.Parameters.AddWithValue("@assessmentID", txtAssessmentID)
Try
    objCommand.ExecuteNonQuery()
    objConnection.Close()
    ' Load Personell Evaluations
Catch ex As Exception
    lblAlert.Text = "Problem updaaating record"
End Try



Any help would be appreciated.
Posted
Updated 29-Sep-11 17:09pm
v2

1 solution

Why are you using the object objCommand, you should be using SQLCmd object.
That could be the problem here...please state the question clearly, I see this is the only issue here.
 
Share this answer
 
Comments
Andy Morris 29-Sep-11 22:26pm    
I'm quite new to VB.Net (crossing over from MS Access). Can you provide me a good reference or sample?
Pradeep Shukla 29-Sep-11 22:32pm    
your current code is all good, just replace the "objCommand" with "SQLCmd". This would help you to gear up...http://www.startvbdotnet.com/
Andy Morris 29-Sep-11 23:05pm    
Thanks for the help. One other question I am also using the "objCommand" on the Insert SQl statement, should I change that as well to the "SQLCommand"?
Pradeep Shukla 29-Sep-11 23:15pm    
Try to understand here...this replacement is done because "objCommand" object has no where been declared....If that applies to the "Insert" code, you should replace that too...
Andy Morris 29-Sep-11 23:18pm    
I see my error, I initialized the SQLcmd and ObjCommand both as New SQLCommand. I appreciate your help.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900