Hi RichardSa,
Please refer below code.
HTML
<asp:Label ID="lblId" runat="server"></asp:Label> <br />
<asp:Label ID="lblName" runat="server"></asp:Label><br />
<asp:Image ID="imgFile" runat="server" Width="60" Height="60" />
<hr />
<asp:TextBox ID="txtName" runat="server"></asp:TextBox>
<asp:Button ID="btnSubmit" runat="server" Text="insert Image" OnClick="OnClick" />
Namespaces
C#
using System.IO;
using System.Data;
using System.Configuration;
using System.Data.SqlClient;
VB.Net
Imports System.IO
Imports System.Data
Imports System.Configuration
Imports System.Data.SqlClient
Code
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
this.BindImage();
}
}
protected void OnClick(object sender, EventArgs e)
{
string name = txtName.Text.Trim();
// Convert Base64String to byte Array.
byte[] image = Convert.FromBase64String(imgFile.ImageUrl.Replace("data:image/jpeg;base64,", ""));
string constring = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(constring))
{
using (SqlCommand cmd = new SqlCommand("INSERT INTO Encrypt_Image(Name,Image) VALUES (@Name,@Image)", con))
{
cmd.Parameters.AddWithValue("@Name", name);
cmd.Parameters.AddWithValue("@Image", image);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
}
}
private void BindImage()
{
string constring = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(constring))
{
using (SqlCommand cmd = new SqlCommand("Select Id,Name,Data from tblfiles", con))
{
using (SqlDataAdapter sda = new SqlDataAdapter(cmd))
{
using (DataTable dt = new DataTable())
{
sda.Fill(dt);
if (dt.Rows.Count > 0)
{
int id = Convert.ToInt32(dt.Rows[0]["Id"]);
string name = dt.Rows[0]["Name"].ToString();
byte[] image = (byte[])dt.Rows[0]["Data"];
lblId.Text = id.ToString();
lblName.Text = name;
imgFile.ImageUrl = "data:image/jpeg;base64," + Convert.ToBase64String(image);
}
}
}
}
}
}
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not Me.IsPostBack Then
Me.BindImage()
End If
End Sub
Protected Sub OnClick(ByVal sender As Object, ByVal e As EventArgs)
Dim name As String = txtName.Text.Trim()
Dim image As Byte() = Convert.FromBase64String(imgFile.ImageUrl.Replace("data:image/jpeg;base64,", ""))
Dim constring As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Using con As SqlConnection = New SqlConnection(constring)
Using cmd As SqlCommand = New SqlCommand("INSERT INTO Encrypt_Image(Name,Image) VALUES (@Name,@Image)", con)
cmd.Parameters.AddWithValue("@Name", name)
cmd.Parameters.AddWithValue("@Image", image)
con.Open()
cmd.ExecuteNonQuery()
con.Close()
End Using
End Using
End Sub
Private Sub BindImage()
Dim constring As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Using con As SqlConnection = New SqlConnection(constring)
Using cmd As SqlCommand = New SqlCommand("Select Id,Name,Data from tblfiles", con)
Using sda As SqlDataAdapter = New SqlDataAdapter(cmd)
Using dt As DataTable = New DataTable()
sda.Fill(dt)
If dt.Rows.Count > 0 Then
Dim id As Integer = Convert.ToInt32(dt.Rows(0)("Id"))
Dim name As String = dt.Rows(0)("Name").ToString()
Dim image As Byte() = CType(dt.Rows(0)("Data"), Byte())
lblId.Text = id.ToString()
lblName.Text = name
imgFile.ImageUrl = "data:image/jpeg;base64," & Convert.ToBase64String(image)
End If
End Using
End Using
End Using
End Using
End Sub