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();
}
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();
}
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.