Guest User

Guest User

  • Tech Writer
  • 271
  • 32.8k

save data using dropdown in asp.net Entity Framework

Aug 14 2020 3:51 AM
Hello..
 
i am saving a data using dropdown but i am getting a error
 
"Cannot implicity convert type "long" to 'String'"
  1. <table class="style1">  
  2. <asp:FileUpload ID="filePath" runat="server" />  
  3. <tr>  
  4. <td>Category:</td>  
  5. <td>  
  6. <asp:DropDownList runat="server" ID="ddlWallCategry">  
  7. </asp:DropDownList></td>  
  8. </tr>  
  9. </table>  
  10. </div>  
  11. <asp:Button ID="btnSave" runat="server" Text="Save" OnClick="btnSave_Click" />  
  1. protected void Page_Load(object sender, EventArgs e)  
  2. {  
  3. if (!IsPostBack)  
  4. {  
  5. bindUsingCategory();  
  6. }  
  7. }  
  8. public void bindUsingCategory()  
  9. {  
  10. using (WallpaperEntities10 context = new WallpaperEntities10())  
  11. {  
  12. ddlWallCategry.DataSource = (from r in context.WallPaperCategries select new { CategoryName = r.CategoryName, Id = r.Id }).ToList();  
  13. ddlWallCategry.DataTextField = "CategoryName";  
  14. ddlWallCategry.DataValueField = "Id";  
  15. ddlWallCategry.DataBind();  
  16. ddlWallCategry.Items.Insert(0, new System.Web.UI.WebControls.ListItem("Select Option"));  
  17. }  
  18. }  
  19. protected void btnSave_Click(object sender, EventArgs e)  
  20. {  
  21. Model.WallpaperCategory newusermodel = new Model.WallpaperCategory();  
  22. string str = filePath.FileName;  
  23. filePath.PostedFile.SaveAs(Server.MapPath("~/Upload/" + str));  
  24. string Path = "~/Upload/" + str.ToString();  
  25. newusermodel.Category = ddlWallCategry.SelectedIndex;  
  26. using (var context = new WallpaperEntities10())  
  27. {  
  28. WallpaperRecord user = new WallpaperRecord();  
  29. {  
  30. string name = filePath.ToString();  
  31. user.Category = newusermodel.Category; //getting error in this line  
  32. };  
  33. var usersadd = context.Set<WallpaperRecord>();  
  34. var response = context.WallpaperRecords.Where(u => u.Category = newusermodel.Category).FirstOrDefault();  
  35. if (response == null)  
  36. {  
  37. usersadd.Add(user);  
  38. context.SaveChanges();  
  39. }  
  40. }  
  41. }  

Answers (3)