Question : Only one parameter passed to function but For Each...Next keeps going after first pass.  Can't understand why.

Can someone help me understand why this code isn't working?  I have used this function (RunProcedureDReader) repeatedly in my application with no problems but always (until now) passing multiple parameters to it.  Now for the first time (Function: AddressTypeReadAll) I'm passing only one parameter.  Ror some reason the "For Each...Next" loop doesn't stop after the parameter is processed.  On the second time through it generates an error (not surprising since there is only one parameter).  I don't even know how to begin debugging this but maybe I'm overlooking something simple.  Thanks for your help.

P.S.  The error occurs on the second execution of this line of code:

objCmd.Parameters.Add(parameter)

Here is the error message:

System.ArgumentNullException was unhandled by user code
  Message="The SqlParameterCollection only accepts non-null SqlParameter type objects. Parameter name: value"
  ParamName="value"
  Source="System.Data"
  StackTrace:
       at System.Data.SqlClient.SqlParameterCollection.ValidateType(Object value)
       at System.Data.SqlClient.SqlParameterCollection.Add(Object value)
       at System.Data.SqlClient.SqlParameterCollection.Add(SqlParameter value)
       at PETSAppeal.DBUtil.RunProcedureDReader(String storedProcName, IDataParameter[] parameters, String tableName) in C:\Users\Public\Documents\Visual Studio 2008\Websites\PETSAppeal_2_01\App_Code\DBUtil.vb:line 940
       at PETSAppeal.DBUtil.AddressTypeReadAll(String _langCult) in C:\Users\Public\Documents\Visual Studio 2008\Websites\PETSAppeal_2_01\App_Code\DBUtil.vb:line 354
       at Members_Profile.FillAddrTypeList() in C:\Users\Public\Documents\Visual Studio 2008\Websites\PETSAppeal_2_01\Members\Profile.aspx.vb:line 16
       at Members_Profile.Page_Load(Object sender, EventArgs e) in C:\Users\Public\Documents\Visual Studio 2008\Websites\PETSAppeal_2_01\Members\Profile.aspx.vb:line 493
       at System.Web.UI.Control.OnLoad(EventArgs e)
       at System.Web.UI.Control.LoadRecursive()
       at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
  InnerException:
Code Snippet:
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
Public Function AddressTypeReadAll(ByVal _langCult As String) As DataTable
	Dim dtAddrTypes As New DataTable
	Dim prmList(1) As SqlParameter

	prmList(0) = New SqlParameter("@LangCult", SqlDbType.VarChar, 15)

	prmList(0).Value = _langCult

	dtAddrTypes = RunProcedureDReader("dbo.procAddressType_GetAll", prmList, "tblAddrTypes")

	Return dtAddrTypes
End Function

Public Function RunProcedureDReader(ByVal storedProcName As String, _
									ByVal parameters As IDataParameter(), _
									ByVal tableName As String) As DataTable

	Dim dt As New DataTable
	Dim objCmd As New SqlCommand(storedProcName, strConn)
	Dim parameter As SqlParameter

	objCmd.CommandType = CommandType.StoredProcedure

	For Each parameter In parameters
		objCmd.Parameters.Add(parameter)
	Next

	strConn.Open()

	Dim dr As SqlDataReader = objCmd.ExecuteReader()
	dt.Load(dr)
	strConn.Close()
	Return dt
End Function

Answer : Only one parameter passed to function but For Each...Next keeps going after first pass.  Can't understand why.

It appears that you are declaring an array that holds two values:

Dim prmList(1) As SqlParameter

When your function runs, it tries to process the 2nd parameter which is NULL.

I am not versed in VB.NET, so that is just a guess.
Random Solutions  
 
programming4us programming4us