So I have this code below in a repeater that is loaded from an SQLDataSource, how can I write in the VB class to check if it is Null or if it is N/A to change the text to something else I would like to add. How do I do that?
There are a few things you can do to solve this solution. The change can be done within the source code or in your database.
Database Solution
SELECT COALESCE(Test, 'Text to display if NULL') As Test
FROM Table
WHERE....
VB SOLUTION
Public Function ConvertNullInteger(field As Object) As Integer
If field = DBNull.Value Then
ConvertNullInteger = 0
Else
ConvertNullInteger = CInt(field)
End If
End Function
Please excuse me if there are any syntax error as I am primarily a C# Developer
Hope this helps.
*** REMINDER ***
If you find this post useful, Please click the 'Mark as Answer' Button.
Public Function CheckNull(byval fieldValue as string) As String
If fieldValue.Equals(DBNull.Value) Then Return "" Else
If fieldValue = "N/A" Then
Return "value for N/A"
Else
Return ""
End If
End Function
the simplest for you now it seems to me would be to keep the function CheckNull in your page, say, right after
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
End Sub
Public Function CheckNull(byval fieldValue as string) As String
If fieldValue.Equals(DBNull.Value) Then Return "" Else
If fieldValue = "N/A" Then
Return "value for N/A"
Else
Return ""
End If
End Function
fyi, we normally keep such code in helper class in the app_code folder
and replace what you have with this <asp:Label ID="lblTest" runat="server" Text='<%# CheckNull(Eval("Test")) %>'></asp:Label>...the only difference is the function call CheckNull(....)..'Test' is the database field you're binding to - replace it with any
actual value that you have....good luck!
please keep posting as much of your code as you can so that we can help you better and effectively!
public string checkNullValue(Object obj)
{
string returnValue = string.Empty;
if (string.IsNullOrEmpty(obj.ToString()))
{
//your logic if the value is empty or null.
returnValue = "I am Null Or Empty";
}
else
{
//your logic if the value is not empty or null.
returnValue = "I am NOT Null Or Empty";
}
return returnValue;
}
This will exactly produce what you need. Hope that will work for you.
Thanks,
Marked as answer by elbasha on Jun 01, 2009 10:41 PM
elbasha
Member
216 Points
635 Posts
Check if DBNull value and replace it in VB.NET
Jun 01, 2009 04:43 AM|LINK
Hello,
So I have this code below in a repeater that is loaded from an SQLDataSource, how can I write in the VB class to check if it is Null or if it is N/A to change the text to something else I would like to add. How do I do that?
<td bgcolor="#E3E3E3" style="text-align: center" width="250">
<asp:Label ID="lblTest" runat="server" Text='<%# Eval("Test") %>'></asp:Label>
SyntaxC4
Contributor
4285 Points
707 Posts
Re: Check if DBNull value and replace it in VB.NET
Jun 01, 2009 05:05 AM|LINK
Hi elbasha,
There are a few things you can do to solve this solution. The change can be done within the source code or in your database.
Please excuse me if there are any syntax error as I am primarily a C# Developer
Hope this helps.
If you find this post useful, Please click the 'Mark as Answer' Button.
*** DISCLAIMER ***
All Code is provided AS IS.
PeteNet
All-Star
81342 Points
11398 Posts
Re: Check if DBNull value and replace it in VB.NET
Jun 01, 2009 05:11 AM|LINK
create this function in your codebehind:
Public Function CheckNull(byval fieldValue as string) As String
If fieldValue.Equals(DBNull.Value) Then Return "" Else
If fieldValue = "N/A" Then
Return "value for N/A"
Else
Return ""
End If
End Function
and call it in, like:
<asp:Label ID="lblTest" runat="server" Text='<%# CheckNull(Eval("Test")) %>'></asp:Label>
note: replace return values in the function per your requirements
Peter
yjaved
Participant
1113 Points
228 Posts
Re: Check if DBNull value and replace it in VB.NET
Jun 01, 2009 05:25 AM|LINK
you can check the null value in your sql query string and then can convert it into default value ........e.g
select isnull(ColName,'') as ColName from TableName......
and also try this ......it will replace the null with empty string
<asp:Label ID="lblTest" runat="server" Text='<%# Eval("Test") & "" %>'></asp:Label>
Mark as ans if got your answer.because it will be used for the readers of the forum
elbasha
Member
216 Points
635 Posts
Re: Check if DBNull value and replace it in VB.NET
Jun 01, 2009 05:39 AM|LINK
How can I get the field value? and do I write this code in the page load event?
PeteNet
All-Star
81342 Points
11398 Posts
Re: Check if DBNull value and replace it in VB.NET
Jun 01, 2009 05:49 AM|LINK
the simplest for you now it seems to me would be to keep the function CheckNull in your page, say, right after
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
End Sub
Public Function CheckNull(byval fieldValue as string) As String
If fieldValue.Equals(DBNull.Value) Then Return "" Else
If fieldValue = "N/A" Then
Return "value for N/A"
Else
Return ""
End If
End Function
fyi, we normally keep such code in helper class in the app_code folder
and replace what you have with this <asp:Label ID="lblTest" runat="server" Text='<%# CheckNull(Eval("Test")) %>'></asp:Label>...the only difference is the function call CheckNull(....)..'Test' is the database field you're binding to - replace it with any actual value that you have....good luck!
please keep posting as much of your code as you can so that we can help you better and effectively!
Peter
sharpeffect
Participant
1322 Points
221 Posts
Re: Check if DBNull value and replace it in VB.NET
Jun 01, 2009 05:49 AM|LINK
Here is one of the easiest way to achieve that.
Simply replace your Label tag with this modified code in your asp.net markup.
<asp:Label ID="lblTest" runat="server" Text='<%# checkNullValue(Eval("Test")) %>'></asp:Label>gailmarboo
Member
6 Points
3 Posts
Re: Check if DBNull value and replace it in VB.NET
May 10, 2013 06:47 AM|LINK
check DBNULL like this way
If IsDBNull(ds.Tables(0).Rows(i).Item(0)) Then
MsgBox("DBNULL exist in the field ")
Else
MsgBox(CInt(ds.Tables(0).Rows(i).Item(0)))
End If
http://net-informations.com/vbprj/ado.net/dbnull.htm
gail