Upload Image using asp.net C#
- To insert image/video into database - we have to save image/video in folder and we will insert the path of image/video file in database
- we can also store images in database by converting them into binary format, in that case we have to use varbinary datatype at server side
Example :
Frond-End code
<div class="form-group">
<asp:FileUpload runat="server" ID="fugallery" CssClass="form-control" Style="height:auto" AllowMultiple="true"/>
</div>
<asp:Button runat="server" CssClass="btn btn-primary" OnClick="btnsubmit_Click" ID="btnsubmit" OnClientClick="return CheckFileExistence()" BorderStyle="Double" Style="margin-top:20px" Text="Upload Photos" />
Back-End code
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
protected void btnsubmit_Click(object sender, EventArgs e)
{
try
{
foreach (var files in fugallery.PostedFiles)
{
string ext = System.IO.Path.GetExtension(files.FileName);
Guid g = Guid.NewGuid();
string imageNameid = g.ToString().Substring(10);
string imgName = imageNameid + ext;
string pathimg = "../Photos/" + imgName;
files.SaveAs(Server.MapPath(pathimg));
string imgPath1 = "../ThumbPhotos/" + imgName;
int widthC = 600;
int heightC = 400;
System.IO.Stream streamC = files.InputStream;
System.Drawing.Bitmap imageC = new Bitmap(streamC);
Bitmap targetC = new Bitmap(widthC, heightC);
Graphics graphicC = Graphics.FromImage(targetC);
graphicC.DrawImage(imageC, 0, 0, widthC, heightC);
targetC.Save(Server.MapPath(imgPath1));
con.Close();
SqlCommand cmd = new SqlCommand("insert into DBPhotos(ThumbPhoto,OriginalPhoto,PhotoType,SubmitDate) values('" + imgPath1 + "','" + pathimg + "','NA','" + DateTime.Now + "')", con);
con.Open();
cmd.ExecuteNonQuery();
showgalleryphotos();
this.ClientScript.RegisterStartupScript(this.GetType(), "SweetAlert", "swal('Gallery Photo/Photos Uploaded,Thank you!','','success');", true);
}
}
catch (Exception es)
{
}
finally
{
con.Close();
}
}
}
Sqldatareader in C#
- SqlDataReader Object provides a connection oriented data access to the SQL Server data Sources from C# applications
- The Read() method in the DataReader is used to read the rows from DataReader
Show data from SQL Database
Example :
Frond-End code
<div class="row">
<div class="col-lg-10" style="padding-top: 30px; padding-bottom: 30px">
<div class="card">
<div class="card-header"><strong>Edit Blog</strong></div>
<div class="card-body card-block">
<div class="form-group">
<label> for="company" class=" form-control-label">Blog Title *</label>
<asp:TextBox> runat="server" CssClass="form-control" ID="txttitle"></asp:TextBox>
</div>
<div class="form-group">
<label> for="company" class=" form-control-label">Blog Content *</label>
<asp:TextBox> runat="server" TextMode="MultiLine" CssClass="form-control" ID="txtcontent"></asp:TextBox>
</div>
<div class="form-group">
<label> for="company" class="form-control-label">Blog Image *</label>
<asp:FileUpload runat="server" Style="margin-bottom:20px" ID="fufile" CssClass="form-control" />
<label> for="company" class="form-control-label">Current Blog Image :</label><br />
<asp:Image ID="imgblog" runat="server" />
</div>
<asp:Button runat="server" OnClientClick="return valid()" OnClick="btnsubmit_Click" CssClass="btn btn-primary" ID="btnsubmit" Text="Update" />
</div>
</div>
</div>
</div>
Back-End code
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data.SqlClient;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace MyTaxSolutions.AdminControl
{
public partial class Editblog : System.Web.UI.Page
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["connstr"].ConnectionString);
protected void Page_Load(object sender, EventArgs e)
{
getblogdata();
}
protected void getblogdata()
{
con.Close();
SqlCommand cmd = new SqlCommand("select * from Blogs where Srno='" + Request.QueryString["id"] + "'", con);
con.Open();
SqlDataReader sdr = cmd.ExecuteReader();
if (sdr.HasRows)
{
sdr.Read();
txttitle.Text = sdr.GetValue(1).ToString();
imgblog.ImageUrl = sdr.GetValue(2).ToString();
txtcontent.Text = sdr.GetValue(3).ToString();
}
}
}
}