473,944 Members | 2,982 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

problem while clicking edit button in gridview

I am facing a problem while clicking edit button of a gridview.after clicking edit button it is displaying empty textboxes. I need old values in that textboxes.

Here i used 3-tier architecture. Below is my code:
Expand|Select|Wrap|Line Numbers
  1.             /***Application layer***/
  2. protected void GridView2_RowEditing(object sender, GridViewEditEventArgs e)
  3.     {
  4.  
  5.         GridView2.EditIndex = e.NewEditIndex;             
  6.         GridView2.DataSource = rigobj.getgvdata();
  7.         GridView2.DataBind();
  8.     }
  9.  
  10.             /***Business layer***/
  11. public DataTable getgvdata()
  12.         {
  13.             return sdb.ds.Tables["rig_mstr_tab"];
  14.         }
  15.  
  16.            /***Data layer***/
  17. public shoredatabase()
  18.         {
  19.  da = new SqlDataAdapter("select * from rig_mstr_tab", con);
  20. da.Fill(ds, "rig_mstr_tab");
  21.             da.FillSchema(ds, SchemaType.Source, "rig_mstr_tab");
  22.             bldr = new SqlCommandBuilder(da);
  23.          }
Jun 1 '10 #1
11 4490
Frinavale
9,735 Recognized Expert Moderator Expert
I cannot open the attachment you posted. Please post the method that handles the GridView's RowEditing event.
Jun 1 '10 #2
@Frinavale
Hi Frinavale.. Thanks for your response.can you pls tell me the solution how to do this..now i am attached the file which in (.txt) format..in that file i kept GridView's RowEditing event method.once again thank u very much..we are waiting your reply....bye
Jun 2 '10 #3
Frinavale
9,735 Recognized Expert Moderator Expert
I cannot open attachments.

Just post the relevant code to your problem in the thread so that I can see it :)
Jun 2 '10 #4
@bala venkata siva ram kum
Hi kub thank u for editing my post.This time i will read the posting guidelines while i am posting question.But i didn't get answer for my question.what should i do.please tell me the solution.
Jun 3 '10 #5
Hi Frinavale i am sending my code.Here i used 3-tier architecture.af ter clicking edit button of a Gridview it is displaying empty textboxes.i need old values in those textboxes please tell me the solution.

Expand|Select|Wrap|Line Numbers
  1.  
  2. <asp:GridView ID="GridView2" runat="server" AutoGenerateColumns="False" 
  3.             AutoGenerateDeleteButton="True" AutoGenerateEditButton="True" 
  4.             OnRowDeleting="GridView2_RowDeleting" OnRowEditing="GridView2_RowEditing" 
  5.             OnRowCancelingEdit="GridView2_RowCancelingEdit" 
  6.             OnRowUpdating="GridView2_RowUpdating">
  7.             <Columns>
  8.                 <asp:TemplateField HeaderText="SupplierId">
  9.                     <ItemTemplate>
  10.                         <asp:Label ID="Label1" runat="server" Text='<%# Bind("sup_id") %>'></asp:Label>
  11.                     </ItemTemplate>
  12.                 </asp:TemplateField>
  13.                 <asp:TemplateField HeaderText="SupplierName">
  14.                     <EditItemTemplate>
  15.                         <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
  16.                     </EditItemTemplate>
  17.                     <ItemTemplate>
  18.                         <asp:Label ID="Label2" runat="server" Text='<%# Bind("sup_name") %>'></asp:Label>
  19.                     </ItemTemplate>
  20.                 </asp:TemplateField>
  21.                 <asp:TemplateField HeaderText="ProductName">
  22.                     <EditItemTemplate>
  23.                         <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
  24.                     </EditItemTemplate>
  25.                     <ItemTemplate>
  26.                         <asp:Label ID="Label3" runat="server" Text='<%# Bind("prod_name") %>'></asp:Label>
  27.                     </ItemTemplate>
  28.                 </asp:TemplateField>
  29.             </Columns>
  30.         </asp:GridView>
/** c#: **/
Expand|Select|Wrap|Line Numbers
  1. using System;
  2. using System.Collections;
  3. using System.Configuration;
  4. using System.Data;
  5. using System.Linq;
  6. using System.Web;
  7. using System.Web.Security;
  8. using System.Web.UI;
  9. using System.Web.UI.HtmlControls;
  10. using System.Web.UI.WebControls;
  11. using System.Web.UI.WebControls.WebParts;
  12. using System.Xml.Linq;
  13. using System.Data.SqlClient;
  14. using System.Collections.Generic;
  15. using rigBLL;
  16. public partial class Default3 : System.Web.UI.Page
  17. {
  18.     sup_mstr_tab supobj = new sup_mstr_tab();
  19.     protected void Page_Load(object sender, EventArgs e)
  20.     {
  21.         if (IsPostBack == false)
  22.         {
  23.             GridView2.DataSource = supobj.getgvdata();
  24.             GridView2.DataBind();
  25.         }
  26.     }
  27.     protected void GridView2_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
  28.     {
  29.         GridView2.EditIndex = -1;
  30.         GridView2.DataSource = supobj.getgvdata();
  31.         GridView2.DataBind();
  32.     }
  33.     protected void GridView2_RowDeleting(object sender, GridViewDeleteEventArgs e)
  34.     {
  35.         Label lb = (Label)GridView2.Rows[e.RowIndex].FindControl("Label1");
  36.         supobj.spid =Int32.Parse(lb.Text);
  37.         supobj.deleterecord();
  38.         GridView2.DataSource = supobj.getgvdata();
  39.         GridView2.DataBind();
  40.     }
  41.     protected void GridView2_RowEditing(object sender, GridViewEditEventArgs e)
  42.     {
  43.         GridView2.EditIndex = e.NewEditIndex;
  44.         GridView2.DataSource = supobj.getgvdata();
  45.         GridView2.DataBind();
  46.     }
  47.     protected void GridView2_RowUpdating(object sender, GridViewUpdateEventArgs e)
  48.     {
  49.         Label lb = (Label)GridView2.Rows[e.RowIndex].FindControl("Label1");
  50.         TextBox tx1 = (TextBox)GridView2.Rows[e.RowIndex].FindControl("TextBox1");
  51.         TextBox tx2 = (TextBox)GridView2.Rows[e.RowIndex].FindControl("TextBox2");
  52.         supobj.spid = Int32.Parse(lb.Text);
  53.         supobj.spname= tx1.Text;
  54.         supobj.prname = tx2.Text;
  55.         supobj.updaterecord();
  56.         GridView2.EditIndex = -1;
  57.         GridView2.DataSource = supobj.getgvdata();
  58.         GridView2.DataBind();
  59.     }
  60. }
/**classlibrary file**/(Business layer)

Expand|Select|Wrap|Line Numbers
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Data;
  6. using clibDAL;
  7.  
  8. namespace rigBLL
  9. {
  10.     public class sup_mstr_tab
  11.     {
  12.         DataRow rec;
  13.         shoredatabase sdb = new shoredatabase();
  14.         int sid;
  15.         string  supname, productname;
  16.         public int spid
  17.         {
  18.             set { sid = value; }
  19.         }
  20.         public string spname
  21.         {
  22.             set { supname = value; }
  23.             get { return supname; }
  24.         }
  25.         public string prname
  26.         {
  27.             set { productname = value; }
  28.             get { return productname; }
  29.         }
  30.         public void insertrecord()
  31.         {
  32.            rec=sdb.ds.Tables["sup_mstr_tab"].NewRow();
  33.            rec[1] = supname;
  34.            rec[2] = productname;
  35.            sdb.ds.Tables["sup_mstr_tab"].Rows.Add(rec);
  36.            sdb.updateDB();
  37.  
  38.         }
  39.         public void deleterecord()
  40.         {
  41.             rec = sdb.ds.Tables["sup_mstr_tab"].Select("sup_id='" + sid + "'")[0];
  42.             rec.Delete();
  43.             sdb.updateDB();
  44.  
  45.         }
  46.         public void updaterecord()
  47.         {
  48.             rec = sdb.ds.Tables["sup_mstr_tab"].Select("sup_id='" + sid + "'")[0];
  49.             rec[1] = supname;
  50.             rec[2] = productname;
  51.             sdb.updateDB();
  52.         }
  53.         public DataTable getgvdata()
  54.         {
  55.             return sdb.ds.Tables["sup_mstr_tab"];
  56.         }
  57.  
  58.     }
  59. }
Expand|Select|Wrap|Line Numbers
  1.     /**class library file**/(data layer)
  2.  
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Data;
  8. using System.Data.SqlClient;
  9.  
  10. namespace clibDAL
  11. {
  12.     public class shoredatabase
  13.     {
  14.         SqlConnection con = new SqlConnection("Data Source=VC8;Initial Catalog=shore;Integrated Security=True");
  15.         SqlDataAdapter da,da1,da2,da3,da4,da5,da6;        
  16.         public DataSet ds;
  17.         SqlCommandBuilder bldr, bldr1, bldr2,bldr3,bldr4,bldr5;
  18.         public shoredatabase()
  19.         {
  20.  
  21.             da = new SqlDataAdapter("select * from rig_mstr_tab", con);
  22.             da1 = new SqlDataAdapter("select * from  ship_mstr_tab", con);
  23.             da2 = new SqlDataAdapter("select * from sup_mstr_tab", con);
  24.             da3 = new SqlDataAdapter("select * from imp_mstr_tab order by imp_type", con);
  25.             da4 = new SqlDataAdapter("select * from meas_mstr_tab", con);
  26.             da5 = new SqlDataAdapter("select *from bck_mstr_tab", con);
  27.             ds = new DataSet();
  28.  
  29.             // insert into rig master table
  30.             da.Fill(ds, "rig_mstr_tab");
  31.             da.FillSchema(ds, SchemaType.Source, "rig_mstr_tab");
  32.             bldr = new SqlCommandBuilder(da);
  33.  
  34.             //insert into ship master table
  35.             da1.Fill(ds, "ship_mstr_tab");
  36.             da1.FillSchema(ds, SchemaType.Source, "ship_mstr_tab");
  37.             bldr1 = new SqlCommandBuilder(da1);
  38.  
  39.             //insert into supplier master table
  40.             da2.Fill(ds, "sup_mstr_tab");
  41.             da2.FillSchema(ds, SchemaType.Source, "sup_mstr_tab");
  42.             bldr2 = new SqlCommandBuilder(da2);
  43.  
  44.             //insert into import master table
  45.             da3.Fill(ds, "imp_mstr_tab");
  46.             da3.FillSchema(ds, SchemaType.Source, "imp_mstr_tab");
  47.             bldr3 = new SqlCommandBuilder(da3);
  48.  
  49.             //insert into measurement master table
  50.             da4.Fill(ds, "meas_mstr_tab");
  51.             da4.FillSchema(ds, SchemaType.Source, "meas_mstr_tab");
  52.             bldr4 = new SqlCommandBuilder(da4);
  53.  
  54.             //insert into backload  master table
  55.             da5.Fill(ds, "bck_mstr_tab");
  56.             da5.FillSchema(ds, SchemaType.Source, "bck_mstr_tab");
  57.             bldr5 = new SqlCommandBuilder(da5);
  58.  
  59.  
  60.  
  61.         }
  62.         public void updateDB()
  63.         {
  64.             da.Update(ds, "rig_mstr_tab");
  65.             da1.Update(ds, "ship_mstr_tab");
  66.             da2.Update(ds, "sup_mstr_tab");
  67.             da3.Update(ds, "imp_mstr_tab");
  68.             da4.Update(ds, "meas_mstr_tab");
  69.             da5.Update(ds, "bck_mstr_tab");
  70.         }
  71.     }
  72.  
  73. }
  74.  
  75.  
Jun 3 '10 #6
Frinavale
9,735 Recognized Expert Moderator Expert
You see how you are biding the text to the Label in your code?

Do the same thing for the TextBoxes and it will work the way you want it to :)

-Frinny
Jun 3 '10 #7
Hi Frinny, sorry i went to chennai for the last three days.
i saw your reply and i tried as you told and some error is coming while assigning text to textboxes.I am sending that code.please send me the code how to assign into textboxes while editing gridview.
Expand|Select|Wrap|Line Numbers
  1. protected void GridView2_RowEditing(object sender, GridViewEditEventArgs e)
  2.     {
  3.         Label lb1 = (Label)GridView2.Rows[e.NewEditIndex].FindControl("Label2");
  4.         Label lb2 = (Label)GridView2.Rows[e.NewEditIndex].FindControl("Label3");
  5.  
  6.         GridView2.EditIndex = e.NewEditIndex;
  7.         (TextBox)GridView2.Rows[e.NewEditIndex].FindControl("TextBox1") = lb1.Text;//(It is showing error asThe left-hand side of an assignment must be a variable, property or indexer)
  8.         GridView2.DataSource = supobj.getgvdata();                                //(cannot implicitly convert type 'string' to 'system.web.UI.webcontrols.textbox')
  9.         GridView2.DataBind();
  10.     }
  11.  
Jun 7 '10 #8
Frinavale
9,735 Recognized Expert Moderator Expert
You are attempting to assign a String to a control that has been located in the GridView. This obviously doesn't make sense, you should be assigning the string to the Text property of the control.

Changing it to the following will work:
Expand|Select|Wrap|Line Numbers
  1. (TextBox)GridView2.Rows[e.NewEditIndex].FindControl("TextBox1").Text = lb1.Text;
But it would be better to check that the TextBox1 exists before attempting to use it:
Expand|Select|Wrap|Line Numbers
  1. if((TextBox)GridView2.Rows[e.NewEditIndex].FindControl("TextBox1") !=null){
  2.   (TextBox)GridView2.Rows[e.NewEditIndex].FindControl("TextBox1").Text = lb1.Text;
  3. }
-Frinny
Jun 7 '10 #9
@Frinavale
Hi Frinny,Good Morning thank q so much.you have solved my problem.
Jun 8 '10 #10

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

Similar topics

2
1488
by: Alex | last post by:
I have a datagrid with two additional columns (edit, delete). However the edit and delete handlers in the code behind page are never reached. When I click on the delete or edit button the load page handler gets executed nothing else. What could I do wrong? Here some code: private void InitializeComponent()
1
8632
by: Mike P | last post by:
How do you disable the edit button for selected rows in a gridview? Here is my button : <asp:CommandField ShowEditButton="True" ButtonType="Link" ShowCancelButton="True" UpdateText="Update" EditText="Edit" CancelText="Cancel" />
0
1343
by: Damien | last post by:
Hi, I have a added an edit button to my gridview and when i click it once the page refreshes but i have to click it again for the textbox to appear, this same problem as exists for the update. I am binding the gridview to a dataset at runtime. Any help would be appreciated Cheers D
1
1216
by: karups | last post by:
Hi I have binded a dataset to my datagrid. Datagrid is present inside a >DIV></DIVtag of specific height. Only 5 records will be visible at a time when i edit the 10th record, the page refreshes and the datagrid goes to its top position. I have to search for the edited item inside the div tag by scrolling and then edit it..
0
2182
by: Not Me | last post by:
Hi there, How come, when using default settings on a gridview which has a stored procedure as it's datasource's selectcommand, do I have to sort the stored procedure in order to have the edit button select the appropriate row? Surely it should know which row I clicked, and choose the right one based on more than the original order when selecting the records?
2
2555
by: ruthie | last post by:
Hi, I have a gridview with edit option. If the user chooses to edit the row and then press the Cancel button - I would like the focus to return to the chosen row. Someone has any idea how to implement it? Thanks. Regards,
1
1324
by: =?Utf-8?B?Ym9iYnk=?= | last post by:
Hi, I have GridView. I set AutoGenerateEditButton="True" . When I click this button it puts some fields in edit mode. But I want to add button field in grid View and When I click on that Edit Button It just make other columns in Edit Mode. Because i don't want After clicking the Edit button to be change to Update or Cancel.
1
1163
by: sejal17 | last post by:
Hello everyone, I have a problem with taking button in gridview and datalist.In datalist,i have one field to display naming card_price and one button naming BUY.I want to click that BUY button and take the card_price on that row in session.so tht clicking on that button i can go on other page with that card_price. Please help me on this. Thanx.....
0
2391
by: DJOuk | last post by:
Posted this on Asp.Net, seems to be a hard problem, I still can't work it out I have a edit button, this works fine, until I add the code that prevents the edit button from being seen by users who did not post that ticket. I cannot understand why this is happening. I get an error on the button says 'object reference not set in instance of object' It happens when I add the following code:
8
6254
by: skuer | last post by:
Hey people If I have made a page with login system, are there any possibilities to let a special group of people edit the page or some of the text on the page, just by clicking a edit button on the page? With that I mean those with a special ID? I have seen pages that have this "edit button", and that include a simple field with text where you can re-write the text, and then submit it. I have a little problem for where I should...
0
10143
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
11134
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
10671
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
8234
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
6090
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
6313
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4918
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
2
4516
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3518
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.