473,772 Members | 2,388 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Problem with the SQLParameterCol lection object

I have the following code that adds SQLParameter objects to an
SQLParameterCol lection object (well at least that's what I am trying to
do!!)

When the line of code runs that adds the parameter (colParams.Add) , the
actual function call (CreateSQLParam ) executes with no errors but the actual
".add" fails with an "Object reference not set to an instance of an object"
error. At first I thought it was maybe because I did not use the "New"
keyword when dimming colParams but there is no constructor for an
SQLParameterCol lection object so that was not the problem. The funny thing
was, this code worked EXACTLY as it is listed below EXCEPT I was using a
standard collection object previously instead of the SQLParameterCol lection
object. As soon as I converted it to the SQLParamaterCol lection object, I
started getting this error. I can not see where I am going awry but it must
be something quite simple!!!

Here is the code...

Dim colParams As SqlClient.SqlPa rameterCollecti on

' Set up the parameters

colParams.Add(C reateSQLParam(" @vcContractNo", ContractNo, SqlDbType.VarCh ar,
ParameterDirect ion.Input))

Function CreateSQLParam( ByVal sName As String, ByVal sValue As Object, ByVal
varType As System.Data.Sql DbType, ByVal varDir As ParameterDirect ion) As
SqlClient.SqlPa rameter

Dim objParam As SqlClient.SqlPa rameter

objParam = New SqlClient.SqlPa rameter()

objParam.Parame terName = sName

If IsNothing(sValu e) Then sValue = System.DBNull.V alue

objParam.Value = sValue

objParam.SqlDbT ype = varType

objParam.Direct ion = varDir

CreateSQLParam = objParam

End Function
Jul 13 '07 #1
2 5867
On 13 Lug, 21:21, "Brad Pears" <br...@truenort hloghomes.comwr ote:
I have the following code that adds SQLParameter objects to an
SQLParameterCol lection object (well at least that's what I am trying to
do!!)

When the line of code runs that adds the parameter (colParams.Add) , the
actual function call (CreateSQLParam ) executes with no errors but the actual
".add" fails with an "Object reference not set to an instance of an object"
error. At first I thought it was maybe because I did not use the "New"
keyword when dimming colParams but there is no constructor for an
SQLParameterCol lection object so that was not the problem. The funny thing
was, this code worked EXACTLY as it is listed below EXCEPT I was using a
standard collection object previously instead of the SQLParameterCol lection
object. As soon as I converted it to the SQLParamaterCol lection object, I
started getting this error. I can not see where I am going awry but it must
be something quite simple!!!

Here is the code...

Dim colParams As SqlClient.SqlPa rameterCollecti on

' Set up the parameters

colParams.Add(C reateSQLParam(" @vcContractNo", ContractNo, SqlDbType.VarCh ar,
ParameterDirect ion.Input))

Function CreateSQLParam( ByVal sName As String, ByVal sValue As Object, ByVal
varType As System.Data.Sql DbType, ByVal varDir As ParameterDirect ion) As
SqlClient.SqlPa rameter

Dim objParam As SqlClient.SqlPa rameter

objParam = New SqlClient.SqlPa rameter()

objParam.Parame terName = sName

If IsNothing(sValu e) Then sValue = System.DBNull.V alue

objParam.Value = sValue

objParam.SqlDbT ype = varType

objParam.Direct ion = varDir

CreateSQLParam = objParam

End Function

Dim SqlDataAdapter As New SqlClient.SqlDa taAdapter 'Use yours

'Dim colParams As New SqlClient.SqlPa rameterCollecti on

' Set up the parameters

SqlDataAdapter. SelectCommand.P arameters.Add(C reateSQLParam(" @vcContractNo",
ContractNo, SqlDbType.VarCh ar, ParameterDirect ion.Input))
see:
http://msdn2.microsoft.com/en-us/lib...ollection.aspx

Tommaso

Jul 13 '07 #2
On Fri, 13 Jul 2007 15:21:17 -0400, "Brad Pears"
<br***@truenort hloghomes.comwr ote:
>I have the following code that adds SQLParameter objects to an
SQLParameterCo llection object (well at least that's what I am trying to
do!!)

When the line of code runs that adds the parameter (colParams.Add) , the
actual function call (CreateSQLParam ) executes with no errors but the actual
".add" fails with an "Object reference not set to an instance of an object"
error. At first I thought it was maybe because I did not use the "New"
keyword when dimming colParams but there is no constructor for an
SQLParameterCo llection object so that was not the problem. The funny thing
was, this code worked EXACTLY as it is listed below EXCEPT I was using a
standard collection object previously instead of the SQLParameterCol lection
object. As soon as I converted it to the SQLParamaterCol lection object, I
started getting this error. I can not see where I am going awry but it must
be something quite simple!!!

Here is the code...

Dim colParams As SqlClient.SqlPa rameterCollecti on

' Set up the parameters

colParams.Add( CreateSQLParam( "@vcContractNo" , ContractNo, SqlDbType.VarCh ar,
ParameterDirec tion.Input))
You declared colParams but never set it to anything, so it has the
value Nothing. If you stepped through this with the debugger you
would see that.

Normally you would fix this by:

Dim colParams As SqlClient.SqlPa rameterCollecti on = New
SqlClient.SqlPa rameterCollecti on()

However, SqlParameterCol lection doesn't have an externally visible
constructor, so you can't create a stand-alone collection. Instead
create a SqlCommand and call the Add method of its Parameters property
passing the return value from CreateSqlParm:

Dim sqlCmd as SqlCommand = New SqlCommand()

sqlCmd.Paramete rs.Add(CreateSq lParm("@vcContr actNo", ...))
Jul 13 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
5378
by: Bob | last post by:
I have an ASP.NET web application that has been running without any problems for a while. I recently transferred the site to shared hosting and had multiple users start to use the site. The problem I'm experiencing is that when many users are hitting the site at once, occasionaly I will see errors. The 3 most common ones are: "The SqlCommand is currently busy Open, Fetching. ", "Internal Connection Fatal Error", "object reference not set to...
4
2404
by: Leslaw Bieniasz | last post by:
Cracow, 20.09.2004 Hello, I need to implement a library containing a hierarchy of classes together with some binary operations on objects. To fix attention, let me assume that it is a hierarchy of algebraic matrices with the addition operation. Thus, I want to have a virtual base class class Matr;
0
816
by: Elliot M. Rodriguez | last post by:
This might be architecturally related, but I'm hoping there is an easy way out that I am just missing. Its hard to explain, so please bear with me. I have a DataAccessLayer class. In it, I have a function that returns a SqlParameterCollection. The idea is I could pass in the name of a procedure and X number of parameters (whether input or output), and return back the SqlParameterCollection that is assigned to the DataAccessLayer...
0
1131
by: simon | last post by:
I have collection: Dim myParams As New Collection Then I fill it with parameters. Now, I would like to use it more than 1 time. When I create first dataAdapter, it works:
6
2347
by: TPJ | last post by:
Help me please, because I really don't get it. I think it's some stupid mistake I make, but I just can't find it. I have been thinking about it for three days so far and I still haven't found any solution. My code can be downloaded from here: http://www.tprimke.net/konto/PyObject-problem.tar.bz2. There are some scripts for GNU/Linux system (bash to be precise). All you need to know is that there are four classes. (Of course, you may...
1
2089
by: Thomee Wright | last post by:
I'm having a problem with a pair of applications I'm developing. I have a server application which interacts with several instruments, and a client app which connects to the server and provides a UI for interacting with the instruments. Readings from the instruments are periodically sent to the clients, and clients will send commands to the server based on user input. In normal usage there will be around 6 clients connected, with a...
1
5012
by: Dhananjay | last post by:
Hi all I have got this error An SqlParameter with ParameterName '@employment' is not contained by this SqlParameterCollection in asp.net
4
1488
by: sweatha | last post by:
Hi I have to insert the commision value and date into the database. For that I have given the coding as Imports System.Data Imports System.Configuration Imports System.Data.SqlClient Partial Class commision1 Inherits System.Web.UI.Page Public strConnection As String
3
3649
by: kpeeroo | last post by:
Private Function AddCompanyOvertime() As Integer Dim companyID As Integer = GetCompanyID() Console.WriteLine(companyID) Dim paramCompanyID As New SqlParameter("@CompanyID", SqlDbType.Int) Dim paramCompanyOvertimeID As New SqlParameter("@companyOvertimeID", SqlDbType.Int) Dim paramDate As New SqlParameter("@Date", SqlDbType.DateTime) Dim paramOvertimePrice As New SqlParameter("@OvertimePrice",...
0
9621
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9454
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10106
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10039
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9914
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8937
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5484
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4009
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3610
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.