Guest User

Guest User

  • Tech Writer
  • 271
  • 36.2k

save image to database in asp.net Using Entity framework

Aug 4 2020 5:52 AM
Hello...
 
I want to save image path in database... I am using fileupload and also give path but the problem is it is not saving in the database...
  1. protected void Button1_Click(object sender, EventArgs e)  
  2. {  
  3. Model.User newusermodel = new Model.User();  
  4. newusermodel.Image = TxtImg.ToString();  
  5. newusermodel.Name = TxtName.Text;  
  6. newusermodel.UserName = TxtUserName.Text;  
  7. newusermodel.EmailID = TxtEmailID.Text;  
  8. newusermodel.Created_Date = DateTime.Now;  
  9. newusermodel.User_Type_Id = 3;  
  10. newusermodel.Password = TxtPassword.Text;  
  11. using (WallpaperEntities4 context = new WallpaperEntities4())  
  12. {  
  13. User user = new User();  
  14. {  
  15. string str = TxtImg.FileName;  
  16. TxtImg.PostedFile.SaveAs(Server.MapPath("~/Upload/" + str));  
  17. string Image = "~/Upload/" + str.ToString();  
  18. string name = TxtImg.ToString();  
  19. user.Image = newusermodel.Image;  
  20. user.Name = newusermodel.Name;  
  21. user.UserName = newusermodel.UserName;  
  22. user.EmailID = newusermodel.EmailID;  
  23. user.Created_Date = newusermodel.Created_Date;  
  24. user.User_Type_Id = newusermodel.User_Type_Id;  
  25. user.Password = newusermodel.Password;  
  26. };  
  27. var usersadd = context.Set<User>();  
  28. var response = context.Users.Where(u => u.UserName == newusermodel.UserName).FirstOrDefault();  
  29. if(response == null)  
  30. {  
  31. usersadd.Add(user);  
  32. context.SaveChanges();  
  33. Response.Redirect("/Login.aspx");  
  34. }  
  35. else  
  36. {  
  37. LblMesge.Text = "User exists";  
  38. }  

Attachment: fileup_(2).zip

Answers (2)