|
-
Jan 27th, 2005, 04:28 AM
#1
Thread Starter
Addicted Member
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???
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
|