473,779 Members | 1,905 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

gridview editing

2 New Member
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_RowUp dating(object sender, GridViewUpdateE ventArgs 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.c s

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 3677
tlhintoq
3,525 Recognized Expert Specialist
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 Recognized Expert Specialist
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 Recognized Expert Moderator Expert
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
sandhyascs
2 New Member
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 Recognized Expert Moderator Expert
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
9192
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 start editing the data, but when i click update i loose my new values. I want this values to be saved in datatable (not straight to database with sqldatasource - i tried this and it worked great), i tried to do this task with objectdatasource...
4
4493
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 tables, a table of cars (pretty basic, an ID, a description, and a Color ID) and a table of colors (Color ID, and a color description). I've added a gridview and a detailsview as I'm playing with both and how to get editing features to work the way I...
1
2713
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 in 'Insert' mode for creating a new record. If the user 'selects' a record in the GridView above, I would like to set the FormView to 'Edit' mode with the 'selected' record data loaded in it for editing. I would use the built-in editing...
4
26707
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
13408
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 of a RadioButtonList ("Yes", "No", "Not Answered") in a TemplateColumn. I want the user to be able to select a radio button for each row in the GridView (without having to first put each row into edit mode). Once the user has selected the Radio...
1
4701
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 ID="GridView1" runat="server" OnRowEditing="RowEdit" OnRowCancelingEdit="RowCancel" OnRowUpdating="RowUpdating"...
1
1805
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 selected a different row, and then checked the selectedindex again, it would be equal to 7. i've checked a few posts and other people seem to have a problem with the selectedindex being one behind the current selection, anyway i'm stuck :-( here is my...
5
5140
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
5339
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 what hotmail web UI is doing now (having the option of selecting multiple rows (using the checkbox provided) and perform a set of operations on them)
11
6068
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 if that's what's causing the behavior or not. It seems like the Update button should at least do something. When the Edit button is clicked, the grid goes into Edit mode and the Cancel button takes the grid out of Edit mode. So, I don't get what...
0
9632
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9471
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10302
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10136
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9925
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7478
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6723
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
4036
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2867
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.