Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8921005
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T06:25:28+00:00 2026-06-15T06:25:28+00:00

My asp.net-Mysql application is hosted on windowsserver,when i tried to insert image into mysql

  • 0

My asp.net-Mysql application is hosted on windowsserver,when i tried to insert image into mysql table from the admin.aspx page,its displaying the error

“Object reference not set to an instance of an object.”

But from my VS I can insert image to local mysql server without any error.

When i inserted image from webadmin,it is inserted successfully and displaying image on website.I can update the contents through my admin.aspx,but when i tried to update or insert image,I am getting the error.

[NullReferenceException: Object reference not set to an instance of an object.]
   Lavande.DBconnect.addimagehome(Byte[] image, String Content, String arabiccontent) in C:\Users\user\Desktop\Lavande_Asp\Lavande\Lavande\DBconnect.cs:33
   Lavande.abklavande900.button_savehome_Click(Object sender, EventArgs e) in C:\Users\user\Desktop\Lavande_Asp\Lavande\Lavande\abklavande900.aspx.cs:84
   System.Web.UI.WebControls.Button.OnClick(EventArgs e) +118
   System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +112
   System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +10
   System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +13
   System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +36
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +5563

Following is the code to read the uploaded file:

 string path = System.IO.Path.GetFullPath(FileUpload_home.PostedFile.FileName);
Byte[] image = WM.ImageSetting("watermarkname", Server.MapPath("upload/") + "logo1.png", path);
DB.addimagehome(image, contentsend, arabiccontent);

watermarkclass.

public class watermark
{
    public Byte[] ImageSetting(string wmText, string wmImage, string mainImage)
    {
        byte[] imageBytes = null;
        if (File.Exists(mainImage))
        {

            System.Drawing.Image image = System.Drawing.Image.FromFile(mainImage);

            Graphics graphic;
            if (image.PixelFormat != PixelFormat.Indexed && image.PixelFormat != PixelFormat.Format8bppIndexed && image.PixelFormat != PixelFormat.Format4bppIndexed && image.PixelFormat != PixelFormat.Format1bppIndexed)
            {
                // Graphic is not a Indexed (GIF) image
                graphic = Graphics.FromImage(image);
            }
            else
            {
                /* Cannot create a graphics object from an indexed (GIF) image.
                 * So we're going to copy the image into a new bitmap so
                 * we can work with it. */
                Bitmap indexedImage = new Bitmap(image);
                graphic = Graphics.FromImage(indexedImage);

                // Draw the contents of the original bitmap onto the new bitmap.
                graphic.DrawImage(image, 0, 0, image.Width, image.Height);
                image = indexedImage;
            }
            graphic.SmoothingMode = SmoothingMode.AntiAlias & SmoothingMode.HighQuality;

            //Text Watermark properties
            Font myFont = new Font("segoe script", 17, FontStyle.Bold);
            SolidBrush brush = new SolidBrush(Color.FromArgb(40, Color.White));
            SizeF textSize = new SizeF();
            if (wmText != "")
                textSize = graphic.MeasureString(wmText, myFont);

            //Image Watermark
            System.Drawing.Image ig = null;
            if (wmImage != "")
                ig = System.Drawing.Image.FromFile(wmImage);

            // Write the text watermark and image watermark across the main image.
            for (int y = 0; y < image.Height; y++)
            {
                for (int x = 0; x < image.Width; x++)
                {
                    PointF pointF = new PointF(x, y);
                    PointF pointFm = new PointF(x, y+150);
                    if (wmText != "")
                    {
                        graphic.DrawString(wmText, myFont, brush, pointFm);
                        x += Convert.ToInt32(textSize.Width);
                    }
                    if (wmImage != "")
                    {
                        graphic.DrawImage(ig, pointF);
                        x += Convert.ToInt32(ig.Width);
                    }
                }
                if (wmText != "")
                    y += Convert.ToInt32(textSize.Height);
                if (wmImage != "")
                    y += Convert.ToInt32(ig.Height);

            }

            using (MemoryStream memoryStream = new MemoryStream())
            {
                // save image in memoryStream with it format which get it from GetImageFormat function
                image.Save(memoryStream, GetImageFormat(mainImage));
                imageBytes = memoryStream.ToArray();
            }
            graphic.Dispose();
        }


        return imageBytes;


    }

    //function to return Image Format
    ImageFormat GetImageFormat(String path)
    {
        switch (Path.GetExtension(path).ToLower())
        {
            case ".bmp": return ImageFormat.Bmp;
            case ".gif": return ImageFormat.Gif;
            case ".jpg": return ImageFormat.Jpeg;
            case ".png": return ImageFormat.Png;
            default: return null;
        }
    }

}

}

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T06:25:29+00:00Added an answer on June 15, 2026 at 6:25 am

    Based on your stack trace and the code it points to, the image variable there must be null. The exception is thrown when you try to look at the Length property… there is no object there to use to check that property.

    The reason it’s null is that your ImageSetting() function can be summarized like this:

    Byte[] ImageSetting(...)
    {
        Byte[] imageBytes = null;
        if (File.Exists(...) )
        {
            //...
        }
        return imageBytes;
    }
    

    Obviously, the File.Exists() call fails, and thus you return no image. You likely should have a placeholder image you can load and use instead. Also, you should not use File.Exists() like that in the first place, as the file system is volatile. Instead, put your coding efforts into the exception handler.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to insert values from my asp.net application into my MySQL database. On
From my ASP.NET application I want to issue this query to MySQL SELECT *
How get data from mysql database? I use ASP.NET and C#.Previously, I used LINQ
I'm currently working on a legacy ASP.NET 2.0 web application using a MySQL 5
I'm trying to connect to a MySQL database using my ASP.NET Web Forms Application.
I have an ASP.NET MVC web application which is hosted by an external provider,
I am using MySQL 5.5 and i linked it with my ASP.net Application .
I am developing a web application with ASP.NET and MySQL, but I am not
I am using asp.net and c# in my application and Mysql as Database.I want
Currently, I am creating an application using ASP.NET MVC3 and MySQL and when I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.