If you often need to retrieve data without the actual image you could also move this column to a child table or use lazy loading so that you don't retrieve image bytes when they are not needed.
If too difficult for now, the main problem with the current approach seems that you are reusing the client side file name. You could store the file name in a database table and name your file using the pk (or using a Guid.NewGuid() value stored in the db
as well).
Member
11 Points
32 Posts
save image in database asp.net entity framework
Aug 04, 2020 11:08 AM|guestadmin@nirvriti.com|LINK
Dear Expets...
need ur help i want save image in database table..the image is save in folder but not in database table..my approach for this senerio is..
protected void Button1_Click(object sender, EventArgs e)
{
Model.User newusermodel = new Model.User();
newusermodel.Image = TxtImg.ToString();
newusermodel.Name = TxtName.Text;
newusermodel.UserName = TxtUserName.Text;
newusermodel.EmailID = TxtEmailID.Text;
newusermodel.Created_Date = DateTime.Now;
newusermodel.User_Type_Id = 3;
newusermodel.Password = TxtPassword.Text;
using (WallpaperEntities4 context = new WallpaperEntities4())
{
User user = new User();
{
string str = TxtImg.FileName;
TxtImg.PostedFile.SaveAs(Server.MapPath("~/Upload/" + str));
string Image = "~/Upload/" + str.ToString();
string name = TxtImg.ToString();
user.Image = newusermodel.Image;
user.Name = newusermodel.Name;
user.UserName = newusermodel.UserName;
user.EmailID = newusermodel.EmailID;
user.Created_Date = newusermodel.Created_Date;
user.User_Type_Id = newusermodel.User_Type_Id;
user.Password = newusermodel.Password;
};
var usersadd = context.Set<User>();
var response = context.Users.Where(u => u.UserName == newusermodel.UserName).FirstOrDefault();
if(response == null)
{
usersadd.Add(user);
context.SaveChanges();
Response.Redirect("/Login.aspx");
}
else
{
LblMesge.Text = "User exists";
}
All-Star
48490 Points
18070 Posts
Re: save image in database asp.net entity framework
Aug 04, 2020 12:13 PM|PatriceSc|LINK
Hi,
And the database you are using is? In SQL Server it could be a varbinary(max) column which is exposed as a byte array by EF. For example see :https://www.codeproject.com/Articles/658522/Storing-images-in-SQL-Server-using-EF-and-ASP-NET
If you often need to retrieve data without the actual image you could also move this column to a child table or use lazy loading so that you don't retrieve image bytes when they are not needed.
You may want to check https://docs.microsoft.com/en-us/sql/relational-databases/blob/binary-large-object-blob-data-sql-server?view=sql-server-ver15
If too difficult for now, the main problem with the current approach seems that you are reusing the client side file name. You could store the file name in a database table and name your file using the pk (or using a Guid.NewGuid() value stored in the db as well).
Member
11 Points
32 Posts
Re: save image in database asp.net entity framework
Aug 05, 2020 06:59 AM|guestadmin@nirvriti.com|LINK
Its done..
protected void Button1_Click(object sender, EventArgs e)
{
Model.User newusermodel = new Model.User();
string str = FileImg.FileName;
FileImg.PostedFile.SaveAs(Server.MapPath("~/Upload/" + str));
string Image = "~/Upload/" + str.ToString();
newusermodel.Image = Image;
newusermodel.Name = TxtName.Text;
newusermodel.UserName = TxtUserName.Text;
newusermodel.EmailID = TxtEmailID.Text;
newusermodel.Created_Date = DateTime.Now;
newusermodel.User_Type_Id = 3;
newusermodel.Password = TxtPassword.Text;
using (WallpaperEntities4 context = new WallpaperEntities4())
{
User user = new User();
{
string name = FileImg.ToString();
user.Image = newusermodel.Image;
user.Name = newusermodel.Name;
user.UserName = newusermodel.UserName;
user.EmailID = newusermodel.EmailID;
user.Created_Date = newusermodel.Created_Date;
user.User_Type_Id = newusermodel.User_Type_Id;
user.Password = newusermodel.Password;
};
var usersadd = context.Set<User>();
var response = context.Users.Where(u => u.UserName == newusermodel.UserName).FirstOrDefault();
if (response == null)
{
usersadd.Add(user);
context.SaveChanges();
Response.Redirect("/Login.aspx");
}
else
{
LblMesge.Text = "User exists";
}