 |
| 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
|
|
|
|

December 4th, 2006, 11:29 AM
|
|
Authorized User
|
|
Join Date: Nov 2006
Posts: 17
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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!!!!
|
|

December 4th, 2006, 11:46 AM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
|
|
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)
|
|

December 4th, 2006, 12:23 PM
|
|
Authorized User
|
|
Join Date: Nov 2006
Posts: 17
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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.
|
|

December 4th, 2006, 12:58 PM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
|
|
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)
|
|

December 4th, 2006, 01:36 PM
|
|
Authorized User
|
|
Join Date: Nov 2006
Posts: 17
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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
}
|
|

December 7th, 2006, 04:29 AM
|
|
Authorized User
|
|
Join Date: Apr 2006
Posts: 36
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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
|
|

December 15th, 2006, 09:02 AM
|
|
Authorized User
|
|
Join Date: Dec 2006
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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
|
|
 |