problem while clicking edit button in gridview

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • bala venkata siva ram kum
    New Member
    • May 2010
    • 15

    #1

    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:
    Code:
                /***Application layer***/
    protected void GridView2_RowEditing(object sender, GridViewEditEventArgs e)
        {
            
            GridView2.EditIndex = e.NewEditIndex;             
            GridView2.DataSource = rigobj.getgvdata();
            GridView2.DataBind();
        }
    
                /***Business layer***/
    public DataTable getgvdata()
            {
                return sdb.ds.Tables["rig_mstr_tab"];
            }
    
               /***Data layer***/
    public shoredatabase()
            {
     da = new SqlDataAdapter("select * from rig_mstr_tab", con);
    da.Fill(ds, "rig_mstr_tab");
                da.FillSchema(ds, SchemaType.Source, "rig_mstr_tab");
                bldr = new SqlCommandBuilder(da);
             }
    Last edited by Niheel; Jun 2 '10, 12:42 PM. Reason: Bala, I edited your post for you this time, but next time take the time to do it properly. Also, Don't double post.
  • Frinavale
    Recognized Expert Expert
    • Oct 2006
    • 9749

    #2
    I cannot open the attachment you posted. Please post the method that handles the GridView's RowEditing event.

    Comment

    • bala venkata siva ram kum
      New Member
      • May 2010
      • 15

      #3
      Originally posted by Frinavale
      I cannot open the attachment you posted. Please post the method that handles the GridView's RowEditing event.
      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

      Comment

      • Frinavale
        Recognized Expert Expert
        • Oct 2006
        • 9749

        #4
        I cannot open attachments.

        Just post the relevant code to your problem in the thread so that I can see it :)

        Comment

        • bala venkata siva ram kum
          New Member
          • May 2010
          • 15

          #5
          Originally posted by bala venkata siva ram kum
          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:
          Code:
                      /***Application layer***/
          protected void GridView2_RowEditing(object sender, GridViewEditEventArgs e)
              {
                  
                  GridView2.EditIndex = e.NewEditIndex;             
                  GridView2.DataSource = rigobj.getgvdata();
                  GridView2.DataBind();
              }
          
                      /***Business layer***/
          public DataTable getgvdata()
                  {
                      return sdb.ds.Tables["rig_mstr_tab"];
                  }
          
                     /***Data layer***/
          public shoredatabase()
                  {
           da = new SqlDataAdapter("select * from rig_mstr_tab", con);
          da.Fill(ds, "rig_mstr_tab");
                      da.FillSchema(ds, SchemaType.Source, "rig_mstr_tab");
                      bldr = new SqlCommandBuilder(da);
                   }
          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.

          Comment

          • bala venkata siva ram kum
            New Member
            • May 2010
            • 15

            #6
            problem while clicking edit button of a Gridview

            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.

            Code:
             
            <asp:GridView ID="GridView2" runat="server" AutoGenerateColumns="False" 
                        AutoGenerateDeleteButton="True" AutoGenerateEditButton="True" 
                        OnRowDeleting="GridView2_RowDeleting" OnRowEditing="GridView2_RowEditing" 
                        OnRowCancelingEdit="GridView2_RowCancelingEdit" 
                        OnRowUpdating="GridView2_RowUpdating">
                        <Columns>
                            <asp:TemplateField HeaderText="SupplierId">
                                <ItemTemplate>
                                    <asp:Label ID="Label1" runat="server" Text='<%# Bind("sup_id") %>'></asp:Label>
                                </ItemTemplate>
                            </asp:TemplateField>
                            <asp:TemplateField HeaderText="SupplierName">
                                <EditItemTemplate>
                                    <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
                                </EditItemTemplate>
                                <ItemTemplate>
                                    <asp:Label ID="Label2" runat="server" Text='<%# Bind("sup_name") %>'></asp:Label>
                                </ItemTemplate>
                            </asp:TemplateField>
                            <asp:TemplateField HeaderText="ProductName">
                                <EditItemTemplate>
                                    <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
                                </EditItemTemplate>
                                <ItemTemplate>
                                    <asp:Label ID="Label3" runat="server" Text='<%# Bind("prod_name") %>'></asp:Label>
                                </ItemTemplate>
                            </asp:TemplateField>
                        </Columns>
                    </asp:GridView>
            /** c#: **/
            Code:
            using System;
            using System.Collections;
            using System.Configuration;
            using System.Data;
            using System.Linq;
            using System.Web;
            using System.Web.Security;
            using System.Web.UI;
            using System.Web.UI.HtmlControls;
            using System.Web.UI.WebControls;
            using System.Web.UI.WebControls.WebParts;
            using System.Xml.Linq;
            using System.Data.SqlClient;
            using System.Collections.Generic;
            using rigBLL;
            public partial class Default3 : System.Web.UI.Page
            {
                sup_mstr_tab supobj = new sup_mstr_tab();
                protected void Page_Load(object sender, EventArgs e)
                {
                    if (IsPostBack == false)
                    {
                        GridView2.DataSource = supobj.getgvdata();
                        GridView2.DataBind();
                    }
                }
                protected void GridView2_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
                {
                    GridView2.EditIndex = -1;
                    GridView2.DataSource = supobj.getgvdata();
                    GridView2.DataBind();
                }
                protected void GridView2_RowDeleting(object sender, GridViewDeleteEventArgs e)
                {
                    Label lb = (Label)GridView2.Rows[e.RowIndex].FindControl("Label1");
                    supobj.spid =Int32.Parse(lb.Text);
                    supobj.deleterecord();
                    GridView2.DataSource = supobj.getgvdata();
                    GridView2.DataBind();
                }
                protected void GridView2_RowEditing(object sender, GridViewEditEventArgs e)
                {
                    GridView2.EditIndex = e.NewEditIndex;
                    GridView2.DataSource = supobj.getgvdata();
                    GridView2.DataBind();
                }
                protected void GridView2_RowUpdating(object sender, GridViewUpdateEventArgs e)
                {
                    Label lb = (Label)GridView2.Rows[e.RowIndex].FindControl("Label1");
                    TextBox tx1 = (TextBox)GridView2.Rows[e.RowIndex].FindControl("TextBox1");
                    TextBox tx2 = (TextBox)GridView2.Rows[e.RowIndex].FindControl("TextBox2");
                    supobj.spid = Int32.Parse(lb.Text);
                    supobj.spname= tx1.Text;
                    supobj.prname = tx2.Text;
                    supobj.updaterecord();
                    GridView2.EditIndex = -1;
                    GridView2.DataSource = supobj.getgvdata();
                    GridView2.DataBind();
                }
            }
            /**classlibrary file**/(Business layer)

            Code:
            using System;
            using System.Collections.Generic;
            using System.Linq;
            using System.Text;
            using System.Data;
            using clibDAL;
            
            namespace rigBLL
            {
                public class sup_mstr_tab
                {
                    DataRow rec;
                    shoredatabase sdb = new shoredatabase();
                    int sid;
                    string  supname, productname;
                    public int spid
                    {
                        set { sid = value; }
                    }
                    public string spname
                    {
                        set { supname = value; }
                        get { return supname; }
                    }
                    public string prname
                    {
                        set { productname = value; }
                        get { return productname; }
                    }
                    public void insertrecord()
                    {
                       rec=sdb.ds.Tables["sup_mstr_tab"].NewRow();
                       rec[1] = supname;
                       rec[2] = productname;
                       sdb.ds.Tables["sup_mstr_tab"].Rows.Add(rec);
                       sdb.updateDB();
            
                    }
                    public void deleterecord()
                    {
                        rec = sdb.ds.Tables["sup_mstr_tab"].Select("sup_id='" + sid + "'")[0];
                        rec.Delete();
                        sdb.updateDB();
            
                    }
                    public void updaterecord()
                    {
                        rec = sdb.ds.Tables["sup_mstr_tab"].Select("sup_id='" + sid + "'")[0];
                        rec[1] = supname;
                        rec[2] = productname;
                        sdb.updateDB();
                    }
                    public DataTable getgvdata()
                    {
                        return sdb.ds.Tables["sup_mstr_tab"];
                    }
            
                }
            }
            Code:
                [B]/**class library file**/(data layer)[/B]
            
            using System;
            using System.Collections.Generic;
            using System.Linq;
            using System.Text;
            using System.Data;
            using System.Data.SqlClient;
            
            namespace clibDAL
            {
                public class shoredatabase
                {
                    SqlConnection con = new SqlConnection("Data Source=VC8;Initial Catalog=shore;Integrated Security=True");
                    SqlDataAdapter da,da1,da2,da3,da4,da5,da6;        
                    public DataSet ds;
                    SqlCommandBuilder bldr, bldr1, bldr2,bldr3,bldr4,bldr5;
                    public shoredatabase()
                    {
            
                        da = new SqlDataAdapter("select * from rig_mstr_tab", con);
                        da1 = new SqlDataAdapter("select * from  ship_mstr_tab", con);
                        da2 = new SqlDataAdapter("select * from sup_mstr_tab", con);
                        da3 = new SqlDataAdapter("select * from imp_mstr_tab order by imp_type", con);
                        da4 = new SqlDataAdapter("select * from meas_mstr_tab", con);
                        da5 = new SqlDataAdapter("select *from bck_mstr_tab", con);
                        ds = new DataSet();
            
                        // insert into rig master table
                        da.Fill(ds, "rig_mstr_tab");
                        da.FillSchema(ds, SchemaType.Source, "rig_mstr_tab");
                        bldr = new SqlCommandBuilder(da);
            
                        //insert into ship master table
                        da1.Fill(ds, "ship_mstr_tab");
                        da1.FillSchema(ds, SchemaType.Source, "ship_mstr_tab");
                        bldr1 = new SqlCommandBuilder(da1);
            
                        //insert into supplier master table
                        da2.Fill(ds, "sup_mstr_tab");
                        da2.FillSchema(ds, SchemaType.Source, "sup_mstr_tab");
                        bldr2 = new SqlCommandBuilder(da2);
            
                        //insert into import master table
                        da3.Fill(ds, "imp_mstr_tab");
                        da3.FillSchema(ds, SchemaType.Source, "imp_mstr_tab");
                        bldr3 = new SqlCommandBuilder(da3);
            
                        //insert into measurement master table
                        da4.Fill(ds, "meas_mstr_tab");
                        da4.FillSchema(ds, SchemaType.Source, "meas_mstr_tab");
                        bldr4 = new SqlCommandBuilder(da4);
            
                        //insert into backload  master table
                        da5.Fill(ds, "bck_mstr_tab");
                        da5.FillSchema(ds, SchemaType.Source, "bck_mstr_tab");
                        bldr5 = new SqlCommandBuilder(da5);
            
            
            
                    }
                    public void updateDB()
                    {
                        da.Update(ds, "rig_mstr_tab");
                        da1.Update(ds, "ship_mstr_tab");
                        da2.Update(ds, "sup_mstr_tab");
                        da3.Update(ds, "imp_mstr_tab");
                        da4.Update(ds, "meas_mstr_tab");
                        da5.Update(ds, "bck_mstr_tab");
                    }
                }
                
            }
            Last edited by Frinavale; Jun 7 '10, 01:03 PM. Reason: Fixed code tags to show the different sections of code.

            Comment

            • Frinavale
              Recognized Expert Expert
              • Oct 2006
              • 9749

              #7
              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

              Comment

              • bala venkata siva ram kum
                New Member
                • May 2010
                • 15

                #8
                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.
                Code:
                protected void GridView2_RowEditing(object sender, GridViewEditEventArgs e)
                    {
                        Label lb1 = (Label)GridView2.Rows[e.NewEditIndex].FindControl("Label2");
                        Label lb2 = (Label)GridView2.Rows[e.NewEditIndex].FindControl("Label3");
                       
                        GridView2.EditIndex = e.NewEditIndex;
                        (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)
                        GridView2.DataSource = supobj.getgvdata();                                //(cannot implicitly convert type 'string' to 'system.web.UI.webcontrols.textbox')
                        GridView2.DataBind();
                    }

                Comment

                • Frinavale
                  Recognized Expert Expert
                  • Oct 2006
                  • 9749

                  #9
                  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:
                  Code:
                  (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:
                  Code:
                  if((TextBox)GridView2.Rows[e.NewEditIndex].FindControl("TextBox1") !=null){
                    (TextBox)GridView2.Rows[e.NewEditIndex].FindControl("TextBox1").Text = lb1.Text;
                  }
                  -Frinny

                  Comment

                  • bala venkata siva ram kum
                    New Member
                    • May 2010
                    • 15

                    #10
                    Originally posted by Frinavale
                    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:
                    Code:
                    (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:
                    Code:
                    if((TextBox)GridView2.Rows[e.NewEditIndex].FindControl("TextBox1") !=null){
                      (TextBox)GridView2.Rows[e.NewEditIndex].FindControl("TextBox1").Text = lb1.Text;
                    }
                    -Frinny
                    Hi Frinny,Good Morning thank q so much.you have solved my problem.

                    Comment

                    • MMcCarthy
                      Recognized Expert MVP
                      • Aug 2006
                      • 14387

                      #11
                      @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

                      Comment

                      • bala venkata siva ram kum
                        New Member
                        • May 2010
                        • 15

                        #12
                        ok,it is better check the condition. Thanq

                        Comment

                        Working...