Results 1 to 9 of 9

Thread: [RESOLVED] ADO.NET Update Code with Parameter - How?

  1. #1
    Giants World Champs!!!! Mark Gambo's Avatar
    Join Date
    Sep 03
    Location
    Colorado
    Posts
    2,965

    Resolved [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."


  2. #2
    Fanatic Member
    Join Date
    May 03
    Posts
    749

    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.

  3. #3
    Software Carpenter dee-u's Avatar
    Join Date
    Feb 05
    Location
    Philippines
    Posts
    10,221

    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

  4. #4
    Fanatic Member
    Join Date
    May 03
    Posts
    749

    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.

  5. #5
    Software Carpenter dee-u's Avatar
    Join Date
    Feb 05
    Location
    Philippines
    Posts
    10,221

    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#...

  6. #6
    Giants World Champs!!!! Mark Gambo's Avatar
    Join Date
    Sep 03
    Location
    Colorado
    Posts
    2,965

    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."


  7. #7
    .NUT jmcilhinney's Avatar
    Join Date
    May 05
    Location
    Sydney, Australia
    Posts
    80,816

    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.

  8. #8
    Shared Member
    Join Date
    May 05
    Location
    Kashmir, India
    Posts
    2,277

    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

  9. #9
    Giants World Champs!!!! Mark Gambo's Avatar
    Join Date
    Sep 03
    Location
    Colorado
    Posts
    2,965

    Re: ADO.NET Update Code with Parameter - How?

    Quote 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
  •