This is a discussion on Edit grid within the ASP and ASP.NET Programming forums, part of the Web Development category; Hi , I tried to edit a DB that was bounded to the grid. The DB consists of three fields as ...
| |||||||
| Register | FAQ | Members List | Calendar | Mark Forums Read |
| |||
| Hi , I tried to edit a DB that was bounded to the grid. The DB consists of three fields as RegNo,Name,Age. I tried to edit and delete it through the <Templetfield> . The delete command is working perfectely. But the editing is not. The row that was edited with a new value is appended with null string in the DB. The value was not assigned to the parameters rightly. The source code is here. Please help me to solve the problem <%--The grid named gridView1--%> <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="RegNo" DataSourceID="SqlDataSource1" CellPadding="4" ForeColor="#333333" GridLines="None"> <Columns> <%--Templetefield for column Regno--%> <asp:TemplateField HeaderText="RegNo"> <ItemTemplate> <asp:Label ID="lblregno" runat="server" Text='<%#Eval("RegNo") %>'></asp:Label> </ItemTemplate> </asp:TemplateField> <%--Templetefield for the column Name--%> <asp:TemplateField HeaderText="Name"> <ItemTemplate> <asp:Label ID="lblname" runat="server" Text='<%#Eval("Name") %>'></asp:Label> </ItemTemplate> <EditItemTemplate> <asp:textbox Width="50px" ID="txtname" runat="server" text='<%# Eval("Name") %>'></asp:textbox> </EditItemTemplate> </asp:TemplateField> <%--Templetefield for the column Age--%> <asp:TemplateField HeaderText="Age"> <ItemTemplate> <asp:Label ID="lblage" runat="server" Text='<%#Eval("Age") %>'></asp:Label> </ItemTemplate> <EditItemTemplate> <asp:textbox Width="50px" ID="txtage" runat="server" text='<%# Eval("Age") %>'></asp:textbox> </EditItemTemplate> </asp:TemplateField> <asp:CommandField ShowEditButton="True" ShowDeleteButton="True" /> </Columns> <FooterStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" /> <RowStyle BackColor="#E3EAEB" /> <EditRowStyle BackColor="#7C6F57" /> <SelectedRowStyle BackColor="#C5BBAF" Font-Bold="True" ForeColor="#333333" /> <PagerStyle BackColor="#666666" ForeColor="White" HorizontalAlign="Center" /> <HeaderStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" /> <AlternatingRowStyle BackColor="White" /> </asp:GridView> <%--The DataSource area with the edit and delete command--%> <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:testConnectionString %>" SelectCommand="SELECT [RegNo], [Name], [Age] FROM [personal]" UpdateCommand="UPDATE personal SET Name = @Name, Age = @Age WHERE RegNo = @RegNo" DeleteCommand="Delete from personal where Regno =@Regno" > <%--The parameters area--%> <UpdateParameters> <asp:Parameter Name="Name" DefaultValue="jj"/> <asp:Parameter Name="Age" DefaultValue="56"/> </UpdateParameters> <DeleteParameters> <asp:Parameter Name="RegNo" /> </DeleteParameters> </asp:SqlDataSource> |
| Sponsored Links |
| |||
| Hi Kirubananth, Another thing,In select Command r u selecting all rows.U r going to edit a single row.For that u need to be select that particular row.In Select Command u have selected all rows.Then how u can able to edit that particular record...Check this...This may be reason for ur problem i think so... |
| |||
| Hi poornima, The field names are [RegNo], [Name], [Age]. I put the same names for the parameter names as RegNo, Names, Age. Eventhough i changed these names it is not functioning. But the delete option is working in any situation.
__________________ --Kirubhaa. Born to win-- |
| |||
| Hi, Without the select ommand the grid was not displayed by the browser. The select command is just to display the grid.
__________________ --Kirubhaa. Born to win-- |
| |||
| Hi Kirubananth, Try Like this. In aspx Page, <asp:GridView ID="GridViewDemo" OnRowCancelingEdit="GridViewDemo_RowCancelEdit" OnRowUpdating="GridViewDemo_RowUpdating" OnRowEditing="GridViewDemo_RowEditing" runat="server" PageSize ="5" OnPageIndexChanging="GridViewDemo_PageIndexChangin g" AutoGenerateColumns="false" AllowPaging="true" DataKeyNames="Sno" > <Columns> <asp:TemplateField HeaderText="Sno"> <ItemTemplate><%#DataBinder.Eval(Container.DataIte m,"sno")%></ItemTemplate> </asp:TemplateField> </Columns> <Columns> <asp:TemplateField HeaderText="Sname"> <ItemTemplate><%#DataBinder.Eval(Container.DataIte m,"sname")%></ItemTemplate> <EditItemTemplate> <asp:TextBox ID="nameTextBox" runat="server" Text='<%#DataBinder.Eval(Container.DataItem,"sname ")%>'></asp:TextBox> </EditItemTemplate> </asp:TemplateField> <asp:CommandField ShowEditButton="true" /> </Columns> </asp:GridView> In Code-Behind, SqlConnection con=new SqlConnection("ConnectionString"); Sqlcommand cmd; protected void Page_Load(object sender, EventArgs e) { if(!IsPostBack) loadData(); } public void loadData() { cmd = new SqlCommand("select * from stud", con); SqlDataAdapter sda = new SqlDataAdapter(cmd); DataSet ds = new DataSet(); sda.Fill(ds, "stud"); GridViewDemo.DataSource = ds; GridViewDemo.DataBind(); } protected void GridViewDemo_PageIndexChanging(object sender, GridViewPageEventArgs e) { GridViewDemo.PageIndex = e.NewPageIndex; loadData(); } protected void GridViewDemo_RowEditing(object sender, GridViewEditEventArgs e) { GridViewDemo.EditIndex = e.NewEditIndex; loadData(); } protected void GridViewDemo_RowCancelEdit(object sender, GridViewCancelEditEventArgs e) { GridViewDemo.EditIndex = -1; loadData(); } protected void GridViewDemo_RowUpdating(object sender,GridViewUpdateEventArgs e) { //int sno=int.Parse(GridViewDemo.DataKeys[e.RowIndex].Value.ToString()); GridViewRow row = GridViewDemo.Rows[e.RowIndex]; int sno = (int)((DataKey)GridViewDemo.DataKeys[e.RowIndex]).Value; TextBox nameTextBox = (TextBox)row.FindControl("nameTextBox"); if (nameTextBox != null) { cmd = new SqlCommand("update stud set sname=@sname where sno=@sno", con); cmd.Parameters.Add("@sname", SqlDbType.VarChar); cmd.Parameters.Add("@sno", SqlDbType.Int); cmd.Parameters["@sname"].Value = nameTextBox.Text; cmd.Parameters["@sno"].Value = sno; con.Open(); cmd.ExecuteNonQuery(); con.Close(); } GridViewDemo.EditIndex = -1; loadData(); } Its working fine... |
| |||
| I am glad to read it here. |
![]() |
| Thread Tools | |
| Display Modes | |
| |
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Paging in Grid | Kirubhananth | ASP and ASP.NET Programming | 8 | 11-28-2008 01:51 AM |
| C#.Net, ASP.Net Data Grid Frequently Asked Questions | a.deeban | C# Programming | 51 | 02-27-2008 08:49 AM |
| Grid View Control in asp.net | krishnakumar | ASP and ASP.NET Programming | 3 | 09-27-2007 03:18 AM |
| How to edit grid view control in asp.net using c#? | bluesky | ASP and ASP.NET Programming | 1 | 07-25-2007 05:54 AM |
| Data grid in PHP | jegan | PHP Programming | 2 | 07-20-2007 07:38 AM |