locked
save data using dropdown in asp.net entity framework RRS feed

  • Question

  • User186310208 posted

    hello...

    save data using dropdown but i am getting a error..

    "Cannot implicity convert type "long" to 'String'" 

    <table class="style1">

    <asp:FileUpload ID="filePath" runat="server" />
    <tr>

    <td>Category:</td>
    <td>
    <asp:DropDownList runat="server" ID="ddlWallCategry">
    </asp:DropDownList></td>
    </tr>
    </table>

    </div>
    <asp:Button ID="btnSave" runat="server" Text="Save" OnClick="btnSave_Click" />

    protected void Page_Load(object sender, EventArgs e)
    {
    if (!IsPostBack)
    {
    bindUsingCategory();
    }
    }

    public void bindUsingCategory()
    {
    using (WallpaperEntities10 context = new WallpaperEntities10())
    {
    ddlWallCategry.DataSource = (from r in context.WallPaperCategries select new { CategoryName = r.CategoryName, Id = r.Id }).ToList();


    ddlWallCategry.DataTextField = "CategoryName";
    ddlWallCategry.DataValueField = "Id";
    ddlWallCategry.DataBind();
    ddlWallCategry.Items.Insert(0, new System.Web.UI.WebControls.ListItem("Select Option"));
    }
    }

    protected void btnSave_Click(object sender, EventArgs e)
    {
    Model.WallpaperCategory newusermodel = new Model.WallpaperCategory();
    string str = filePath.FileName;
    filePath.PostedFile.SaveAs(Server.MapPath("~/Upload/" + str));
    string Path = "~/Upload/" + str.ToString();
    newusermodel.Category = ddlWallCategry.SelectedIndex;


    using (var context = new WallpaperEntities10())
    {
    WallpaperRecord user = new WallpaperRecord();
    {
    string name = filePath.ToString();
    user.Category = newusermodel.Category; //getting error in this line

    };

    var usersadd = context.Set<WallpaperRecord>();
    var response = context.WallpaperRecords.Where(u => u.Category = newusermodel.Category).FirstOrDefault();
    if (response == null)
    {
    usersadd.Add(user);
    context.SaveChanges();

    }


    }

    Friday, August 14, 2020 9:01 AM

Answers

  • User-821857111 posted

    "Cannot implicity convert type "long" to 'String'" 
    It appears that the user.Category's type is a string, whereas the newusermodel.Category's type is a long (Int64). First, you should check that the properties actually represent the values you intend them to represent. If they do, you need to convert newusermodel.Category to a string:

    user.Category = newusermodel.Category.ToString();

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, August 17, 2020 5:25 AM