473,785 Members | 2,639 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

SqlDecimal.Null and DbNull.Value Problems

Hello
Could anyone tell me how to send a null decimal value to the sql
server depending on its value?
The following code gives me an error:
System.Data.Sql Types.SqlNullVa lueException: Data is Null. This method
or property cannot be called on Null values.

I figured out SqlDecimal.Null is used only with db Money fields. But
when I changed SqlDecimal.Null to DBNull.Value I got the error:
CS0173 Type of conditional expression can't be determined because
there is no implicit conversion between
'System.Data.Sq lTypes.SqlDecim al' and 'System.DBNull'

How should I rewrite this code to get it working?

Any help appreciated.
Daniel Zelisko
dzelisko AT go2.pl

(sql server field ProductPrice is decimal)
------------------- CODE STARTS HERE
------------------------------------

//variable declaration
protected Decimal _productPrice = new System.Decimal( );

.....
//(reading data)
//if _productPrice equals zero it should send a null value
myCommand.Param eters.Add("@Pro ductPrice", SqlDbType.Decim al).Value =
(_productPrice != 0 ? new SqlDecimal(_pro ductPrice) :
SqlDecimal.Null );

try
{
SqlDataAdapter adapter = new SqlDataAdapter( );
adapter.SelectC ommand = myCommand;
adapter.Fill(my DataSet);
}
catch (Exception es)
{
ErrorText = es.ToString();
}
.....
Nov 15 '05 #1
0 3580

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

Similar topics

7
2615
by: MP | last post by:
Hello, I am trying to write a class that will expose some properties. One of the property is extracted from a SQL database and can be NULL or some integer value. Is there a elegant way of implementing this in C# ? I do not want to use variant (or similar types) because I want to retain a strnong types property. Will I have no choices but to add another property that would set/get the NULL value?
1
1912
by: excelleinc.com | last post by:
Hi, I'm trying to check if field contains NULL value in MSSQl 2000 database but keep receiving error. asp.net code: If Trim(HLSQLDSet.Tables("mfglinks").Rows(15).Item(0)) Is Null Then URLSurgeS.Visible = "False" End If
2
1425
by: Mark | last post by:
Hi all, I have a DAL which takes in a value as shown below ready for my MSSQL stored procedure. protected SqlDecimal _annualLeave = SqlDecimal.Null; public SqlDecimal AnnualLeave { get {return _annualLeave;} set{ _annualLeave = value;}
15
29280
by: TC | last post by:
What does it mean for an integer to have a null value? I am trying to use the DataView.Find method. That method has an integer return type which contains the "index of the row in the DataView containing the sort key value specified; otherwise a null value if the sort key value does not exist." By "null value", does it mean System.DBNull? (I thought only objects could evaluate to System.DBNull.) How can I test whether an integer variable...
6
12997
by: Dean Slindee | last post by:
I am looking for the "right" way to handle inserting and presenting null date values. Public Const c_NullDate As Date = #12:00:00 AM# If I set the value of a date variable in an SQL Server insert statement to c_NullDate, the insert statement works. When I re-read the row and display the inserted date value in a text box, the string "1/1/1900" is displayed. If one wanted the textbox to display an empty string, how should either the insert...
17
1572
by: John | last post by:
Hi I have a datadapter with the following SQL; SELECT ID, Company, Status, CompanyType FROM Companies WHERE (@Status IS NULL or @Status = Status) When I try to fill like so; Me.CompanyTableAdapter.Fill(Me.MyDataSet.Clients, "Current"), it brings all
4
10698
by: Bill Gower | last post by:
Why won't this work? What do I need to do to make it work? DateTime? DateMember; if((DateTime.Parse(oldRow.ToString) == null)) DateMember = null; else DateMember = DateTime.Parse(oldRow.ToString()); The program is failing on the if saying that the null value in the field in
9
3279
by: GotDotNet? | last post by:
I have a dataset and I have to loop through it and some of the values for an insertition into the db. Some of the fields are integers and booleans but contain a NULL in the field. how can I check for a NULL and if its NULL insert DBNull into the db? Currently I'm getting error message System.DBNull.System.IConvertible.ToBoolean() now i need to get this field and insert a NULL into the db is the values
10
2472
by: =?Utf-8?B?R3JlZw==?= | last post by:
I have the following three files. 1. Users.aspx is a webpage that uses the <asp:ObjectDataSourcecontrol to populate a simple <asp:ListBoxcontrol. 2. The UserDetails.cs file creates a Namespace named UserComponents and creates an object named UserDetails. 3. The UserDB.cs file retrieves the actual data from the database. The code below has been condensed and only includes two fields from my datasource. I actually am also retrieving...
0
10336
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...
1
10095
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
8978
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...
1
7502
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
6741
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
5383
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
4054
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
3655
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2881
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.