Home > Forum > ASP.NET > How to check Dbnull in aspx page

How to check Dbnull in aspx page

by Thamil   on Aug 17, 2013   Category: ASP.NET  |  Views: 2030    |  Points: 5   |  Gold 
  Reply
Dear members
I have to make a check for the DBnull in the aspx code but not able to find how to do it.

I am not able to attach the screen shot but pasting the code.
<asp:TemplateColumn>
<ItemTemplate>

<asp:Label ID="Label7"
runat="server"
Text=<%#Container.DataItem("InstituteDetail")%> >
</asp:Label>

</ItemTemplate>

</asp:TemplateColumn>


I am getting the error : Conversion from type DBnull to type string is not valid..

I am getting the null value in the InstituteDetail column but I had to use it in the same way as its the old working project

I tried to write for the DBnull check but not working
I am pasting the code for the same to its as belows


<asp:Label ID="Label7"
runat="server"
Text=<%#If(Container.DataItem("InstituteDetail")).ToString() Is DBNull.Value, "0 value" Container.DataItem("InstituteDetail"))%> >
</asp:Label>


Please help

Thanks

Question posted onbehalf of abhishek saxena.

Bookmark and Share:
 
  User Reply  | Ask a question  |   Reply 
  Re :How to check Dbnull in aspx page   
by Thamil
on Aug 17 2013 11:29AM
Points : 10
Gold 
Hello abhishek saxena,

Try this below way, instead of doing validation in front side, do it in sql server side in select query, use isnull for the specific column as like below so that if its null then it will return 0.
select isnull(convert(varchar(100),InstituteDetail),'0') from test

Recent Post

Latest Posts