IT Community - Software Programming, Web Development and Technical Support

Edit grid

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 ...


Go Back   IT Community - Software Programming, Web Development and Technical Support > Web Development > ASP and ASP.NET Programming

Register FAQ Members List Calendar Mark Forums Read
  #1 (permalink)  
Old 03-25-2008, 10:08 PM
Kirubhananth Kirubhananth is offline
D-Web Programmer
 
Join Date: Feb 2008
Location: My native is Madurai but now in Chennai
Posts: 61
Kirubhananth is on a distinguished road
Send a message via AIM to Kirubhananth Send a message via Skype™ to Kirubhananth
Question Edit grid

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>
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 03-26-2008, 12:22 AM
poornima poornima is offline
D-Web Sr.Programmer
 
Join Date: Dec 2007
Posts: 189
poornima is on a distinguished road
Smile Re: Edit grid

Hai Kirubananth,
See the Field name 'RegNo'..
Is it same as the field in DB?
Is it RegNo or Regno?Check and Correct it
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 03-26-2008, 01:07 AM
poornima poornima is offline
D-Web Sr.Programmer
 
Join Date: Dec 2007
Posts: 189
poornima is on a distinguished road
Smile Re: Edit grid

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...
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 03-26-2008, 02:08 AM
Kirubhananth Kirubhananth is offline
D-Web Programmer
 
Join Date: Feb 2008
Location: My native is Madurai but now in Chennai
Posts: 61
Kirubhananth is on a distinguished road
Send a message via AIM to Kirubhananth Send a message via Skype™ to Kirubhananth
Smile Re: Edit grid

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--
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 03-26-2008, 04:35 AM
poornima poornima is offline
D-Web Sr.Programmer
 
Join Date: Dec 2007
Posts: 189
poornima is on a distinguished road
Smile Re: Edit grid

Hi,
Have u checked for Select Command query to select particular row?
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #6 (permalink)  
Old 03-26-2008, 05:03 AM
Kirubhananth Kirubhananth is offline
D-Web Programmer
 
Join Date: Feb 2008
Location: My native is Madurai but now in Chennai
Posts: 61
Kirubhananth is on a distinguished road
Send a message via AIM to Kirubhananth Send a message via Skype™ to Kirubhananth
Smile Re: Edit grid

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--
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #7 (permalink)  
Old 03-28-2008, 04:45 AM
poornima poornima is offline
D-Web Sr.Programmer
 
Join Date: Dec 2007
Posts: 189
poornima is on a distinguished road
Smile Re: Edit grid

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...
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #8 (permalink)  
Old 11-14-2008, 05:08 AM
levelup2 levelup2 is offline
D-Web Trainee
 
Join Date: Nov 2008
Posts: 8
levelup2 is on a distinguished road
Default Re: Edit grid

I am glad to read it here.
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


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


All times are GMT -7. The time now is 07:34 AM.


Copyright ©2004 - 2007, DiscussWeb. All Rights Reserved.

SEO by vBSEO 3.0.0