views:

57

answers:

2

Hi, I'm trying to do the following for asp.net combobox:

Text='<%# IIf(Eval("Name").ToString().Equals(""), Bind("Other"), Bind("Name") %>'  

What I'm trying to do is if the "Name" column is empty then bind to "Other" column, otherwise bind to "Name" column.

Anyone know the right syntax, keep getting object not set to reference. I'm doing this in VB.Net.

Thanks,
Jim

A: 

Try

Text='<%# IIf(Eval("Name").Equals(DBNull.Value), Eval("Other") , Eval("Name")) %>' 
hallie
Object reference not set to an instance of an object. I get that error? column can contain Null, how should I be dealing with that?
Joe
you need to do something like Eval("Name").Equals(DBNull.Value), in c# there is string.IsNullOrEmpty function...not sure with VB.
hallie
i've made some edits, but i didn't test it...just try
hallie
Conversion from type 'DBNull' to type 'String' is not valid.
Joe
does you field "Other" has a value?...i've made a changes (again), assuming that your "Other" field is not null and it will not test if your "Name" field is empty.
hallie
+2  A: 

Try this (Edited)

Text='<%# If(Not Eval("Name").ToString.Length = 0, Eval("Name") , Eval("Other")) %>' 

If this doesn't work, then you might have to build a Code Behind method

Text='<%# RetrieveName(Eval("Name"),Eval("Other"))'

Code Behind

Public Function RetrieveName(Byval name As String?, Byval other As String?) As String
  If Not String.IsDBNull(name) Then
    return name
  ElseIf String.IsDBNull(name) AndAlso Not String.IsDBNull(other) Then
    return other
  Else
    return String.Empty
  End If
End Function
rockinthesixstring
Okay I'll try it now
Joe
Conversion from type 'DBNull' to type 'String' is not valid.
Joe
edited... please test.
rockinthesixstring
Edited Again...
rockinthesixstring
ah object not set to an instance
Joe
did you try the code behind function?
rockinthesixstring
Let me try one second
Joe
sorry, I forgot to add "As String" to the end of the function - fixed now.
rockinthesixstring
So, in what you're testing.. Name is obviously NULL ;-)
rockinthesixstring
I think I'm going to give on this one Conversion from type 'DBNull' to type 'String' is not valid.
Joe
did that function not work? Is Name and Other in the database actually Strings (varchar, etc)?
rockinthesixstring
I did one more edit using `IsDBNull`, though I'm not sure if it's supported in VB.NET 2 since I haven't used V2 in a very long time.
rockinthesixstring
Also please forgive me if there are minor typos... I'm on my mac and don't have VS in front of me for formatting and intellisense.
rockinthesixstring