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, it should save as well.
The example 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.
My insert code
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();