473,378 Members | 1,500 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

gridview editing

Hi everyone

I’m using the GridView in asp.net and populating the grid dynamically using c#.net.To edit the rows i i m using GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e).
Now if I run the application and click the “Edit” Button then the complete row is generating the textboxes to edit the data in that particular row and also the “Update” and “Cancel” buttons are generated.When I change the data in any of the textboxes in that row then how should I Update that data to the Database(Sql server) using stored procedure . and display the updated data in the grid.Plzzz provide the code for this and also Is the same thing applies to the delete button.Plzzzzz help..
i m sending my code plz tell me where is d error..

gridview.aspx
Expand|Select|Wrap|Line Numbers
  1. <asp:GridView ID="GridView1" AutoGenerateColumns="false" runat="server" OnRowEditing="GridView1_RowEditing"
  2. OnRowUpdating="GridView1_RowUpdating" >
  3.         <Columns>           
  4.          <asp:TemplateField HeaderText="userid">
  5.             <ItemTemplate>
  6.               <asp:Label runat="server" ID="uid" Text='<%# DataBinder.Eval(Container.DataItem,"uid") %>'/>
  7.             </ItemTemplate>
  8.             <EditItemTemplate>
  9.            <asp:TextBox runat="server" ID="Textuid" Text='<%# DataBinder.Eval(Container.DataItem,"uid") %>' EnableViewState="true" />
  10.             </EditItemTemplate>
  11.  
  12.          </asp:TemplateField>
  13.         <asp:TemplateField HeaderText="password">
  14.             <ItemTemplate>
  15.               <asp:Label runat="server" ID="pwd" Text='<%# DataBinder.Eval(Container.DataItem,"pwd") %>' />
  16.             </ItemTemplate>
  17.             <EditItemTemplate>
  18.             <asp:TextBox runat="server" ID="txtpwd"  Text='<%# DataBinder.Eval(Container.DataItem,"pwd") %>' EnableViewState="true"/>
  19.             </EditItemTemplate>
  20.          </asp:TemplateField>
  21.       <asp:TemplateField HeaderText="Action">
  22.       <ItemTemplate>
  23.       <asp:LinkButton ID="btnEdit" Text="Edit" runat="server" CommandName="Edit" />
  24.      <br />
  25.       <asp:LinkButton ID="btnDelete" Text="Delete" runat="server" CommandName="Delete" />
  26.       </ItemTemplate>
  27.       <EditItemTemplate>
  28.       <asp:LinkButton ID="btnUpdate" Text="Update" runat="server" CommandName="Update"  />
  29.       <asp:LinkButton ID="btnCancel" Text="Cancel" runat="server" CommandName="Cancel" />
  30.       </EditItemTemplate>
  31.       </asp:TemplateField>
  32.   </Columns>
  33.         </asp:GridView>
stored procedure
Expand|Select|Wrap|Line Numbers
  1. Editdata
  2.  
  3. ALTER PROCEDURE [dbo].[EditData]
  4.  
  5. @name as varchar(50)    ,
  6. @password as varchar(20)
  7.  
  8. AS
  9. BEGIN
  10.  
  11.     SET NOCOUNT ON;
  12. update [dbo].[Login] set  [pwd]= @password where [uid]=@name
  13. END
  14.  

gridview.aspx.cs

Expand|Select|Wrap|Line Numbers
  1. public partial class gridview : System.Web.UI.Page
  2. {
  3.     dbconn obj = new dbconn();
  4.     protected void Page_Load(object sender, EventArgs e)
  5.     {
  6.  
  7.         bindGrid();
  8.     }
  9.     public static DataTable  GetAllData()
  10.     {
  11.         DataTable alldata=new DataTable();
  12.         string dbcon = ConfigurationManager.AppSettings["connectioninfo"];
  13.         SqlConnection con = new SqlConnection(dbcon);
  14.         con.Open();
  15.         SqlCommand cmd = new SqlCommand("GetData",con);
  16.         cmd.CommandType = CommandType.StoredProcedure;
  17.         cmd.ExecuteNonQuery();
  18.         SqlDataAdapter da = new SqlDataAdapter(cmd);
  19.         da.Fill(alldata);
  20.         return alldata;
  21.     }
  22.     protected void bindGrid()
  23.     {
  24.  
  25.  
  26.         GridView1.DataSource = GetAllData();
  27.         GridView1.DataBind();
  28.     }
  29.     protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
  30.     {
  31.  
  32.         GridView1.EditIndex = e.NewEditIndex;
  33.  
  34.         bindGrid();
  35.  
  36.     }
  37.     protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
  38.  
  39. {
  40.  TextBox uidtext = GridView1.Rows[e.RowIndex].FindControl("Textuid") as TextBox;
  41.     TextBox pwdtext = GridView1.Rows[e.RowIndex].FindControl("txtpwd") as TextBox;
  42. string dbcon = ConfigurationManager.AppSettings["connectioninfo"];
  43.  
  44.         SqlConnection con = new SqlConnection(dbcon);
  45.         con.Open();
  46.         SqlCommand cmd = new SqlCommand("EditData", con);
  47.         cmd.CommandType = CommandType.StoredProcedure;
  48.         cmd.Parameters.AddWithValue("@name", uidtext.Text);
  49.         cmd.Parameters.AddWithValue("@password", pwdtext.Text);
  50.         cmd.ExecuteNonQuery();
  51. }
we.config
Expand|Select|Wrap|Line Numbers
  1. public class dbconn
  2. {
  3.  
  4.         public  SqlConnection  GetConnection()
  5.         {
  6.             SqlConnection con = default(SqlConnection);
  7.             string connectioninfo = ConfigurationManager.AppSettings["connectionstring"];
  8.             con = new  SqlConnection(connectioninfo);
  9.             //if (con.State = ConnectionState.Closed)
  10.             //    con.Open();
  11.             return con;
  12.  
  13.  
  14.  
  15.         }
  16.     public DataSet ExecuteDataset(string sql)
  17.     {
  18.         SqlConnection conn = GetConnection();
  19.         if (conn.State== ConnectionState.Closed)
  20.             conn.Open();
  21.         DataSet ds=new DataSet();
  22.  
  23.         SqlDataAdapter da = new SqlDataAdapter(sql,conn);
  24.         da.Fill(ds);
  25.         return (ds);
  26.  
  27.  
  28.     }
  29.     public void ExecuteCommand(string sql)
  30.     {
  31.         SqlConnection con = GetConnection();
  32.         if (con.State == ConnectionState.Closed)
  33.             con.Open();
  34.         SqlCommand cmd = new SqlCommand();
  35.         cmd.ExecuteNonQuery();
  36.  
  37.     }
  38. }
Aug 24 '09 #1
5 3650
tlhintoq
3,525 Expert 2GB
TIP: When you are writing your question, there is a button on the tool bar that wraps the [code] tags around your copy/pasted code. It helps a bunch. Its the button with a '#' on it. More on tags. They're cool. Check'em out.
Aug 24 '09 #2
tlhintoq
3,525 Expert 2GB
Please read the guidelines about posting a question. The use of SMS speak and unnecessary abbreviations just makes it hard to read your question. Personally when I see a bunch of this my thinking is "If it's not important enough for the poster to type the extra few characters to make a real word, then it must not be very important." But that's just me and I don't claim to have insight on how the other volunteers here think.

i m sending.......... I am sending.
Plzzzzz........ Actually longer than "Please"
where is d error............ Just makes you sound like a wannabe rapper
Aug 24 '09 #3
Frinavale
9,735 Expert Mod 8TB
Do not call the bindGrid() method in your page load without checking if it ispostback = false (the first time the page is loading).

Instead, move this method to the Page PreRender event.

What's happening is that you're binding the GridView to a "new" datasource every time the page posts back to the server.

When you bind the GridView to the new datasource when you're trying to Edit something, all of the values entered by the user (during editing) will be lost....

If you moved this function call to the PreRender event instead, then the grid will be bound to the new datasource (updated datasource) after the Edit has occurred (whereas if you do this in the PageLoad event it occurs before the edit does) and everything will work nicely....
Aug 27 '09 #4
i hve moved bindgrid method in Page_PreRender event bt the prob arising is that text boxes are containing null value instead of updated values.so update method is not having any effect.
Sep 2 '09 #5
Frinavale
9,735 Expert Mod 8TB
This should not happen if you are no longer calling the DataBind method on the GridView before the Update event.

Post your updated code so that I can see what you're doing now.
Sep 2 '09 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: j.zascinski | last post by:
Hi, i have a "simple" problem with gridview, please help me :) i want to have gridview which is binded to a datatable (or a dataset). i can show the data from the dataset in the gridview and i can...
4
by: P. Yanzick | last post by:
Hello, I've been playing with master/detail views as well as editing in the gridview, and I ran across a strange problem that I am not exactly sure where to go to try to solve. I have 2...
1
by: Kyle K. | last post by:
I would like to build my data pages such that the top of the page contains a GridView showing the data with 'Enable Select = true'. Below that I would like to have a FormView, that by default is...
4
by: Tomasz Jastrzebski | last post by:
Hello Everyone, I have a GridView control bound to a plain DataTable object. AutoGenerateEditButton is set to true, Edit button gets displayed, and RowEditing event fires as expected.
3
by: cpnet | last post by:
I have a GridView which I'm populating from an ObjectDataSource (give the GridView a DataTable). The GridView will have about 20 rows, and only one editable column. The editable column consists...
1
by: =?Utf-8?B?Q2hyaXM=?= | last post by:
Hi, I have a gridview which I added a <asp:CommandField EditText="E" CancelText="C" UpdateText="U" ButtonType="Link" ShowEditButton="True" /> my gridview looks like this <asp:GridView...
1
by: pieandpeas | last post by:
he selectedindex of my gridview is always one behind for example, if i click on the EDIT.gif on row 1, then 5 , then 7, if i break the code and check the selectedindex, it is equal to 5. if i...
5
by: =?Utf-8?B?Z3V5?= | last post by:
How do you enable editing in a GridView programatically rather than via its Tasks menu? Guy
2
by: Michael | last post by:
It seems that a gridview allows us to delete only a single row at a time. How to extend this functionality to select multiple rows and delete all of the selected rows in a single stroke? just like...
11
by: SAL | last post by:
Hello, I have a Gridview control (.net 2.0) that I'm having trouble getting the Update button to fire any kind of event or preforming the update. The datatable is based on a join so I don't know...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.