locked
Want to update data through entity framework in asp.net c# RRS feed

  • Question

  • User-1647172364 posted

    Here is my update code

    C# protected void BT_update_Click(object sender, EventArgs e) { using (SchoolEntities2 ctx = new SchoolEntities2()) { var package = (from c in ctx.packages where c.tbl_id = (HiddenField1.Value) select c).FirstOrDefault(); package.package_name = tb_name.Text; package.reward = dd3.SelectedValue.ToString(); package.remarks = tb_remarks.Text; ctx.SaveChanges(); } }

    Monday, June 22, 2020 6:47 AM

All replies

  • User475983607 posted

    The code is incomplete and you have not explained the problem.  It is your responsibility to provide enough code for the community to reproduce this issue.  Otherwise, it is up to you to use the standard debugging tools and find the source of the problem.

    Monday, June 22, 2020 11:09 AM
  • User-1647172364 posted
    C#
    
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    
    
    namespace WebApplication14
    {
    
        public partial class Model : System.Web.UI.Page
        {
            protected void Page_Load(object sender, EventArgs e)
            {
                if (!Page.IsPostBack)
                {
                   
                    Filldropdownlist();
                }
            }
    
           
            public void Filldropdownlist()
            {
                using (SchoolEntities2 ctx = new SchoolEntities2())
    
    
                    dd3.DataSource = (from SchoolEntities2 in ctx.packages select SchoolEntities2).Distinct().ToList();
                   dd3.DataBind();
                dd3.Items.Insert(0, new ListItem() { Text = "Select", Value = "0" });
            }
            
            protected void BT_submit_Click(object sender, EventArgs e)
            {
                //model.package_name = tb_name.Text.Trim();
                //model.reward = tb_reward.Text.Trim();
                //model.remarks = tb_remarks.Text.Trim();
    
                int status = 0;
                using (SchoolEntities2 ctx = new SchoolEntities2())
                {
                    ctx.packages.Add(new package()
                    {
    
                        package_name = tb_name.Text,
                        reward = dd3.SelectedItem.ToString(),
                        remarks = tb_remarks.Text,
                        by_whom = Session["username"].ToString(),
                        date_time = System.DateTime.Now.ToLocalTime()
    
                    }) ;
    
                    
                    status = ctx.SaveChanges();
                    
                    
    }
            }
    
            protected void BT_update_Click(object sender, EventArgs e)
            {
    
                
                using (SchoolEntities2 ctx = new SchoolEntities2())
                {
                    var std = ctx.packages.FirstOrDefault<package>();
                    std.package_name = tb_name.Text;
                    std.reward = dd3.SelectedValue.ToString();
                    std.remarks = tb_remarks.Text;
    
                    ctx.Entry(std).State = System.Data.Entity.EntityState.Modified;
                    ctx.SaveChanges();
                    
                }
    
            }
    
    
            protected void BT_delete_Click(object sender, EventArgs e)
            {
                using (SchoolEntities2 ctx = new SchoolEntities2())
                {
                    
    
                    var std = ctx.packages.First<package>();
                    ctx.packages.Remove(std);
                    ctx.SaveChanges();
                    
                
                }
            }
    
    Aspx
    
    Page Title="" Language="C#" MasterPageFile="~/Site1.Master" AutoEventWireup="true" CodeBehind="Model.aspx.cs" Inherits="WebApplication14.Model" %>
    <asp:Content ID="Content1" ContentPlaceHolderID="title" runat="server">
    </asp:Content>
    <asp:Content ID="Content2" ContentPlaceHolderID="head" runat="server">
    </asp:Content>
    <asp:Content ID="Content3" ContentPlaceHolderID="body" runat="server">
    
        
                   <div style="margin-top:30px; margin-left:20px">
            
                   
                       <h1><strong style="color:black">Entity Fm 6.0</strong></h1>    
                       <hr />
               
        <table>
                       
                
         <tr>
                        <td><span style="color:black">Package Name</span><br />
                        <asp:TextBox ID ="tb_name"  runat="server" Width="200px"   /><br />
            
          
                        </td>
    
                       <td><span style="margin-left:25px; color:black">Reward<br /></span>
                        <span style="margin-left:25px">
                            <asp:DropDownList ID="dd3" runat="server" Width="200px" DataTextField="reward">
                            
    
                                                       </asp:DropDownList></span> <br />
           
                       </td>
                         
                        <td><span style=" margin-left:25px; color:black">Remarks</><br /></span>
                        <span style="margin-left:25px"><asp:TextBox ID="tb_remarks"  runat="server" Width="410px" /></span> <br />
          
                            
                        </td>
                         
                        
                      
             </tr>
    
                              
         <tr>   
             <td>     
            <br /> <asp:Button ID="BT_submit" runat="server"  Text="Submit" style="border-radius:5px" Height="35px" Width="80px"   OnClick="BT_submit_Click" BackColor="#3366CC" BorderColor="#3366CC" ForeColor="White"  />
           
         </td>
             <td>
               <br /><asp:Button ID="BT_update" runat="server"   Text="Update" style="border-radius:5px; margin-left:1.5em" Height="35px" Width="80px"   OnClick="BT_update_Click" BackColor="#3366CC" BorderColor="#3366CC" ForeColor="White" />
         
             </td>
                  <td><br /><asp:Button ID="BT_delete" runat="server" Text="Delete" style=  " margin-left:1.6em;    border-radius:5px" Height="35px" Width="80px"  OnClick="BT_delete_Click" BackColor="#3366CC" BorderColor="#3366CC"  ForeColor="White" /> </td>
           
    
             </tr>
    
    
    
        </table></div>
    

    Monday, June 22, 2020 11:23 AM
  • User288213138 posted

    Hi sanam13,

    sanam13

    var package = (from c in ctx.packages
                                   where c.tbl_id = (HiddenField1.Value)

    Can your code compile successfully? Shouldn't it be "== "after where?

    I tested your code, it can be successfully updated.

    Please try to debug your code, and make sure it can get the value of the control in the code behind.

    Best regards,

    Sam

    Tuesday, June 23, 2020 3:28 AM
  • User-1647172364 posted

    Hlo sir 

    I have applied your given code . Getting 2 errors.  Error occur on Hiddenfield1.Value property. Errors are "Error CS0742 A query body must end with a select clause or a group clause". 

    "Error CS0029 Cannot implicitly convert type 'string' to 'decimal' "

     protected void BT_update_Click(object sender, EventArgs e)
            {
                
                using (SchoolEntities2 ctx = new SchoolEntities2())
                {
    
                    var std = (from c in ctx.packages where c.tbl_id = (HiddenField1.Value));
    
                    std.package_name = tb_name.Text;
                    std.reward = dd3.SelectedValue.ToString();
                    std.remarks = tb_remarks.Text;
    
    
                    ctx.Entry(std).State = System.Data.Entity.EntityState.Modified;
                    ctx.SaveChanges();
                    Fillgridview();
    
                }
    
            }

     

    Tuesday, June 23, 2020 6:44 AM
  • User288213138 posted

    Hi sanam13,

    Error CS0742 A query body must end with a select clause or a group clause". 

    from c in ctx.packages where c.tbl_id = (HiddenField1.Value)

    from c in ctx.packages where c.tbl_id = (HiddenField1.Value)
                                   select c)

    Why are your 2 queries different? you should add "select c" in your query. 

    "Error CS0029 Cannot implicitly convert type 'string' to 'decimal' "

    This error is because HiddenField1.Value is of type string, and tbl_id is of type decimal, so you only need to do type conversion.

    Convert.ToDecimal (HiddenField1.Value)

    Best regards,

    Sam

    Tuesday, June 23, 2020 8:54 AM
  • User-1647172364 posted

    Now i again tried Ur given code. Now the error occur on this code.  decimal Tbl_id = Convert.ToDecimal(HiddenField1.Value); 

    Error is 'Input string was not in a correct format.'

    Please execute it sir.

    protected void BT_update_Click(object sender, EventArgs e)
            {
               
                using (SchoolEntities2 ctx = new SchoolEntities2())
                {
    
                     decimal Tbl_id = Convert.ToDecimal(HiddenField1.Value);
    
                    
                    var std = (from c in ctx.packages
                                   where c.tbl_id == Tbl_id select c).First();
                    
                    std.package_name = tb_name.Text;
                    std.reward = dd3.SelectedValue.ToString();
                    std.remarks = tb_remarks.Text;
    
    
                    ctx.Entry(std).State = System.Data.Entity.EntityState.Modified;
                    ctx.SaveChanges();
                   
    
                }
    
            }

    Tuesday, June 23, 2020 9:13 AM
  • User288213138 posted

    Hi sanam13,

    Now i again tried Ur given code. Now the error occur on this code.  decimal Tbl_id = Convert.ToDecimal(HiddenField1.Value); 

    Error is 'Input string was not in a correct format.'

    Please make sure that the value in HiddenField1 is of type decimal.

    Best regards,

    Sam

    Tuesday, June 23, 2020 9:19 AM
  • User-1647172364 posted

    With your reference sir i have added a value in code  <asp:HiddenField ID="HiddenField1" runat="server" Value="69" /> 

    value = 69 is a tbl_id of one row but i want to update all rows. Please suggest me that method.


     

    Tuesday, June 23, 2020 10:52 AM
  • User288213138 posted

    Hi sanam13,

    but i want to update all rows. Please suggest me that method.

    If you want to update all rows, then you need to traverse all the data. here a demo for you as a reference.

    var idList=new int[]{1, 2, 3, 4};
    using (var db=new SomeDatabaseContext())
    {
        var friends= db.Friends.Where(f=>idList.Contains(f.ID)).ToList();
        friends.ForEach(a=>a.msgSentBy='1234');
        db.SaveChanges();
    }

    Best regards,

    Sam

    Wednesday, June 24, 2020 2:36 AM
  • User-1647172364 posted

    Sir this is also a static method which u provided in this code we also first declare rows {1,2,3,4} like this and i want dynamic method. Please execute the code through hiddenfield control and using label or provide any method sir.

    Wednesday, June 24, 2020 6:51 AM
  • User288213138 posted

    Hi sanam13,

    Sir this is also a static method which u provided in this code we also first declare rows {1,2,3,4} like this and i want dynamic method. Please execute the code through hiddenfield control and using label or provide any method sir.

    According to your aspx code, there are no multiple rows, so I can’t understand what it means to update multiple rows and can only provide you with a demo.

    Best regards,

    Sam

    Thursday, June 25, 2020 9:15 AM