|
-
Feb 1st, 2006, 02:46 PM
#1
Thread Starter
Giants World Champs!!!!
[RESOLVED] ADO.NET Update Code with Parameter - How?
Here is a snippet of code that I am using to update a row in a DB:
Code:
Dim MyCONNECTIONSTRING As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\test.mdb"
Dim DBConnection As New OleDb.OleDbConnection(MyCONNECTIONSTRING)
Dim strSQL As String = "UPDATE Customers SET Rank = + @SearchFor + WHERE ID=1;"
Dim OLECmd As New OleDb.OleDbCommand(strSQL)
Dim SelectParameter As OleDb.OleDbParameter = OLECmd.CreateParameter
SelectParameter.ParameterName = "@SearchFor"
SelectParameter.Value = "Test Rank"
OLECmd.Parameters.Add(SelectParameter)
DBConnection.Open()
OLECmd.ExecuteNonQuery()
I can't seem to be able to get this code to work by using the Parameter, any ideas?
Thanks!
Regards,
Mark
Please remember to rate posts! Rate any post you find helpful. Use the link to the left - "Rate this Post". Please use [highlight='vb'] your code goes in here [/highlight] tags when posting code. When a question you asked has been resolved, please go to the top of the original post and click "Thread Tools" then select "Mark Thread Resolved."
-
Feb 1st, 2006, 03:53 PM
#2
Fanatic Member
Re: ADO.NET Update Code with Parameter - How?
I have not coded statements in this manner, but I would guess that you should change strSQL to take out the + signs. I usually just use a ? in mine and then make sure that if there are multiple parameters that you put them in the correct order.
I usually create my parameters as follows:
Code:
cmd.Parameters.Add(New OldDb.OleDbParameter("@CodeNo", SqlDbType.SmallInt, 2, "CodeNo")).Value = "Test Rank"
That just saves you from having to create and deal with a Parameter object. It is all created and added in one line.
-
Feb 1st, 2006, 07:41 PM
#3
Re: ADO.NET Update Code with Parameter - How?
Shouldn't it be...
Code:
Dim MyCONNECTIONSTRING As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\test.mdb"
Dim DBConnection As New OleDb.OleDbConnection(MyCONNECTIONSTRING)
Dim strSQL As String = "UPDATE Customers SET Rank = ? WHERE ID=1;"
Dim OLECmd As New OleDb.OleDbCommand(strSQL)
Dim SelectParameter As OleDb.OleDbParameter = OLECmd.CreateParameter
SelectParameter.ParameterName = "Rank"
SelectParameter.Value = "Test Rank"
OLECmd.Parameters.Add(SelectParameter)
DBConnection.Open()
OLECmd.ExecuteNonQuery()
? or @Rank
-
Feb 1st, 2006, 07:44 PM
#4
Fanatic Member
Re: ADO.NET Update Code with Parameter - How?
I don't think so. If you name the parameter @SearchFor in the SQL statement, then you have to set the command object's ParameterName property equal to the same name.
-
Feb 1st, 2006, 07:55 PM
#5
Re: ADO.NET Update Code with Parameter - How?
That's how I did it here though I maybe missing something since I am just starting with C#...
-
Feb 2nd, 2006, 06:13 AM
#6
Thread Starter
Giants World Champs!!!!
Re: ADO.NET Update Code with Parameter - How?
dee-u,
I tried your code but I had to add "@Rank" in order to get it to work.
Code:
Dim MyCONNECTIONSTRING As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\test1.mdb"
Dim DBConnection As New OleDb.OleDbConnection(MyCONNECTIONSTRING)
Dim strSQL As String = "UPDATE Customers SET Rank = @Rank WHERE ID=1;"
Dim OLECmd As New OleDb.OleDbCommand(strSQL)
Dim SelectParameter As OleDb.OleDbParameter = OLECmd.CreateParameter
SelectParameter.ParameterName = "@Rank"
SelectParameter.Value = "Test Rank"
OLECmd.Parameters.Add(SelectParameter)
DBConnection.Open()
OLECmd.ExecuteNonQuery() '<===Error
But I am getting an error on the last line: "ExecuteNonQuery: Connection property has not been initialized."
Any ideas?
Regards,
Mark
Please remember to rate posts! Rate any post you find helpful. Use the link to the left - "Rate this Post". Please use [highlight='vb'] your code goes in here [/highlight] tags when posting code. When a question you asked has been resolved, please go to the top of the original post and click "Thread Tools" then select "Mark Thread Resolved."
-
Feb 2nd, 2006, 06:21 AM
#7
Re: ADO.NET Update Code with Parameter - How?
At no point have you assigned your connection object to the Connection property of the command object. You must either pass it as a parameter in the constructor or set the property explicitly. Also, you can use un-named parameters (?) with any ParameterName you like. As was said, you just need to make sure they are added in the same order as they appear in the SQL code. In fact, if you access SQL Server via OleDb then you cannot used named parameters and you must use "?" place-holders.
-
Feb 2nd, 2006, 06:22 AM
#8
Re: ADO.NET Update Code with Parameter - How?
Modify this statement to look like this
Code:
Dim OLECmd As New OleDb.OleDbCommand(strSQL, DBConnection)
Use [code] source code here[/code] tags when you post source code.
My Articles
-
Feb 2nd, 2006, 06:43 AM
#9
Thread Starter
Giants World Champs!!!!
Re: ADO.NET Update Code with Parameter - How?
 Originally Posted by Shuja Ali
Modify this statement to look like this
Code:
Dim OLECmd As New OleDb.OleDbCommand(strSQL, DBConnection)
Shuja,
Thank, that did the trick!
Mark
Regards,
Mark
Please remember to rate posts! Rate any post you find helpful. Use the link to the left - "Rate this Post". Please use [highlight='vb'] your code goes in here [/highlight] tags when posting code. When a question you asked has been resolved, please go to the top of the original post and click "Thread Tools" then select "Mark Thread Resolved."
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|