reducing some array lines of code

E

Eric Sabine

Below is a sub I have which contains 4 lines of code. I would like to
reduce it to 1. My problem is that it takes 3 lines to create the array to
hold the sql parameter and since there's only one dimension in the array,
I'd like to do something more like what's at the bottom of this post, but a
lack of understanding prevents me from doing it properly. Any suggestions?

Sub getUserVendorAssignment(ByVal UserName As String)
' fill the table UserVendor in the local dataset with the
' results from the usp_dr_userVendorReceivingAssignments stored
procedure
' Pass in one parameter item which is received as a string

' 3 lines to create our parameter?
Dim arParms() As System.Data.SqlClient.SqlParameter = _
New System.Data.SqlClient.SqlParameter(0) {}
arParms(0) = New SqlParameter("@user_name", SqlDbType.VarChar, 8)
arParms(0).Value = UserName .ToUpper

localFillDataset( _
"dbo.usp_dr_userVendorReceivingAssignments", _
"UserVendor", _
arParms)
End Sub

--------------------
what I'd _like_ to do (I know it's wrong, it's just the "guess")
localFillDataset( _
"dbo.usp_dr_userVendorReceivingAssignments", _
"UserVendor", _
New SqlParameter() {CType(UserName , SqlParameter)})
 
A

Armin Zingler

Eric Sabine said:
' Pass in one parameter item which is received as a string

' 3 lines to create our parameter?
Dim arParms() As System.Data.SqlClient.SqlParameter = _
New System.Data.SqlClient.SqlParameter(0) {}
arParms(0) = New SqlParameter("@user_name", SqlDbType.VarChar,
8) arParms(0).Value = UserName .ToUpper

You could write a function returning the array.


--
Armin

How to quote and why:
http://www.plig.net/nnq/nquote.html
http://www.netmeister.org/news/learn2quote.html
 
E

Eric Sabine

Thanks Armin, I saw your response and I kept hammering at it. I did get to
acheive the 1 line of code that I wanted :)

This is what I ended up doing

localFillDataset( _
"dbo.usp_dr_userVendorReceivingAssignments", _
"UserVendor", _
New SqlParameter("@user_name", UserName))

and I changed the signature of localFillDataset to the following (I added
the "ParamArray")

Sub localFillDataset(ByVal ProcedureName As String, _
ByVal TableName As String, _
ByVal ParamArray prms() As System.Data.SqlClient.SqlParameter)


My question to you is, was the absence of ParamArray in the first place
incorrect? The IDE didn't seem to complain when I left it out. Actually,
everything worked fine with the earlier code.

Eric
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top