• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 672
  • Last Modified:

SQL data access layer helper class using SqlParameterCollection

I'm trying to create a canned function to handle all non-query, parameterized SQL statements.  I've come across the following function, however, I can't seem to use it without receiving errors.

------------------------------------------------------------------------------------------------------------------------------------
        Sub ExecuteNonQuery(ByVal sqlString As String, ByVal sqlParams As SqlParameterCollection, ByVal connectionString As String)
            Dim dbCon As New SqlConnection(connectionString)

            Dim cmdExp As SqlCommand
            cmdExp = New SqlCommand(sqlString, dbCon)

            Dim sqlParam As SqlParameter
            For Each sqlParam In sqlParams
                cmdExp.Parameters.Add(sqlParam)
            Next

            dbCon.Open()
            cmdExp.ExecuteNonQuery()
            dbCon.Close()
        End Sub
------------------------------------------------------------------------------------------------------------------------------------


---------------------------------------------------------------------------------------------------
'Code
---------------------------------------------------------------------------------------------------
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim oDA As New Globals.DataAccess.SQL
        Dim strSQL As String = "[Some SQL String]"
        Dim strConn As String = "[Some Connection String]"

        'This is where I'm stuck. I don't know how to add parameters to the collection.
        'Dim sqlParams As SqlParameterCollection


        oDA.ExecuteNonQuery(strSQL, sqlParams, strConn)
    End Sub



0
RobinsRL
Asked:
RobinsRL
1 Solution
 
SammyCommented:
You need an SQLCommand to add the parameters to
Dim objSqlCommand as SqlCommand=New SqlCommand
With ObjSqlCommand
.Parameters.Add("@SomeParam", SqlDbType.Int).Value = 0
End With

If you are using asp.net 2.0 you have to use
AddWithValue("@SomeParam", object Value)

Good luck
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Microsoft Office 2010

This course will introduce you to the interfaces and features of Microsoft Office 2010 Word, Excel, PowerPoint, Outlook, and Access. You will learn about the features that are shared between all products in the Office suite, as well as the new features that are product specific.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now