When iParentID=30 and strTexte="A2LA Website"
What an I doing wrong?
Table:
ID int(identity)
ParentID int
Text nvarchar(50)
Valeur nvarchar(50) Allow Null
Ordre int Allow Nulls
(1) when you add parameters, specify their type/lengths too
Public Sub addItemToDdSelection(ByVal strTable As String, ByVal iParentID As Int16, ByVal strTexte As String)
Dim connect As New SqlConnection(strConnection)
Dim cmdSelect As New SqlCommand("AddDropDownContent", connect)
Dim paramReturnValue As SqlParameter
Dim strError As StringcmdSelect.CommandType = CommandType.StoredProcedure
'PARAM
cmdSelect.Parameters.Add("@.intParentID", iParentID)
cmdSelect.Parameters.Add("@.strTexte", strTexte)
Try
connect.Open()
cmdSelect.ExecuteNonQuery()
connect.Close()
Catch ex As ExceptionstrError = ex.Message
Finally
If connect.State = ConnectionState.Open Then
connect.Close()
End If
End Try
End SubALTER PROCEDURE dbo.AddDropDownContent
(
@.intParentID int,
@.strTexte varchar(50)
)
AS
EXEC ('INSERT INTO DropDownMenus (ParentID, Texte) VALUES(' + @.intParentID + ',"' + @.strTexte + '")')
xample :
cmdSelect.Parameters.Add("@.intParentID", iParentID)should be
cmdSelect.Parameters.Add(New SqlParameter("@.intParentID",SqlDbType.int))
cmdSelect.Parameters("@.intParentID").Value = Trim(iParentID)
and
(2) you dont need dynamic sql to insert. you can modify your insert stmt as
Create Procedure ...
AS
SET NOCOUNT ON
INSERT INTO DropDownMenus (ParentID, Texte) VALUES ( @.intParentID ,@.strTexte )SET NOCOUNT OFF
hth|||Thanks for your reply this is really helping. A few questions,
-Trim(iParentID) is used in case the id is enter from a text box I guess?
-What is the role of the line "SET NOCOUNT ON" and "SET NOCOUNT OFF"?|||>>-Trim(iParentID) is used in case the id is enter from a text box I guess?
yes. you might also want to do a
Convert.ToInt16(Trim(iParentId))to make sure you are sending in an integer type value. Its a good idea to validate user input.
>>What is the role of the line "SET NOCOUNT ON" and "SET NOCOUNT OFF"?
read up BOL (Books On Line)
hth
No comments:
Post a Comment