Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C# 1.0 > C#
|
C# Programming questions specific to the Microsoft C# language. See also the forum Beginning Visual C# to discuss that specific Wrox book and code.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C# section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old December 4th, 2006, 11:29 AM
su su is offline
Authorized User
 
Join Date: Nov 2006
Posts: 17
Thanks: 0
Thanked 0 Times in 0 Posts
Default how to capture null value(ifpresent) from database




I am using C#.NET, Visual Web Develpoper, SQL Server 2000.

In my WebForm, I am trying to get the value from database using following code : Where OSP_OrderNum is the column name in database.

// Get the value of the OSP_OrderNum field.

ordernum = (string)DataBinder.Eval(e.Item.DataItem, "OSP_OrderNum");

Here I am trying to insert the value of OSP_OrderNum into a dropdownlist of a DataGrid. Now what I would like to do is

if (ordernum == " ")

{

insert Text : "**Select Order**" into DDL

}

else

{

insert : "**Select 'ordernum**' order"

}

When I am using this loop my code breaks on the 1st line itself where I have declared ordernum. It is not able to capture null value from the database and giving me error: Unable to cast object of type 'System.DBNull' to type 'System.String'

How do I Solve this ?

Please Help!!!!
 
Old December 4th, 2006, 11:46 AM
joefawcett's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

A blank space or an empty string is not the same as a NULL in a database. The error message was trying to be helpful, for once, you need to compare to System.DBNull.Value.

--

Joe (Microsoft MVP - XML)
 
Old December 4th, 2006, 12:23 PM
su su is offline
Authorized User
 
Join Date: Nov 2006
Posts: 17
Thanks: 0
Thanked 0 Times in 0 Posts
Default

How and where do I compare with System.DBNull.Value ?


Code does not go to if loop. Code breaks before it on the following line.
ordernum = (string)DataBinder.Eval(e.Item.DataItem, "OSP_OrderNum");

I am inserting this value in DDL of DataGrid.
 
Old December 4th, 2006, 12:58 PM
joefawcett's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

Sorry, I misread where the error appeared. You need to store the value of DataBinder.Eval(e.Item.DataItem, "OSP_OrderNum") in a variable and compare to System.DBNull.Value.


--

Joe (Microsoft MVP - XML)
 
Old December 4th, 2006, 01:36 PM
su su is offline
Authorized User
 
Join Date: Nov 2006
Posts: 17
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi joefawcett,

How should I compare and where as my code does not reach till if loop?

ordernum = (string)DataBinder.Eval(e.Item.DataItem, "OSP_OrderNum");

if (ordernum == System.DBNull.Value)

{

insert Text : "**Select Order**" into DDL

}
 
Old December 7th, 2006, 04:29 AM
Authorized User
 
Join Date: Apr 2006
Posts: 36
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to vivekshah Send a message via Yahoo to vivekshah
Default

Hello Su,

In your sql query you can use

ISNULL(OSP_OrderNum,0)

The about will check if the value in OSP_OrderNum is NULL then it will replace the value with zero(0).

Hope this solves the problem you are facing

Vivek Shah
 
Old December 15th, 2006, 09:02 AM
Authorized User
 
Join Date: Dec 2006
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
Default

hi, i m new member.
    my problem is, i am using datagrid and in that i am showing the members information. In this information i used one field called website which is populated from database using Databinder.Eval() Method. Now i want to hide that particular control for every member whose website field is null. For displaying the website field i used the hyperlink control in template column. plz send me
some help for this asap.
thanks in advance.

Sachin







Similar Threads
Thread Thread Starter Forum Replies Last Post
capture URL SKhna ASP.NET 2.0 Basics 1 February 5th, 2008 11:02 AM
Access Database & Null Values jroxit Classic ASP Databases 4 January 9th, 2007 09:34 PM
How to store a capture packet into database leenhassan VB How-To 0 March 6th, 2006 05:41 AM
How to set Not Null constraint to Null Columns arasu Oracle 1 August 22nd, 2005 10:09 AM
insert null value into database akibaMaila VB.NET 2002/2003 Basics 1 August 15th, 2005 05:44 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.