473,788 Members | 3,053 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 5868
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
5380
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
1133
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
2090
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
1489
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
3651
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
9655
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
10363
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10172
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...
0
9964
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...
1
7517
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6749
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5398
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4069
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
3
2894
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.