locked
save image in database asp.net entity framework RRS feed

  • Question

  • User186310208 posted

    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";
    }

    Tuesday, August 4, 2020 11:08 AM

Answers

  • User186310208 posted

    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";
    }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, August 5, 2020 6:59 AM

All replies

  • User753101303 posted

    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).

    Tuesday, August 4, 2020 12:13 PM
  • User186310208 posted

    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";
    }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, August 5, 2020 6:59 AM