473,326 Members | 2,126 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,326 software developers and data experts.

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 4446
Frinavale
9,735 Expert Mod 8TB
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 Expert Mod 8TB
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.after 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 Expert Mod 8TB
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 Expert Mod 8TB
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
MMcCarthy
14,534 Expert Mod 8TB
@bala

It seems you set your last reply to frinny as the best answer in error. I have reset it to allow you to correctly set which post you consider to be the best answer.

Mary
Aug 4 '10 #11
ok,it is better check the condition. Thanq
Dec 28 '10 #12

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

Similar topics

2
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...
1
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"...
0
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. ...
1
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...
0
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...
2
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...
1
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...
1
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...
0
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...
8
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...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
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...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.