how to update record

Discussion in 'C#' started by kharearch, Apr 25, 2008.

  1. #1
    I am writing following code to update record but it is not updating. Please help me to solve this problem.

    Dim objcommand As SqlCommand
    ' Dim objtextbox As TextBox
    Dim str As String = "update my set age = @age where name = @name"
    objcommand = New SqlCommand(str, objconnection)
    objcommand.Parameters.Add(New SqlParameter("@age", SqlDbType.Int, 4))
    objcommand.Parameters.Add(New SqlParameter("@name", SqlDbType.Char, 2))
    objcommand.Parameters("@age").Value = TextBox2.Text
    objcommand.Parameters("@name").Value = TextBox1.Text
    objcommand.Connection = objconnection
    objcommand.Connection.Open()
    objcommand.ExecuteNonQuery()
     
    kharearch, Apr 25, 2008 IP
  2. dgxshiny

    dgxshiny Greenhorn

    Messages:
    65
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    23
    #2
    Parameters.Add is a depreciated function, I would switch to .AddWithValue


    Dim objcommand As SqlCommand
    ' Dim objtextbox As TextBox
    Dim str As String = "update my set age = @age where name = @name"
    objcommand = New SqlCommand(str, objconnection)
    objcommand.Parameters.AddWithValue("@age", TextBox2.Text)
    objcommand.Parameters.AddWithValue("@name", TextBox1.Text)
    objcommand.Connection = objconnection
    objcommand.Connection.Open()
    objcommand.ExecuteNonQuery()
    objcommand.Connection.Close()
     
    dgxshiny, Apr 25, 2008 IP