Results 1 to 4 of 4

Thread: updating parameterized Update Sql

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Jan 2003
    Posts
    175

    updating parameterized Update Sql

    i use the folowing update proedure
    Public Sub dgProducts_Update(ByVal obj As Object, ByVal e As DataGridCommandEventArgs)
    'Collecting Values
    Dim intProductId As Integer = e.Item.Cells(1).Text
    Dim dblPrice As Double = e.Item.Cells(2).Text
    Dim strName As String = CType(e.Item.Cells(3).Controls(0), TextBox).Text
    Dim strDesc As String = CType(e.Item.Cells(4).Controls(0), TextBox).Text

    'construct the Updating Sql Statement
    Dim StrUpdateSql As String
    StrUpdateSql = "Update Products " & _
    "Set ProductName = @ProductName, " & _
    "ProductDescription = @ProductDescription " & _
    "Where ProductId = @ProductID"

    'Creating Conections and open
    Const strConn As String = "Initial catalog = grocertogo;uid=sa;pwd=sa"
    Dim objConn As New SqlConnection(strConn)
    Dim objCommand As New SqlCommand(StrUpdateSql, objConn)
    objCommand.CommandType = CommandType.Text
    Try
    objConn.Open()
    Catch ex As Exception
    Response.Write(ex.Message)
    End Try

    'Adding parameters to the Sql
    objCommand.Parameters.Clear()
    Dim ParameterProdName As SqlParameter = New SqlParameter("@ProdName", SqlDbType.NVarChar, 75)
    ParameterProdName.Value = strName
    objCommand.Parameters.Add(ParameterProdName)

    'Dim ParameterUnitPrice As SqlParameter = New SqlParameter("@UnitPrice", SqlDbType.Money, 8)
    'ParameterUnitPrice.Value = dblPrice
    'objCommand.Parameters.Add(ParameterUnitPrice)

    Dim ParameterProdDescription As SqlParameter = New SqlParameter("@ProductDescription", SqlDbType.NVarChar, 50)
    ParameterProdDescription.Value = strDesc
    objCommand.Parameters.Add(ParameterProdDescription)

    Dim ParameterProdID As SqlParameter = New SqlParameter("@ProductID", SqlDbType.Int)
    ParameterProdID.Value = intProductId
    objCommand.Parameters.Add(ParameterProdID)

    Try
    objCommand.ExecuteNonQuery()
    objConn.Close()
    dgProducts1.EditItemIndex = -1
    BindData()
    Catch ex As Exception
    Response.Write(ex.Message)
    End Try
    End Sub

    and got the folowing error code
    Must declare the variable '@ProductName'.

    i had checked the command.commandtext and it doesnt puts the appropriate values in the parameters i guess it considering the strSQL as a whole string
    any help please???

  2. #2
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: updating parameterized Update Sql

    You've spelt it as @ProdName once.

  3. #3
    PowerPoster
    Join Date
    Nov 2001
    Location
    Trying to reach and stay in the cloud
    Posts
    2,089

    Re: updating parameterized Update Sql

    109 posts, and yet the code tags are missing

  4. #4
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: updating parameterized Update Sql

    LOL. I mean...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width