Something in my C# class or the code using the class doesn't work

macwinlin

Senior member
Apr 11, 2002
523
0
76
I'm doing a really basic online survey application done using ASP.NET and VB.NET that I would like to show prospective employers since the one I've actually for a former employer cannot be shown readily.

So I've built this survey app using a C# class I found on the internet that has methods and properties relevant to accessing and storing information to a SQL Server DB. However, after entering information in the form and clicking the submit button, I get an error:

Exception Details: System.ArgumentNullException: The SqlParameterCollection only accepts non-null SqlParameter type objects. Parameter name: value

I wrote some test code to isolate the problem. And it works, saving the information I had entered. So it has to be the either code inside the class or code using the class that's the problem.

Please let me know if you need more code-I thought I'd post code that most likely contained the problem. Thanks in advance.
 

UCJefe

Senior member
Jan 27, 2000
302
0
0
That error pretty much means what it says. It means that a null SqlParameter object was attempted to be added to the command. So something like sqlcmd.Parameters.Add(null). So how big is your arrayParams array? Could it be bigger than 8 elements so that arrayParams.Length is returning something > 8 and you are attempting to add null parameter args to the command? Everything in the code you posted looks ok. Check arrayParams.Length and if you still haven't found it, post where you declare/re-size/mess with the arrayParams array.
 

macwinlin

Senior member
Apr 11, 2002
523
0
76
Originally posted by: UCJefe
That error pretty much means what it says. It means that a null SqlParameter object was attempted to be added to the command. So something like sqlcmd.Parameters.Add(null). So how big is your arrayParams array? Could it be bigger than 8 elements so that arrayParams.Length is returning something > 8 and you are attempting to add null parameter args to the command? Everything in the code you posted looks ok. Check arrayParams.Length and if you still haven't found it, post where you declare/re-size/mess with the arrayParams array.


Thank you for your reply. I looked at the code regarding arrayParams and it seems ok, at least to me. I declared an array of eight SqlParameters in my code-behind file, and I'm sure the number of SqlParameters is eight throughout. So here is more code regarding the use of an array of SqlParameters:
 

UCJefe

Senior member
Jan 27, 2000
302
0
0
Well that's your problem. In VB.NET, Dim arrayParams(8) declares an array of 9 elements. The number you put in parentheses always represents the value of the upper index, not the number of values like in almost every other language. Change that to arrayParams(7) and you should be good
 

Jim Bancroft

Senior member
Nov 9, 2004
212
2
81
Hi,

Are you sure all the web controls have been filled out when you submitted the form? If you left txtbxOrgName blank for instance, it might be translated as a null value; checking for null and "" can be two different things in .Net. That's bitten me before.

I'd test for both "" and null. Not sure how to do it in VB.Net but in C# you can do things along the lines of " if (myVariable == null) ....."
 

macwinlin

Senior member
Apr 11, 2002
523
0
76
Originally posted by: UCJefe
Well that's your problem. In VB.NET, Dim arrayParams(8) declares an array of 9 elements. The number you put in parentheses always represents the value of the upper index, not the number of values like in almost every other language. Change that to arrayParams(7) and you should be good

Thank you-it worked.

How embarassing. It was such a simple error. Perhaps I should stay with C# :eek:

Jim Bancroft: Thanks for your help as well. It wasn't the thing that produced the error (see UCJefe's response above), but it does give me something to check for in the future.