Hi Friend’s, In this
post I have explained how to save image in Database and in Specified
Folder with Specified name in asp.net using c#.
Step1:-
Open visual studio -> create a new web site.
Step2:-
Create a folder whose
name is App_Code inside the application.
In this folder create one bal.cs, one dal.cs and one Utile.cs file inside this.
See the figure..
Bal.cs:-
using System;
using
System.Collections.Generic;
using
System.Linq;
using
System.Web;
/// <summary>
///
Summary description for bal
/// </summary>
public class bal
{
dal
d1 = new dal();
public
bal()
{
//
// TODO: Add constructor logic here
//
}
public
int insert(string
Username, string password, string email_id, string
image)
{
return d1.insert(Username,password,email_id,image);
}
}
Dal.cs:-
using System;
using
System.Collections.Generic;
using
System.Linq;
using
System.Web;
using
System.Data;
using
System.Data.SqlClient;
/// <summary>
///
Summary description for dal
/// </summary>
public class dal
{
public
dal()
{
//
// TODO: Add constructor logic here
//
}
public
int insert(string
Username,string password, string email_id,string
image)
{
SqlConnection con = new SqlConnection(@"Data
Source=SUSHIL-PC;Initial Catalog=modi;Integrated Security=True");
SqlCommand com = new SqlCommand("sp_insert",con);// here sp_insert is stored procedure
con.Open();
com.CommandType = CommandType.StoredProcedure;
com.Parameters.AddWithValue("@username",Username);
com.Parameters.AddWithValue("@password",password);
com.Parameters.AddWithValue("@email",email_id);
com.Parameters.AddWithValue("@photo",image);
return com.ExecuteNonQuery();
con.Close();
}
}
Utile.cs:
using System;
using
System.Collections.Generic;
using
System.Linq;
using
System.Web;
/// <summary>
///
Summary description for Util
/// </summary>
public class Util
{
public
Util()
{
//
// TODO: Add constructor logic here
//
}
public
static string
IMAGE = "image\\";
public
static string
TILED_ROOT = "~/";
public
static string
ROOT_PATH = System.Web.HttpContext.Current.Server.MapPath("~/");
public
static string
makePictureName( String orignalFileName)
{
return getName(orignalFileName) + "."
+ getExtension(orignalFileName);
}
public
static string
getName(String fileName)
{
if (fileName.Contains("."))
{
return fileName.Split('.')[0].ToString();
}
else
{
//throw Exception;
return fileName;
}
}
public
static string
getExtension(String fileName)
{
if (fileName.Contains("."))
{
return fileName.Split('.')[1].ToString();
}
else
{
//throw Exception;
return fileName;
}
}
public
static string
makePhysicalPath(String virtualPath)
{
return virtualPath.Replace('\\',
'/');
}
public
static string
makeVirtulPath(String virtualPath)
{
return virtualPath.Replace('/',
'\\');
}
}
Step3:
Make a folder which name is image(in which image is stored)
inside the project(see the above figure.)
Step4:
Create the table and
make a stored procedure .
Sql Query:
Create table register(@username varchar(max),@password varchar(50),@email varchar(50),@photo varchar(max));
Stored Procedure:
CREATE PROCEDURE
sp_insert
(
@username varchar(max),
@password varchar(50),
@email varchar(50),
@photo varchar(max)
)
AS
BEGIN
insert into register values(@username,@password,@email,@photo);
END
GO
Step5:Code for Design page and Code behid design means .cs
page.
Default.aspx:
<!DOCTYPE html PUBLIC "-//W3C//DTD
XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
UserName:<asp:TextBox ID="TextBox1"
runat="server"></asp:TextBox><br />
Password:<asp:TextBox ID="TextBox2"
runat="server"></asp:TextBox><br />
Email Id:<asp:TextBox ID="TextBox3"
runat="server"></asp:TextBox><br />
Photo:<asp:FileUpload ID="FileUpload1"
runat="server"
/><asp:Image ID="Image1" runat="server"
/><br />
<asp:Button ID="Button1" runat="server" Text="Save" onclick="Button1_Click" />
</div>
</form>
</body>
</html>
Default.aspx.cs:-
using System;
using
System.Collections.Generic;
using
System.Linq;
using
System.Web;
using
System.Web.UI;
using
System.Web.UI.WebControls;
using
System.Data;
using
System.Data.SqlClient;
public partial class _Default: System.Web.UI.Page
{
bal
b1 = new bal();
protected
void Page_Load(object
sender, EventArgs e)
{
}
protected
void Button1_Click(object
sender, EventArgs e)
{
string username = TextBox1.Text;
string password = TextBox2.Text;
string email = TextBox3.Text;
string image = FileUpload1.PostedFile.FileName;
if (FileUpload1.PostedFile != null
&& FileUpload1.PostedFile.FileName != "")
{
String picture_path = Util.IMAGE
+ Util.makePictureName(image);
FileUpload1.PostedFile.SaveAs(Util.ROOT_PATH
+ picture_path);
Image1.ImageUrl = Util.TILED_ROOT
+ Util.makePhysicalPath(picture_path);
b1.insert(username,
password, email, picture_path);
}
}
}
Step6:- This is final step . Debug the program and run
the code. If any problem regarding this post you can contact me at sushilct86@gmail.com. In next I will come with how show image from DataBase.
Please do not forget to comment for this post. You comment
is very important for other whose are reading this blog.
No comments:
Post a Comment
Note: only a member of this blog may post a comment.