We have a very simple page for displaying the records.
Basically, it passes certain phoneno and if it’s found then displayed and there is a potential the same number that is why using repeater. This thing work nicely no problem BUT now the requirement change by introduicng the photo profile that needs to be displayed.
The way it works on this is basically once a return record it checkes the physical photo based on the PhoneID on the certain folder and if it’s exist then displayed such as “/images/profiles/1.jpg” and if it couldn’t find then use “/images/profiles/default.jpg”
I couldn’t make it work on how to render though.
ASPX:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="index.aspx.cs" Inherits="IndexPage" %>
<form id="form1" runat="server">
<asp:label ID="lblErrMsg" runat="server" ></asp:label>
<asp:Panel runat="server" ID="pnlMainNoRec" Visible="false">
<table>
<tr>
<td>No record available.</td>
</tr>
</table>
</asp:Panel>
<asp:Panel runat="server" ID="pnlMainData" Visible="false">
<asp:Repeater id="rptrMain" runat="server">
<ItemTemplate>
<table>
<tr valign="top">
<td>
<table>
<tr>
<td><img alt="" src="/images/profiles/<%# DataBinder.Eval(Container.DataItem, "PhotoID") %>" /></td>
</tr>
</table>
</td>
<td>
<table>
<tr>
<td><p>First Name:</p></td>
<td><%# DataBinder.Eval(Container.DataItem, "FirstName") %></td>
</tr>
<tr>
<td>Last Name:</td>
<td><%# DataBinder.Eval(Container.DataItem, "LastName") %></td>
</tr>
<tr>
<td>Status:</td>
<td><%# DataBinder.Eval(Container.DataItem, "Status") %></td>
</tr>
<tr>
<td>Region:</td>
<td><%# DataBinder.Eval(Container.DataItem, "Region") %></td>
</tr>
<tr>
<td>Address:</td>
<td><%# DataBinder.Eval(Container.DataItem, "Address") %></td>
</tr>
<tr>
<td>Caller Type:</td>
<td><%# DataBinder.Eval(Container.DataItem, "CallerType") %></td>
</tr>
<tr>
<td>Program:</td>
<td><%# DataBinder.Eval(Container.DataItem, "ClientProgram")%></td>
</tr>
<tr>
<td colspan="2"> </td>
</tr>
</table>
</td>
</tr>
</ItemTemplate>
</asp:Repeater>
</asp:Panel>
CS:
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
using System.IO;
public partial class IndexPage : System.Web.UI.Page
{
protected string thisConnectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
protected string thisQuery = "SELECT * FROM AllPhoneList WHERE PhoneNumber=@PhoneNumber";
protected string thisProfileLocation = "/images/profiles/";
protected void Page_Load(object sender, EventArgs e)
{
string phoneNo = Request.QueryString["PhoneNumber"];
GetDetail(phoneNo);
}
void GetDetail(string PhoneNumber)
{
try
{
using (SqlConnection conn = new SqlConnection(this.thisConnectionString))
{
conn.Open();
SqlCommand cmd = new SqlCommand(thisQuery, conn);
cmd.CommandType = CommandType.Text;
cmd.Parameters.Add("@PhoneNumber", SqlDbType.VarChar, 50);
cmd.Parameters["@PhoneNumber"].Value = PhoneNumber;
DataTable dt = new DataTable();
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(dt);
int x = 0;
if (dt.Rows.Count > 0)
{
pnlMainData.Visible = true;
if (isFileExist(Int32.Parse(dt.Rows[x]["PhoneID"].ToString())))
???
//lblMainFirstName.Text = dt.Rows[0]["FirstName"].ToString();
//lblMainLastName.Text = dt.Rows[0]["LastName"].ToString();
rptrMain.DataSource = dt;
rptrMain.DataBind();
x+=1;
}
else
{
pnlMainNoRec.Visible = true;
}
}
}
catch (Exception err)
{
//lblErrMsg.Text = err.Message.ToString();
pnlMainNoRec.Visible = true;
}
}
public bool isFileExist(int phoneID)
{
string imageFolder;
imageFolder = Server.MapPath(thisProfileLocation) + phoneID.ToString();
if (File.Exists(imageFolder))
return true;
else
return false;
}
}
Change:
To:
When the image returns a 404, you will get the default image.