ASP.NET newbie

M

M. Ali Qureshi

Hi,

This maybe a very simple question, but i am very new to asp.net.

Can someone please fix following for me?

<%
If IsDBNull(Container.DataItem("initials")) Then
Response.Write("N/A")
End If
%>

I'm using repeater control to list data from a table. I want to check if
field "initials" is null, then it should print "N/A".

I get following error when i run the above code:
"Compiler Error Message: BC30451: Name 'Container' is not declared."


Thanks in advance.

/MAQ
 
J

Juan T. Llibre

You are missing the "#" before the expression.

The databinding expression <%# some expression %>
is evaluated in the language of the page (VB, C#, etc.)

Container.DataItem is a runtime alias for the DataItem for this specific item in the bound list.
The actual type of DataItem is determined by the type of the datasource.

If the datasource is a Dataview, the type of DataItem is DataRowView.
If the type of the datasource is an array of strings, the type of DataItem is String.

If the datasource is a collection of strongly-typed objects (for example "Employees" objects),
the type of DataItem is Employees.

Each of these cases requires a slightly different databinding expression,
with marked differences between VB and C#, which I will not go into, since you're using VB.

In all cases, the databinding expressions produce a string that can be displayed in the page.

Here are some VB examples:

Array of Strings:
<%# Container.DataItem %>

Field from DataView:
<%# Container.DataItem("initials") %>

Property from a collection of objects:
<%# Container.DataItem.initials %>

Non-String property from a collection of objects:
<%# CStr(Container.DataItem.initials) %>

I hope this makes the concept clearer...
 
M

M. Ali Qureshi

Hi,

Thanks for your answer, which did clear my concept for DataItem.

But i am still having the same (syntax) problem here.

How do i check (syntax) if the column "initials" is null or not?

Thanks

MAQ
 
J

Juan T. Llibre

re:
!> How do i check (syntax) if the column "initials" is null or not?

I gave you the correct syntax :

<%# Container.DataItem("initials") %>

If IsDBNull(<%#Container.DataItem("initials")%>) Then
Response.Write("N/A")
End If
 
G

Guest

try to place the following code in your ItemTemplate
<%# Container.DataItem("initials") ?"N/A" :
Container.DataItem("initials").ToString() %>
 
J

Juan T. Llibre

You could also use a function...


Function CheckNull(ByVal ValuetoCheck)
if isDbNull(ValuetoCheck)
Return "N/A"
else
Return ValuetoCheck
end if
End Function

the Databinder code would look like this...

<%# CheckNull(DataBinder.Eval(Container.DataItem, "initials")) %>

As said earlier, there are many ways to skin a cat.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top