GridView problem with DetectImagePath in the c# call...

T

trint

Here is my gridview that really gives a headache...It works very well
with all of the selections in the call back to the c# code I have
pasted at the bottom. But it ALWAYS gives this error: "An exception
of type 'System.InvalidCastException' occurred in App_Web_dcieiiqw.dll
but was not handled in user code" only when I select the "gifts"
selection (which is listed at the bottom exactly like the "products"
folder and the "categories" folder).
Any help is very much appreciated.
Thanks,
Trint

Additional information: Unable to cast object of type 'System.DBNull'
to type 'System.String'.

<asp:panel ID="Panel1" runat="server" Height="850px" Width="125px">
<asp:GridView ID="GridView1"
runat="server" AutoGenerateColumns="False" CssClass="basix"
DataKeyNames="id"
OnSelectedIndexChanged="getSelected" BorderColor="DarkGray"
ForeColor="MidnightBlue" BorderStyle="Solid" BorderWidth="2px"
BackColor="White">
<SelectedRowStyle
BackColor="#FFFFCC" Font-Bold="True" ForeColor="#00CC00" />
<AlternatingRowStyle
BackColor="WhiteSmoke" ForeColor="Black" />
<RowStyle BackColor="White" />
<columns>
<asp:templatefield>

<headertemplate>
Description
</headertemplate>

<itemtemplate>
<asp:LinkButton ID="Linkbutton1" runat="server"
CommandName="select"
Font-Names="Arial" Font-Size="Small" Text='<%# Eval
( "name" ) %>'>
</asp:LinkButton>
</itemtemplate>

<controlstyle forecolor="Black" />
<itemstyle forecolor="Black" wrap="False" />
</asp:templatefield>
</columns>
<EditRowStyle
ForeColor="Black" />
<HeaderStyle
BackColor="Gainsboro" ForeColor="MidnightBlue" />
</asp:GridView>
&nbsp;
<table id="tblSearch1" runat="server"
style="width: 281px" visible="false">
<tr>
<td style="width: 14px">
&nbsp;</td>
<td style="width: 3px">
<table
id="searchWarningTable" runat="server" style="width: 14033%;"
visible="False">
<tr>
<td>
<asp:Label ID="Label18" runat="server" Font-Bold="True"
ForeColor="#FF0066"
Text="Your search -" Width="108px"></asp:Label>
&nbsp;<asp:Label ID="Label25" runat="server" Font-
Bold="True"
ForeColor="#FF0066" Width="108px"></asp:Label>
<asp:Label ID="Label26" runat="server" Font-Bold="True"
ForeColor="#FF0066"
Width="134px">- did not match </asp:Label>
</td>
</tr>
<tr>
<td>
<asp:Label ID="Label21" runat="server" Font-Bold="True"
ForeColor="#FF0066"
Text="Suggestions:" Width="104px"></asp:Label>
</td>
</tr>
<tr>
<td>
<asp:Label ID="Label22" runat="server" Font-Bold="True"
ForeColor="#FF0066"
Text="Make sure all words are spelled correctly."></
asp:Label>
</td>
</tr>
<tr>
<td>
<asp:Label ID="Label23" runat="server" Font-Bold="True"
ForeColor="#FF0066"
Text="Try different keywords."></asp:Label>
&nbsp;</td>
</tr>
<tr>
<td>
<asp:Label ID="Label24" runat="server" Font-Bold="True"
ForeColor="#FF0066"
Text="Try more general keywords."></asp:Label>
&nbsp;</td>
</tr>
</table>
&nbsp;</td>
</tr>
<tr>
<td style="width: 3px">
&nbsp;<asp:GridView ID="GridView9" runat="server"
AutoGenerateColumns="False" CssClass="basix"
DataKeyNames="id"
OnSelectedIndexChanged="buttonClickFromSearch"
BorderColor="Gainsboro"
Font-Bold="True" ForeColor="MidnightBlue" Width="431px"
HorizontalAlign="Left" >
<RowStyle BackColor="White" ForeColor="Black" />
<columns>
<asp:boundfield HeaderText="ProductID">

</asp:boundfield>
<asp:templatefield>

<headertemplate>
Product Name
</headertemplate>

<itemtemplate>
<asp:LinkButton ID="Linkbutton1" runat="server"
commandname="select"
Font-Names="Arial" Font-Size="Small" text='<%# Eval
( "name" ) %>' />
</itemtemplate>

<controlstyle forecolor="Black" />
<itemstyle forecolor="Black" horizontalalign="Left"
wrap="True" />
</asp:templatefield>
<asp:templatefield>

<headertemplate>
Image
</headertemplate>

<itemtemplate>
<asp:ImageButton ID="Imagebutton1" runat="server"
BorderColor="Gold"
BorderStyle="Outset" BorderWidth="2px"
commandname="select"
ImageUrl='<%#
DetectImagePath(Eval("image_file_name"), Eval("thumbnail")) %>'
Width="75" />
</itemtemplate>

<controlstyle bordercolor="DarkGray"
borderstyle="Solid"
borderwidth="1px" />
</asp:templatefield>
<HeaderStyle BackColor="Gainsboro"
ForeColor="MidnightBlue" />
<AlternatingRowStyle BackColor="WhiteSmoke" />
</asp:GridView>
</td>
</tr>
<tr>
<td style="width: 14px">
&nbsp;</td>
<td style="width: 3px">
&nbsp;</td>
</tr>
</table>
&nbsp; &nbsp;&nbsp;
</asp:panel>

Here is the c# code called when a selection is made from the gridview:

protected string DetectImagePath(object filename, object thumbnail)
{

string sFileName = string.Empty;
string path1NULL = string.Empty;

string testForImage = string.Empty;

testForImage = (string)filename;


if ((testForImage == string.Empty) || (testForImage ==
"NONE"))
{
sFileName = (string)thumbnail;
}
else
{
sFileName = (string)filename;
}

if (sFileName == "")
{
sFileName = "NullImageValue.gif";
path1NULL = String.Format("~/images/products/{0}",
sFileName.Trim());

return String.Format("~/images/products/{0}",
sFileName.Trim());
}
else
{
string path1 = string.Empty;
path1 = String.Format("~/images/categories/{0}",
sFileName.Trim());
string path2 = string.Empty;
path2 = String.Format("~/images/products/{0}",
sFileName.Trim());
string path3 = string.Empty;
path3 = String.Format("~/images/gifts/{0}",
sFileName.Trim());

if (System.IO.File.Exists(Server.MapPath(path1)))
{
return String.Format("~/images/categories/{0}",
sFileName.Trim());
}

if (System.IO.File.Exists(Server.MapPath(path2)))
{
return String.Format("~/images/products/{0}",
sFileName.Trim());
}

if (System.IO.File.Exists(Server.MapPath(path3)))
{
return String.Format("~/images/gifts/{0}",
sFileName.Trim());
}
else
{
sFileName = "noImageAvailable.gif";
return String.Format("~/images/products/{0}",
sFileName.Trim());
}


}

return sFileName;


}
 
I

Ignacio Machin \( .NET/ C# MVP \)

Hi,

Can you reproduce it in a small program?
What value sFileName & path3 have?
 

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

Top