Hi Tevin,
For Upload and save binary data in database refer below article.
Check this example. Now please take its reference and correct your code.
HTML
<asp:TextBox runat="server" ID="txtId" />
<asp:Button Text="Search" runat="server" ID="btnSearch" OnClick="Search" />
<br /><br />
<table>
<tr>
<td>Id</td>
<td><asp:Label ID="lblID" runat="server" /></td>
</tr>
<tr>
<td>Name</td>
<td><asp:TextBox runat="server" ID="txtName" /></td>
</tr>
<tr>
<td>Photo</td>
<td><asp:Image ID="imgPhoto" runat="server" /></td>
</tr>
</table>
<br />
<asp:Button Text="Save" runat="server" ID="btnSave" OnClick="Save" />
Namespaces
C#
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
VB.Net
Imports System.Configuration
Imports System.Data
Imports System.Data.SqlClient
Code
C#
protected void Search(object sender, EventArgs e)
{
string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand("SELECT id,Name,Data FROM tblFiles WHERE id = @Id"))
{
cmd.Parameters.AddWithValue("@Id", txtId.Text.Trim());
using (SqlDataAdapter sda = new SqlDataAdapter())
{
cmd.CommandType = CommandType.Text;
cmd.Connection = con;
sda.SelectCommand = cmd;
DataTable dt = new DataTable();
sda.Fill(dt);
if (dt.Rows.Count > 0)
{
lblID.Text = dt.Rows[0]["id"].ToString();
txtName.Text = dt.Rows[0]["Name"].ToString();
byte[] bytes = (byte[])dt.Rows[0]["Data"];
string base64String = Convert.ToBase64String(bytes, 0, bytes.Length);
imgPhoto.ImageUrl = "data:image/png;base64," + base64String;
}
}
}
}
}
protected void Save(object sender, EventArgs e)
{
string name = txtName.Text.Trim();
string contentType = "image/jpeg";
byte[] data = Convert.FromBase64String(imgPhoto.ImageUrl.Replace("data:image/png;base64,", ""));
string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand("INSERT INTO tblFiles (Name,ContentType,Data) VALUES(@Name,@ContentType,@Data)"))
{
cmd.Parameters.AddWithValue("@Name", name);
cmd.Parameters.AddWithValue("@ContentType", contentType);
cmd.Parameters.AddWithValue("@Data", data);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
}
}
VB.Net
Protected Sub Search(ByVal sender As Object, ByVal e As EventArgs)
Dim constr As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Using con As SqlConnection = New SqlConnection(constr)
Using cmd As SqlCommand = New SqlCommand("SELECT id,Name,Data FROM tblFiles WHERE id = @Id")
cmd.Parameters.AddWithValue("@Id", txtId.Text.Trim())
Using sda As SqlDataAdapter = New SqlDataAdapter()
cmd.CommandType = CommandType.Text
cmd.Connection = con
sda.SelectCommand = cmd
Dim dt As DataTable = New DataTable()
sda.Fill(dt)
If dt.Rows.Count > 0 Then
lblID.Text = dt.Rows(0)("id").ToString()
txtName.Text = dt.Rows(0)("Name").ToString()
Dim bytes As Byte() = CType(dt.Rows(0)("Data"), Byte())
Dim base64String As String = Convert.ToBase64String(bytes, 0, bytes.Length)
imgPhoto.ImageUrl = "data:image/png;base64," & base64String
End If
End Using
End Using
End Using
End Sub
Protected Sub Save(ByVal sender As Object, ByVal e As EventArgs)
Dim name As String = txtName.Text.Trim()
Dim contentType As String = "image/jpeg"
Dim data As Byte() = Convert.FromBase64String(imgPhoto.ImageUrl.Replace("data:image/png;base64,", ""))
Dim constr As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Using con As SqlConnection = New SqlConnection(constr)
Using cmd As SqlCommand = New SqlCommand("INSERT INTO tblFiles (Name,ContentType,Data) VALUES(@Name,@ContentType,@Data)")
cmd.Parameters.AddWithValue("@Name", name)
cmd.Parameters.AddWithValue("@ContentType", contentType)
cmd.Parameters.AddWithValue("@Data", data)
con.Open()
cmd.ExecuteNonQuery()
con.Close()
End Using
End Using
End Sub