Wednesday, March 21, 2012

Parameter help

Object reference is not an object....

Errors on .Initialize() and in the debugger parmlist has a value of Nothing when it gets to that line. Where am I going wrong here?


Public Shared Function AddUserInfo(ByVal user As User)
Dim strSQL As String = "EmployeeInfo_Intranet_AddUser"

Dim parmlist As SqlParameterCollection()
parmlist.Initialize()

With user
parmlist(0).Add(New SqlParameter("@dotnet.itags.org.WindowsLogin", .windowsLogin))
parmlist(1).Add(New SqlParameter("@dotnet.itags.org.Firstname", .FirstName))
parmlist(2).Add(New SqlParameter("@dotnet.itags.org.Lastname", .LastName))
parmlist(3).Add(New SqlParameter("@dotnet.itags.org.Email", .emailAddress))
parmlist(4).Add(New SqlParameter("@dotnet.itags.org.Title", .Title))
parmlist(5).Add(New SqlParameter("@dotnet.itags.org.PriorityApp", .priorityApp))
End With
SqlHelper.ExecuteNonQuery(ConfigurationSettings.AppSettings("connString"), CommandType.StoredProcedure, strSQL, parmlist)
End Function

try this

Dim parmlist AsNEW SqlParameterCollection()
That gives an error.

'System.Data.SqlClient.SqlParameterCollection.Private Overloads Sub New(parent As System.Data.SqlClient.SqlCommand)' is not accessible in this context because it is 'Private'.
Ok I found out you can't use the SqlParameterCollection with the Data Access Application Block.

It looks like I have to do something like this...


Dim parmArray() As SqlParameter = New SqlParameter(5) {}
parmArray(0) = New SqlParameter("@.WindowsLogin", .login)
parmArray(1) = New SqlParameter("@.FirstName", .firstname)

And build it into a loop with passed values so it can be used elsewhere.
Dim parmlist As SqlParameterCollection() = new SqlParameterCollection()
me titus - again that gives an error when you try to declare it in VS.NET, thanks for trying though!

This works:


Public Shared Function AddUserInfo(ByVal user As User)
Dim strSQL As String = "EmployeeInfo_Intranet_AddUser"
Dim parmArray() As SqlParameter = New SqlParameter(5) {}
With user
parmArray(0) = New SqlParameter("@.WindowsLogin", .windowsLogin)
parmArray(1) = New SqlParameter("@.Firstname", .FirstName)
parmArray(2) = New SqlParameter("@.Lastname", .LastName)
parmArray(3) = New SqlParameter("@.Email", .emailAddress)
parmArray(4) = New SqlParameter("@.Title", .Title)
parmArray(5) = New SqlParameter("@.PriorityApp", .priorityApp)
End With
SqlHelper.ExecuteNonQuery(ConfigurationSettings.AppSettings("connString"), CommandType.StoredProcedure, strSQL, parmArray)
End Function

Although I'll build a class now that will build up the array based on what's passed to it.

Thanks for the input everyone.
This works:


Dim strSql As String = "select * from products where categoryid = @.CategoryID and supplierid = @.SupplierID"
Dim strConnTxt As String = "Server=(local);Database=Northwind;Integrated Security=True;"
DataGrid4.DataSource = SqlHelper.ExecuteReader(strConnTxt, CommandType.Text, strSql, _
New SqlParameter("@.CategoryID", 1), _
New SqlParameter("@.SupplierID", 16))
DataGrid4.DataBind()

0 comments:

Post a Comment