Replace blank images with default image

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • amandab116
    New Member
    • Apr 2009
    • 13

    Replace blank images with default image

    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?
  • Bassem
    Contributor
    • Dec 2008
    • 344

    #2
    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.

    Comment

    • amandab116
      New Member
      • Apr 2009
      • 13

      #3
      Hey, thanks for your response, I don't have a picture box, rather an image tag.
      Code:
      <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.
      Code:
      image1.ImageUrl = "DisplayPicture.aspx?id=" + reader["resource_id"].ToString();
      I'm not sure how I'd go about implying your methods.
      Last edited by PRR; Apr 16 '09, 01:38 PM. Reason: Please post code in [code] [/code] tags.

      Comment

      • amandab116
        New Member
        • Apr 2009
        • 13

        #4
        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.

        Comment

        • Bassem
          Contributor
          • Dec 2008
          • 344

          #5
          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.
          Code:
          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.
          Code:
          image1.ImageUrl=(image1.ImageUrl=="value in TextBox") ? "~\myimages\blank.gif" : image1.ImageUrl ;

          Comment

          • amandab116
            New Member
            • Apr 2009
            • 13

            #6
            Thank you very much for your help with this! :)

            Comment

            • Bassem
              Contributor
              • Dec 2008
              • 344

              #7
              If it works, that's good, but don't forget removing the TextBox after all.

              Kind Regards,
              Bassem

              Comment

              • amandab116
                New Member
                • Apr 2009
                • 13

                #8
                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?

                Comment

                • Bassem
                  Contributor
                  • Dec 2008
                  • 344

                  #9
                  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.

                  Comment

                  • amandab116
                    New Member
                    • Apr 2009
                    • 13

                    #10
                    Here is my boatload of code... the "picture" field is allowing nulls.


                    Code:
                     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;
                        }

                    Comment

                    • Bassem
                      Contributor
                      • Dec 2008
                      • 344

                      #11
                      I didn't get the code-problem so let me suggest another way to solve it.
                      It's not a perfect one but until you find the problem.
                      You may pass intImageSize as a parameter to myCommand and make your test for the NULL value in Sql as this :
                      Code:
                      @intImageSize   int
                           if(@intImageSize = 0)
                                @picture = null
                       -- then the rest of your query ...
                      If it works so the problem was in passing the null value. If not make sure that you didn't set a default value for picture column. If you did remove it, It've to be null.

                      Actually I don't store images in database, I save images as files each has an identical name, by using an index for example, and storing only the path. Think about it, if the problem still annoying.

                      Thanks,
                      Bassem

                      Comment

                      • amandab116
                        New Member
                        • Apr 2009
                        • 13

                        #12
                        I've solved the problem by implemeting the code like you've suggested :)

                        Now I've run into the dilemma of trying to solve the issue of overwriting already supplied images.

                        If the upload box is left blank, it still updates the database as a null... overwriting anything that was already stored.

                        As you can see, this is my first time using images in my database. I can't store the filepath because this is a program allowing users to create new resources and upload a picture from their machine and it needs to be stored in the database itself, not just the filepath.

                        Comment

                        • Bassem
                          Contributor
                          • Dec 2008
                          • 344

                          #13
                          You're inserting records, I see no updating?!
                          What's the primary key in this table and which columns are unique?
                          How do you checked that the date is overwritten? or how do you make the select query?

                          About storing paths of images,
                          You can save the image on a server directory and store paths refer to these images on server.

                          >> data not date <<

                          Comment

                          Working...