In my program I am storing images in the database. Not all of the records in the database have images, and when I load the details into another .aspx page, sometimes the image is empty- has a red "x". How do I replace that "x" with a default image?
12 12557
Hey
It might be like this :
Option#1 : In your database set the default value for the image column to your default image, so if it is empty (at new row added) the default value will be set.
(actually I didn't add images to db before, only pathes "Strings"). This is very easy.
Option#2 : You may check the ImageLocation property of the PictureBox, this is a string if equal to String.Empty so there is no image then you've to assign one.
I prefer Option#1, I hope one of them will help.
Hey, thanks for your response, I don't have a picture box, rather an image tag. -
<asp:Image ID="image1" runat="server" ImageUrl="DisplayPicture.aspx" BorderColor="#6f9dd9" BorderWidth="2px" BorderStyle="Solid" Height="150" Width="150" />
-
In the code behind, I have the following code to get the ID of the resource I'm trying to display the image for. -
image1.ImageUrl = "DisplayPicture.aspx?id=" + reader["resource_id"].ToString();
-
I'm not sure how I'd go about implying your methods.
Actually, I think what I want to really accomplish is this:
If the value of the picture field is null, I want to show this blank.gif image which says that there is no image available.
Do you've this image (blank.gif), if so there is a default value.
In your database do you store the image or only the path (string) as nvarchar?
if you store path to image, for each column you add to a table there is a default value, click the column in your database management system IDE you'll find its properties set the default value to the path of blank.gif
Or try this :
Check if the ImageUrl is null,
add a TextBox to your page and set its Text property to the same ImageUrl that is shown now. - textBox1.Text = image1.ImageUrl;
Make it display when there is no image.
now you can see what is the value of ImageUrl. Do your test on this value. - image1.ImageUrl=(image1.ImageUrl=="value in TextBox") ? "~\myimages\blank.gif" : image1.ImageUrl ;
Thank you very much for your help with this! :)
If it works, that's good, but don't forget removing the TextBox after all.
Kind Regards,
Bassem
The only other thing I'm trying to figure out is even when a user doesn't upload an image, it's still stored as a binary instead of a NULL. How would I replace binary with NULL when no image is selected for upload?
I don't know much about binary SqlDataType, but how do you add a new record, could you post the code, if you allow the column to be nullable, it shouldn't be a problem there.
Here is my boatload of code... the "picture" field is allowing nulls. - protected void ButtonSave_Click(object sender, EventArgs e)
-
{
-
int intImageSize;
-
string strImageType;
-
Stream ImageStream;
-
FileUpload FileUpload1 = (FileUpload)this.FindControl("FileUpload1");
-
-
// Gets the Size of the Image
-
intImageSize = FileUpload1.PostedFile.ContentLength;
-
-
// Gets the Image Type
-
strImageType = FileUpload1.PostedFile.ContentType;
-
-
// Reads the Image
-
ImageStream = FileUpload1.PostedFile.InputStream;
-
-
byte[] ImageContent = new byte[intImageSize];
-
int intStatus;
-
intStatus = ImageStream.Read(ImageContent, 0, intImageSize);
-
-
// Create Instance of Connection and Command Object
-
SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["BayNetConnectionString"].ConnectionString);
-
SqlCommand myCommand = new SqlCommand("INSERT into sch_resources ([name], [description], [resource_type_id], [picture]) VALUES (@name, @description, @resource_type_id, @picture)", myConnection);
-
-
// Mark the Command as a SPROC
-
myCommand.CommandType = CommandType.Text;
-
-
// Add Parameters to SPROC
-
SqlParameter picture = new SqlParameter("@picture", SqlDbType.Image);
-
if (intImageSize == 0)
-
picture.Value = Convert.DBNull;
-
else
-
picture.Value = ImageContent;
-
myCommand.Parameters.Add(@picture);
-
-
SqlParameter name = new SqlParameter("@name", lblrname.Text);
-
myCommand.Parameters.Add(@name);
-
-
SqlParameter description = new SqlParameter("@description", RadTextBoxRDescription.Text);
-
myCommand.Parameters.Add(@description);
-
-
SqlParameter resource_type_id = new SqlParameter("@resource_type_id", RadComboBoxRTypeAdd.SelectedValue);
-
myCommand.Parameters.Add(@resource_type_id);
-
-
-
// if (FileUpload1.PostedFile == null || string.IsNullOrEmpty(FileUpload1.PostedFile.FileName) || FileUpload1.PostedFile.InputStream == null)
-
// {
-
// Response.Write("Please upload a valid picture file.");
-
// return;
-
// }
-
string extension = System.IO.Path.GetExtension(FileUpload1.PostedFile.FileName).ToLower();
-
string MIMEType = null;
-
switch (extension)
-
{
-
case ".jpg":
-
case ".jpeg":
-
case ".jpe":
-
MIMEType = "image/jpg";
-
break;
-
default: Response.Write("Not a valid file format.");
-
return;
-
}
-
-
try
-
{
-
myConnection.Open();
-
myCommand.ExecuteNonQuery();
-
myConnection.Close();
-
Response.Write("New resource successfully added!");
-
}
-
-
catch (SqlException SQLexc)
-
{
-
Response.Write("Insert Failed. Error Details are: " + SQLexc.ToString());
-
}
-
ButtonCancel.Text = "Close";
-
Panel1.Visible = false;
-
}
Sign in to post your reply or Sign up for a free account.
Similar topics |
by: Trent |
last post by:
Hey All,
I am new to javascript and currently coding a site with scripts that
are beyond my level of understanding. The problematic page has
thumbnail images that can be clicked on to zoom in. When clicked, the
preloaded larger image displays in a div layer that gets turned on. The
problem is that ie sometimes does not display the image - the
placeholder just stays blank. The script works fine in firefox and
netscape (haven't tried...
|
by: Jake |
last post by:
Not sure if this can be done with javascript but I would like to find a way
to detect if an image is missing and replace it with a default image instead
of the dreaded RED X showing up.
So - Is Javascript the way? Or should I look elesewhere?
Thanks in Advance!
|
by: Saya |
last post by:
Hello,
The IIS default location for images, usually is
'Inetpub/wwwroot/images'; currently I'm using:
'picWeb.Image=GetImage(IMG_URL+filename)', where
IMG_URL="http://ipadress/images", to reach my images in my web service
application.
Is there a command available to get the images from a different
location in the server machine, for example at another logical drive?
|
by: KitKat |
last post by:
With the help of kind, brilliant folks, I have been working on this program
that uses a combo box selection to display six different jpgs. The jps are a
time stamp, and the files are located in six different folders of camera
views. HOWEVER, some of these folders do not have that certain timed jpg.
So...I need to figure out how to show all the rest of the pictures and use
an "no image available or something" for the program to work...
|
by: sanju |
last post by:
Hi,
I am struggling to replace a bit value 'True' or 'False' with a image
true.gif or false.gif and bind it to a repeater control.
I am getting values 'true' or 'false' depending on whether user is logged in
or not(with asp.net membership provider)
Getting true or false with this <%#
Membership.GetUser(Eval("UserID")).IsOnline % and
| |
by: wkerplunk |
last post by:
On mouseover it goes to the correct map say TheMap1.jpg and then on mouseOut it defaults back to map, I need to do a onClick that sets the TheMap1.jpg mouseOver to the default TheMap.jpg so the TheMap1.jpg is now the default image not the TheMap.jpg. So after I click for information, and mouseOut of that area TheMap1.jpg will stay as all the MouseOuts Until I click another area of the map say TheMap10.jpg once clicked TheMap10.jpg will become...
|
by: Paul Cheetham |
last post by:
Hi,
I have an application that will display user-defined images in some
asp:image controls.
(sets the ImageURL, and is always built from the site root)
The problem comes when the images specified do not exist, as it displays
a red cross and the alternate text.
What I would like to do is display a default image instead.
|
by: aris1234 |
last post by:
Can you use PHP to show a default image if the image field in a database row is empty?
I'm creating a news page, but the client may not always upload an image with a news story. I know how to hide the area, but this leaves a big blank space. It would be much better if a default image were to appear.
|
by: Simon Wigzell |
last post by:
I have inherited a database driven website that comes with a table of image
links. The images are scattered all of the internet and there are thousands
of them. I would like to write an asp script to just run through the table
and download every link by it's URL. I thought I had id by faking a form
with the "File" element and loading it with all the URLs. Would have worked
except you cannot preload the "File" element. As I understand it,...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
| |
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |