Click here to Skip to main content
16,019,873 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
This is my code behind file in Details.aspx.cs

C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Details : System.Web.UI.Page
{
    DataClassesDataContext db = new DataClassesDataContext();
    Data_Acess da = new Data_Acess();
    Register rg=new Register();
    protected void Page_Load(object sender, EventArgs e)
    {

        Display();
     
    }
    private void Display()
    {
        var query = from c in db.Registers where c.Name == Convert.ToString(Session["Name"]) select c;
        GridView1.DataSource = query;
        GridView1.DataBind();
    }

    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {
        GridView1.EditIndex = e.NewEditIndex;
        Display();
    }
    protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        int id = (int)GridView1.DataKeys[e.RowIndex].Value;
     
  
        TextBox name = (TextBox)GridView1.Rows[e.RowIndex].FindControl("TextBox1");
        TextBox mobile = (TextBox)GridView1.Rows[e.RowIndex].FindControl("TextBox2");
        TextBox address = (TextBox)GridView1.Rows[e.RowIndex].FindControl("TextBox3");
        TextBox city = (TextBox)GridView1.Rows[e.RowIndex].FindControl("TextBox4");
        TextBox gender = (TextBox)GridView1.Rows[e.RowIndex].FindControl("TextBox5");
        TextBox email = (TextBox)GridView1.Rows[e.RowIndex].FindControl("TextBox6");
        TextBox dateofbirth = (TextBox)GridView1.Rows[e.RowIndex].FindControl("TextBox7");

        Register reg = (from c in db.Registers where c.Id == id select c).FirstOrDefault();
        reg.Name = name.Text;
        reg.Mobile = Convert.ToInt32(mobile.Text);
        reg.Address = address.Text;
        reg.City = city.Text;
        reg.Gender = gender.Text;
        reg.Email = email.Text;
        reg.Dateofbirth = dateofbirth.Text;
        db.SubmitChanges();



        GridView1.EditIndex = -1;
        GridView1.DataBind();
        

    }


I am unable to update data, i am using visual studio 2012 pls help me with that
this is my HTML code for details.Aspx page

ASP.NET
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Details.aspx.cs" Inherits="Details" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <style type="text/css">
        .auto-style1 {
            width: 136px;
        }
        .auto-style2 {
            width: 1048px;
            text-align: center;
        }
        .auto-style3 {
            width: 136px;
            height: 43px;
        }
        .auto-style4 {
            width: 1048px;
            text-align: center;
            height: 43px;
            font-size: x-large;
        }
        .auto-style5 {
            height: 43px;
        }
        .auto-style6 {
            width: 136px;
            height: 37px;
        }
        .auto-style7 {
            width: 1048px;
            text-align: center;
            height: 37px;
        }
        .auto-style8 {
            height: 37px;
        }
    </style>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    
        <table style="width:100%;">
            <tr>
                <td class="auto-style3"></td>
                <td class="auto-style4">Details Page</td>
                <td class="auto-style5"></td>
            </tr>
            <tr>
                <td class="auto-style1"> </td>
                <td class="auto-style2">
                    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" BackColor="#CCCCCC" BorderColor="#999999" BorderStyle="Solid" BorderWidth="3px" CellPadding="4" CellSpacing="2" ForeColor="Black" style="text-align: center" Width="1031px" AutoGenerateDeleteButton="True" AutoGenerateEditButton="True" DataKeyNames="Id" OnRowEditing="GridView1_RowEditing" OnRowUpdating="GridView1_RowUpdating" >
                        <Columns>
                            <asp:BoundField DataField="Id" HeaderText="Id" SortExpression="Id" />
                            <asp:TemplateField HeaderText="Name" SortExpression="Name">
                                <EditItemTemplate>
                                    <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("Name") %>'></asp:TextBox>
                                </EditItemTemplate>
                                <ItemTemplate>
                                    <asp:Label ID="Label1" runat="server" Text='<%# Bind("Name") %>'></asp:Label>
                                </ItemTemplate>
                            </asp:TemplateField>
                            <asp:TemplateField HeaderText="Mobile" SortExpression="Mobile">
                                <EditItemTemplate>
                                    <asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("Mobile") %>'></asp:TextBox>
                                </EditItemTemplate>
                                <ItemTemplate>
                                    <asp:Label ID="Label2" runat="server" Text='<%# Bind("Mobile") %>'></asp:Label>
                                </ItemTemplate>
                            </asp:TemplateField>
                            <asp:TemplateField HeaderText="Address" SortExpression="Address">
                                <EditItemTemplate>
                                    <asp:TextBox ID="TextBox3" runat="server" Text='<%# Bind("Address") %>'></asp:TextBox>
                                </EditItemTemplate>
                                <ItemTemplate>
                                    <asp:Label ID="Label3" runat="server" Text='<%# Bind("Address") %>'></asp:Label>
                                </ItemTemplate>
                            </asp:TemplateField>
                            <asp:TemplateField HeaderText="City" SortExpression="City">
                                <EditItemTemplate>
                                    <asp:TextBox ID="TextBox4" runat="server" Text='<%# Bind("City") %>'></asp:TextBox>
                                </EditItemTemplate>
                                <ItemTemplate>
                                    <asp:Label ID="Label4" runat="server" Text='<%# Bind("City") %>'></asp:Label>
                                </ItemTemplate>
                            </asp:TemplateField>
                            <asp:TemplateField HeaderText="Gender" SortExpression="Gender">
                                <EditItemTemplate>
                                    <asp:TextBox ID="TextBox5" runat="server" Text='<%# Bind("Gender") %>'></asp:TextBox>
                                </EditItemTemplate>
                                <ItemTemplate>
                                    <asp:Label ID="Label5" runat="server" Text='<%# Bind("Gender") %>'></asp:Label>
                                </ItemTemplate>
                            </asp:TemplateField>
                            <asp:TemplateField HeaderText="Email" SortExpression="Email">
                                <EditItemTemplate>
                                    <asp:TextBox ID="TextBox6" runat="server" Text='<%# Bind("Email") %>'></asp:TextBox>
                                </EditItemTemplate>
                                <ItemTemplate>
                                    <asp:Label ID="Label6" runat="server" Text='<%# Bind("Email") %>'></asp:Label>
                                </ItemTemplate>
                            </asp:TemplateField>
                            <asp:TemplateField HeaderText="Date Of Birth" SortExpression="Dateofbirth">
                                <EditItemTemplate>
                                    <asp:TextBox ID="TextBox7" runat="server" Text='<%# Bind("Dateofbirth") %>'></asp:TextBox>
                                </EditItemTemplate>
                                <ItemTemplate>
                                    <asp:Label ID="Label7" runat="server" Text='<%# Bind("Dateofbirth") %>'></asp:Label>
                                </ItemTemplate>
                            </asp:TemplateField>
                        </Columns>
                        <FooterStyle BackColor="#CCCCCC" />
                        <HeaderStyle BackColor="Black" Font-Bold="True" ForeColor="White" />
                        <PagerStyle BackColor="#CCCCCC" ForeColor="Black" HorizontalAlign="Left" />
                        <RowStyle BackColor="White" />
                        <SelectedRowStyle BackColor="#000099" Font-Bold="True" ForeColor="White" />
                        <SortedAscendingCellStyle BackColor="#F1F1F1" />
                        <SortedAscendingHeaderStyle BackColor="#808080" />
                        <SortedDescendingCellStyle BackColor="#CAC9C9" />
                        <SortedDescendingHeaderStyle BackColor="#383838" />
                    </asp:GridView>
                </td>
                <td> </td>
            </tr>
            <tr>
                <td class="auto-style6"></td>
                <td class="auto-style7"></td>
                <td class="auto-style8"></td>
            </tr>
        </table>
    
    </div>
    </form>
</body>
</html>
Posted

1 solution

Hi,

From your codes I can see that your LINQ select statement can return you read-only object collection in an anoymous class collection, So this has nothing to do with original databases. Maybe in my mind I think you can use EntityFramework DataSource to bind to the GridView, and then do modifications.

In your query you are not updating the values before saving the changes. Use the following line before you submit the changes.

Db.Registers.InsertOnSubmit(reg);


Thanks
Sisir Patro
 
Share this answer
 
Comments
Parth Mashroo 30-Dec-14 2:44am    
i have changed that but i have one more problem if i want to change Name field in gridview then then that name isnt getting selected but id is getting correct in the first line of id pls help me on that!!
[no name] 30-Dec-14 4:00am    
I could not follow please explain more.
Parth Mashroo 30-Dec-14 5:08am    
you see by writing that update command particular row dosent get selected pls see what error i am making??
[no name] 30-Dec-14 5:34am    
Here your code "int id = (int)GridView1.DataKeys[e.RowIndex].Value;" will give you the row index not the Id value that you are binding with the grid. Fetch the Id value first and then use that Id there in the where statement and you will get the exact value.
Parth Mashroo 30-Dec-14 6:00am    
sir can you elaborate by changing in my code??
u see sir if i want to update name field then that is not getting update it returns only the same value as it was before so help!!
i have tried using break point it only shows same value..

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900