473,799 Members | 2,779 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Replace blank images with default image

13 New Member
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?
Apr 14 '09 #1
12 12557
Bassem
344 Contributor
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.
Apr 14 '09 #2
amandab116
13 New Member
Hey, thanks for your response, I don't have a picture box, rather an image tag.
Expand|Select|Wrap|Line Numbers
  1. <asp:Image ID="image1" runat="server" ImageUrl="DisplayPicture.aspx" BorderColor="#6f9dd9" BorderWidth="2px" BorderStyle="Solid" Height="150" Width="150" />
  2.  
In the code behind, I have the following code to get the ID of the resource I'm trying to display the image for.
Expand|Select|Wrap|Line Numbers
  1. image1.ImageUrl = "DisplayPicture.aspx?id=" + reader["resource_id"].ToString();
  2.  
I'm not sure how I'd go about implying your methods.
Apr 15 '09 #3
amandab116
13 New Member
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.
Apr 15 '09 #4
Bassem
344 Contributor
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.
Expand|Select|Wrap|Line Numbers
  1. 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.
Expand|Select|Wrap|Line Numbers
  1. image1.ImageUrl=(image1.ImageUrl=="value in TextBox") ? "~\myimages\blank.gif" : image1.ImageUrl ;
Apr 16 '09 #5
amandab116
13 New Member
Thank you very much for your help with this! :)
Apr 16 '09 #6
Bassem
344 Contributor
If it works, that's good, but don't forget removing the TextBox after all.

Kind Regards,
Bassem
Apr 16 '09 #7
amandab116
13 New Member
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?
Apr 16 '09 #8
Bassem
344 Contributor
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.
Apr 16 '09 #9
amandab116
13 New Member
Here is my boatload of code... the "picture" field is allowing nulls.


Expand|Select|Wrap|Line Numbers
  1.  protected void ButtonSave_Click(object sender, EventArgs e)
  2.     {
  3.         int intImageSize;
  4.         string strImageType;
  5.         Stream ImageStream;
  6.         FileUpload FileUpload1 = (FileUpload)this.FindControl("FileUpload1");
  7.  
  8.         // Gets the Size of the Image
  9.         intImageSize = FileUpload1.PostedFile.ContentLength;
  10.  
  11.         // Gets the Image Type
  12.         strImageType = FileUpload1.PostedFile.ContentType;
  13.  
  14.         // Reads the Image
  15.         ImageStream = FileUpload1.PostedFile.InputStream;
  16.  
  17.         byte[] ImageContent = new byte[intImageSize];
  18.         int intStatus;
  19.         intStatus = ImageStream.Read(ImageContent, 0, intImageSize);
  20.  
  21.         // Create Instance of Connection and Command Object
  22.         SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["BayNetConnectionString"].ConnectionString);
  23.         SqlCommand myCommand = new SqlCommand("INSERT into sch_resources ([name], [description], [resource_type_id], [picture]) VALUES (@name, @description, @resource_type_id, @picture)", myConnection);
  24.  
  25.         // Mark the Command as a SPROC
  26.         myCommand.CommandType = CommandType.Text;
  27.  
  28.         // Add Parameters to SPROC
  29.         SqlParameter picture = new SqlParameter("@picture", SqlDbType.Image);
  30.         if (intImageSize == 0)
  31.             picture.Value = Convert.DBNull;
  32.         else
  33.             picture.Value = ImageContent;
  34.         myCommand.Parameters.Add(@picture);
  35.  
  36.         SqlParameter name = new SqlParameter("@name", lblrname.Text);
  37.         myCommand.Parameters.Add(@name);
  38.  
  39.         SqlParameter description = new SqlParameter("@description", RadTextBoxRDescription.Text);
  40.         myCommand.Parameters.Add(@description);
  41.  
  42.         SqlParameter resource_type_id = new SqlParameter("@resource_type_id", RadComboBoxRTypeAdd.SelectedValue);
  43.         myCommand.Parameters.Add(@resource_type_id);
  44.  
  45.  
  46.        // if (FileUpload1.PostedFile == null || string.IsNullOrEmpty(FileUpload1.PostedFile.FileName) || FileUpload1.PostedFile.InputStream == null)
  47.        // {
  48.        //     Response.Write("Please upload a valid picture file.");
  49.         //    return;
  50.       //  }
  51.         string extension = System.IO.Path.GetExtension(FileUpload1.PostedFile.FileName).ToLower();
  52.         string MIMEType = null;
  53.         switch (extension)
  54.         {
  55.             case ".jpg":
  56.             case ".jpeg":
  57.             case ".jpe":
  58.                 MIMEType = "image/jpg";
  59.                 break;
  60.             default: Response.Write("Not a valid file format.");
  61.                 return;
  62.         }
  63.  
  64.         try
  65.         {
  66.             myConnection.Open();
  67.             myCommand.ExecuteNonQuery();
  68.             myConnection.Close();
  69.             Response.Write("New resource successfully added!");
  70.         }
  71.  
  72.         catch (SqlException SQLexc)
  73.         {
  74.             Response.Write("Insert Failed. Error Details are: " + SQLexc.ToString());
  75.         }
  76.         ButtonCancel.Text = "Close";
  77.         Panel1.Visible = false;
  78.     }
Apr 16 '09 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

2
1906
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...
15
6225
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!
4
4624
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?
1
1342
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...
2
1602
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
1
6316
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...
3
7922
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.
2
3345
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.
2
3022
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,...
0
9687
marktang
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...
0
9541
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,...
0
10482
Oralloy
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...
0
10251
jinu1996
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...
1
10225
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,...
0
9072
agi2029
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...
1
7564
isladogs
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...
0
6805
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();...
1
4139
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

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.