Please help: "Specified cast is not valid" error when binding the Visible propery of a hyperlink con

K

keithb

Using a GridView, I get a "Specified cast is not valid" error when binding
the Visible propery of a hyperlink control to a DataTable text field. The
error goes away if I replace the data binding statement with "true" or
"false". This code causes the error:

<asp:HyperLink ID="HyperLink1"
runat="server"
NavigateUrl='<%#
Eval("c2Hyperlink1Link") %>'
Target="_blank"
Text='<%#
Eval("c2Hyperlink1Text") %>'
Visible='<%#
Eval("c2Hyperlink1Visible") %>' ></asp:HyperLink>

This code runs without error:

<asp:HyperLink ID="HyperLink1"
runat="server"
NavigateUrl='<%#
Eval("c2Hyperlink1Link") %>'
Target="_blank"
Text='<%#
Eval("c2Hyperlink1Text") %>'
Visible="true" >'
></asp:HyperLink>

I do not understand this, because in both cases the Visible property
receives the same text value, "true". Any suggestions or ideas will be
greatly appreciated.

Thanks,

Keith
 
C

Cowboy \(Gregory A. Beamer\)

It is seeing "true" (string) not true (boolean), or visa versa. You will
have to cast out the value as the correct type. Easiest way is to alter the
SQL Statement that creates the value and cast there, as you will not have to
change your binding at all.

--
Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA

*************************************************
Think outside of the box!
*************************************************
 
G

Guest

What if your data column has value of Null?
Make sure you are getting True/False value out of database.

Try convert value to Boolean explicitly

Visible='<%#
Ctype(Eval("c2Hyperlink1Visible"), Boolean) %>' ></asp:HyperLink>
 
K

keithb

The data comes from a DataTable that is defined by an XSD file. I see no way
of defining the data field as Boolean. It looks like all of the fields are
text.

Thanks,

Keith
 
Joined
Oct 17, 2008
Messages
2
Reaction score
0
solution

In case someone else happens to bounce into this post. This works

<asp:CheckBox ID="cbxDisplay" runat="server" Checked='<%# Convert.ToBoolean(Eval("Display") == DBNull.Value ? "False" : Eval("Display")) %>' />
 

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,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top