By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
458,005 Members | 1,226 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 458,005 IT Pros & Developers. It's quick & easy.

gridview editing

P: 2
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
Share this Question
Share on Google+
5 Replies


tlhintoq
Expert 2.5K+
P: 3,525
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
Expert 2.5K+
P: 3,525
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
Expert Mod 5K+
P: 9,731
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

P: 2
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
Expert Mod 5K+
P: 9,731
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

Post your reply

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