ERROR: How do I insert into an image column if no image is present in image control?

Donald Symmons 2,856 Reputation points
2023-05-27T08:27:35.76+00:00

How do I save an empty image data into database if the image control has no image? But if the image control has image data, the it should save as well.

The xample is that I have a web page that when the page loads, data is displayed from a database table into the different controls. The problem I have is that if for instance image data is not displayed inside the image control, I find it difficult to insert and empty image value into the table from the image control. The column that image is inserted into is called "UploadBg", and its DataType is varbinary(MAX).

I get error when I want to insert into the table, only when the image control has no image in it. But if the image has image in it, then the insert statement inserts into database. So Please, how do I insert even the image control has no image?

Here is the error:

Server Error in '/' Application.


The input is not a valid Base-64 string as it contains a non-base 64 character, more than two padding characters, or an illegal character among the padding characters.

byte[] imageData = Convert.FromBase64String(imgFileUpload.ImageUrl.Replace("data:image/jpeg;base64,", ""));
string query = @"INSERT INTO Register (email, Name, UploadBg) VALUES(@email, @Name, @UploadBg)";
using (SqlCommand cmd1 = new SqlCommand(query, con))
{
  cmd1.CommandType = CommandType.Text;
  cmd1.Parameters.AddWithValue("@email", mailtxt.Text.Trim());
  cmd1.Parameters.AddWithValue("@Name", orgName.Text.Trim());
  cmd1.Parameters.AddWithValue("@UploadBg", imageData);
  cmd1.Connection = con;
  con.Open();
  cmd1.ExecuteNonQuery();
}
con.Close();
.NET
.NET
Microsoft Technologies based on the .NET software framework.
3,415 questions
ASP.NET
ASP.NET
A set of technologies in the .NET Framework for building web applications and XML web services.
3,288 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,306 questions
{count} votes

Accepted answer
  1. Lan Huang-MSFT 25,876 Reputation points Microsoft Vendor
    2023-05-29T06:34:20.6433333+00:00

    Hi @Donald Symmons,

    I tested both methods of getting imgFileUpload.ImageUrl with your code, neither reproduced your error. I think you can refer to my code first. If that doesn't solve the problem, you'll need to provide more information.

    From FileUpload

     <div>
                <asp:TextBox ID="mailtxt" runat="server"></asp:TextBox>
                <asp:TextBox ID="orgName" runat="server"></asp:TextBox>
              <asp:FileUpload ID="FileUpload1" runat="server" />
    <asp:Button ID="btnUpload" Text="Upload" runat="server" OnClick="UploadFile" />
    <hr />
    <asp:Image ID="imgFileUpload" runat="server" Height = "100" Width = "100" />
            </div>
    
      protected void UploadFile(object sender, EventArgs e)
            {
                Stream fs = FileUpload1.PostedFile.InputStream;
                BinaryReader br = new BinaryReader(fs);
                byte[] bytes = br.ReadBytes((Int32)fs.Length);
                imgFileUpload.ImageUrl = "data:image/jpeg;base64," + Convert.ToBase64String(bytes);
                byte[] imageData = Convert.FromBase64String(imgFileUpload.ImageUrl.Replace("data:image/jpeg;base64,", ""));
                string query = @"INSERT INTO Register (email, Name, UploadBg) VALUES(@email, @Name, @UploadBg)";
                using (SqlCommand cmd1 = new SqlCommand(query, con))
                {
                    cmd1.CommandType = CommandType.Text;
                    cmd1.Parameters.AddWithValue("@email", mailtxt.Text.Trim());
                    cmd1.Parameters.AddWithValue("@Name", orgName.Text.Trim());
                    cmd1.Parameters.AddWithValue("@UploadBg", imageData);
                    cmd1.Connection = con;
                    con.Open();
                    cmd1.ExecuteNonQuery();
                }
                con.Close();
            }
    

    16

    From Database

     <div>
                <asp:TextBox ID="mailtxt" runat="server"></asp:TextBox>
                <asp:TextBox ID="orgName" runat="server"></asp:TextBox>
    <asp:Button ID="btnUpload" Text="Upload" runat="server" OnClick="UploadFile" />
    <hr />
    <asp:Image ID="imgFileUpload" runat="server" Height = "100" Width = "100" />
            </div>
    
     protected void Page_Load(object sender, EventArgs e)
            {
    
                con.Open();
                SqlCommand cmd = new SqlCommand();
                cmd.CommandText = "SELECT * FROM tblFiles WHERE id = 5";
                cmd.Connection = con;
                SqlDataAdapter sda = new SqlDataAdapter();
                DataSet ds = new DataSet();
                sda.SelectCommand = cmd;
                sda.Fill(ds, "detail");
                if (ds.Tables[0].Rows.Count > 0)
                {
                    byte[] image;
                    if (!Convert.IsDBNull(ds.Tables[0].Rows[0]["data"]))
                    {
                        image = (byte[])ds.Tables[0].Rows[0]["data"];
                        imgFileUpload.ImageUrl = "data:image/jpeg;base64," + Convert.ToBase64String(image);
                    }
                }
                con.Close();
            }
    
    
            protected void UploadFile(object sender, EventArgs e)
            {                   
                byte[] imageData = Convert.FromBase64String(imgFileUpload.ImageUrl.Replace("data:image/jpeg;base64,", ""));
                string query = @"INSERT INTO Register (email, Name, UploadBg) VALUES(@email, @Name, @UploadBg)";
                using (SqlCommand cmd1 = new SqlCommand(query, con))
                {
                    cmd1.CommandType = CommandType.Text;
                    cmd1.Parameters.AddWithValue("@email", mailtxt.Text.Trim());
                    cmd1.Parameters.AddWithValue("@Name", orgName.Text.Trim());
                    cmd1.Parameters.AddWithValue("@UploadBg", imageData);
                    cmd1.Connection = con;
                    con.Open();
                    cmd1.ExecuteNonQuery();
                }
                con.Close();
            }
    

    17

    Best regards,

    Lan Huang


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


0 additional answers

Sort by: Most helpful