Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 SQL Server Development (2000)
 C# Webforms and SQL Stored Procedure

Author  Topic 

Sirrombor
Starting Member

12 Posts

Posted - 2002-07-09 : 12:38:20
Hello,

I am trying submit values to a stored procedure using Webforms in C#...

On the form I have a radio button list that is optional for the user to select a value...

protected System.Web.UI.WebControls.RadioButtonList RadiocustSpouseStatus;

In my command parameters I am passing the radio list value to a stored procedure that inserts into an SQL server column that has no required constraints and accepts NULLS...

cmd.Parameters.Add("@custSpouseStatus", System.Data.SqlDbType.VarChar).Value = RadiocustSpouseStatus.SelectedItem.Value;

I think my error is when the radio button isnt selected and doesn't receive a value I get this message...

System.NullReferenceException: Object reference not set to an instance of an object.


How do tell my code to ignore that command parameter if the user hasn't selected anything.. Or do I need to assign a value if not checked and where would I do this, on the webform or in the Stored Procedure..

I hope this makes sense if not I can post my code... Just ask :)

setbasedisthetruepath
Used SQL Salesman

992 Posts

Posted - 2002-07-09 : 13:03:23
You need to check if RadiocustSpouseStatus.SelectedItem = NULL before adding it to the command object's parameters collection.

Jonathan Boott, MCDBA
Go to Top of Page

joldham
Wiseass Yak Posting Master

300 Posts

Posted - 2002-07-09 : 16:30:05
As setbased said, you need to check to see if the value is null before you add the parameter.
Visual Basic .NET no longer supports the IsNull function. To fix, replace IsNull with IsDBNull.

[url]http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dndotnet/html/convertasptoaspnet.asp[/url]

Jeremy



Edited by - joldham on 07/09/2002 16:30:48

Edited by - joldham on 07/09/2002 16:32:20
Go to Top of Page

chadmat
The Chadinator

1974 Posts

Posted - 2002-07-09 : 17:00:14
You could also place that code in a try/catch block, and catch that particular exception. Then react appropriately.

-Chad

Go to Top of Page

Onamuji
Aged Yak Warrior

504 Posts

Posted - 2002-07-09 : 19:15:20
That goes against performance since exceptions are costly. If you look on MSDN they suggest if you can fix the error instead of using and exception that you should. Just search MSDN for ASP.NET Optimizations.

Go to Top of Page

Sirrombor
Starting Member

12 Posts

Posted - 2002-07-10 : 12:59:02
Does anyone have a link or an example?

Go to Top of Page

royv
Constraint Violating Yak Guru

455 Posts

Posted - 2002-07-10 : 13:17:23
Onamuji, does it say in MSDN that using exception code is a big performance hit? Can you provide me a link?

*************************
Someone done told you wrong!
Go to Top of Page

setbasedisthetruepath
Used SQL Salesman

992 Posts

Posted - 2002-07-10 : 13:48:06
I would generally agree w/ Onamuji. An exception should only occur in exceptional circumstances. If you know that an object reference might be invalid and decide to reference it anyway, the resulting exception is more resource intensive to handle versus checking the validity of the reference beforehand.

Jonathan Boott, MCDBA
Go to Top of Page
   

- Advertisement -