ASPForums.Net RSS Feedhttp://www.aspforums.net/Handlers/RSS.ashxLatest additions to the content that appears on ASPForums.Net(c) 2019 www.aspforums.com. All rights reserved.Save (Insert) Binary Image to database using Image Control in ASP.Net<p>Hi <a class="username" rel="pratikshir"> pratikshir</a>,</p> <p>Refer below sample code.</p> <p><span style="text-decoration: underline;"><strong>HTML</strong></span></p> <pre class="brush: html">&lt;div class=&#34;col-sm-6&#34;&gt; &lt;div class=&#34;form-group&#34;&gt; &lt;div class=&#34;profile-upload&#34;&gt; &lt;div class=&#34;upload-img&#34;&gt; &lt;asp:Image ID=&#34;imgDoctor&#34; runat=&#34;server&#34; Height=&#34;40px&#34; Width=&#34;40px&#34; class=&#34;rounded-circle m-r-5&#34; /&gt; &lt;/div&gt; &lt;div class=&#34;upload-input&#34;&gt; &lt;asp:FileUpload ID=&#34;fuImage&#34; runat=&#34;server&#34; class=&#34;form-control&#34; /&gt; &lt;/div&gt; &lt;/div&gt; &lt;asp:Button Text=&#34;Update&#34; runat=&#34;server&#34; OnClick=&#34;Update&#34; /&gt; &lt;/div&gt; &lt;/div&gt;</pre> <p><span style="text-decoration: underline;"><strong>Namespaces</strong></span></p> <p><strong>C#</strong></p> <pre class="brush: csharp">using System.Data; using System.Data.SqlClient; using System.Configuration;</pre> <p><strong>VB.Net</strong></p> <pre class="brush: vb">Imports System.Data Imports System.Data.SqlClient Imports System.Configuration</pre> <p><span style="text-decoration: underline;"><strong>Code</strong></span></p> <p><strong>C#</strong></p> <pre class="brush: csharp">protected void Page_Load(object sender, EventArgs e) { if (!this.IsPostBack) { string constr = ConfigurationManager.ConnectionStrings[&#34;constr&#34;].ConnectionString; SqlConnection con = new SqlConnection(constr); SqlCommand cmd = new SqlCommand(&#34;SELECT Image_Data FROM tblFiles&#34;, con); SqlDataAdapter da = new SqlDataAdapter(cmd); DataSet ds = new DataSet(); da.Fill(ds); if (ds.Tables[0].Rows[0][&#34;Image_Data&#34;] != DBNull.Value) { string imageUrl = &#34;data:image/jpg;base64,&#34; + Convert.ToBase64String((byte[])ds.Tables[0].Rows[0][&#34;Image_Data&#34;]); imgDoctor.ImageUrl = imageUrl; } } } protected void Update(object sender, EventArgs e) { string url = imgDoctor.ImageUrl; byte[] bytes = Convert.FromBase64String(imgDoctor.ImageUrl.Split(&#39;,&#39;)[1]); string constr = ConfigurationManager.ConnectionStrings[&#34;constr&#34;].ConnectionString; using (SqlConnection conn = new SqlConnection(constr)) { string sql = &#34;UPDATE tblFiles SET Data=@Data WHERE Id= @Id&#34;; using (SqlCommand cmd = new SqlCommand(sql, conn)) { cmd.Parameters.AddWithValue(&#34;@Id&#34;, 1); cmd.Parameters.AddWithValue(&#34;@Data&#34;, bytes); conn.Open(); cmd.ExecuteNonQuery(); conn.Close(); } } }</pre> <p><strong>VB.Net</strong></p> <pre class="brush: vb">Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load If Not Me.IsPostBack Then Dim constr As String = ConfigurationManager.ConnectionStrings(&#34;constr&#34;).ConnectionString Dim con As SqlConnection = New SqlConnection(constr) Dim cmd As SqlCommand = New SqlCommand(&#34;SELECT Image_Data FROM tblFiles&#34;, con) Dim da As SqlDataAdapter = New SqlDataAdapter(cmd) Dim ds As DataSet = New DataSet() da.Fill(ds) If ds.Tables(0).Rows(0)(&#34;Image_Data&#34;) &lt;&gt; Nothing Then Dim imageUrl As String = &#34;data:image/jpg;base64,&#34; &amp; Convert.ToBase64String(CType(ds.Tables(0).Rows(0)(&#34;Image_Data&#34;), Byte())) imgDoctor.ImageUrl = imageUrl End If End If End Sub Protected Sub Update(ByVal sender As Object, ByVal e As EventArgs) Dim url As String = imgDoctor.ImageUrl Dim bytes As Byte() = Convert.FromBase64String(imgDoctor.ImageUrl.Split(&#34;,&#34;c)(1)) Dim constr As String = ConfigurationManager.ConnectionStrings(&#34;constr&#34;).ConnectionString Using conn As SqlConnection = New SqlConnection(constr) Dim sql As String = &#34;UPDATE tblFiles SET Data=@Data WHERE Id= @Id&#34; Using cmd As SqlCommand = New SqlCommand(sql, conn) cmd.Parameters.AddWithValue(&#34;@Id&#34;, 1) cmd.Parameters.AddWithValue(&#34;@Data&#34;, bytes) conn.Open() cmd.ExecuteNonQuery() conn.Close() End Using End Using End Sub</pre> <p>&nbsp;</p>https://www.aspforums.net:443/Threads/260847/Save-Insert-Binary-Image-to-database-using-Image-Control-in-ASPNet/https://www.aspforums.net:443/Threads/260847/Save-Insert-Binary-Image-to-database-using-Image-Control-in-ASPNet/Mon, 26 Aug 2019 03:05:47 GMT