Subject: how to capture null value(ifpresent) from database
Posted By: su Post Date: 12/4/2006 10:29:36 AM



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!!!!
Reply By: joefawcett Reply Date: 12/4/2006 10:46:36 AM
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)
Reply By: su Reply Date: 12/4/2006 11:23:01 AM
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.
Reply By: joefawcett Reply Date: 12/4/2006 11:58:06 AM
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)
Reply By: su Reply Date: 12/4/2006 12:36:06 PM
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

}
Reply By: vivekshah Reply Date: 12/7/2006 3:29:56 AM
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
Reply By: SachinMalge Reply Date: 12/15/2006 8:02:30 AM
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



Go to topic 51529

Return to index page 92
Return to index page 91
Return to index page 90
Return to index page 89
Return to index page 88
Return to index page 87
Return to index page 86
Return to index page 85
Return to index page 84
Return to index page 83