Problem related to insert/update record

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • navneetkaur
    New Member
    • Sep 2007
    • 45

    Problem related to insert/update record

    Updation Should Reflect at same time
    hi every1
    i am stuck at 1 place...i want whatever updation i do it should be reflected at the same time in table(i am using table to display all records)then if i delete 1 record frm this table it shd be reflected at the same time in table....bt its not working i am using following codeplz help me....m waiting fr ur response....


    code is
    Code:
            SqlCommand cmd;
            string str;
            conn.Open();
            str = "update cur_month_ach set active = 0 where rowid=" + rwid + "";
            cmd = new SqlCommand(str, conn);
            int temp = cmd.ExecuteNonQuery();
            conn.Close();
    'd there is 1 html table in which i displayed all these records 'd there is 1 command button deactivate....w hen i click on this i pass row id 'd above procedure is called.....
  • mohitkatariya
    New Member
    • Apr 2007
    • 26

    #2
    After Updation you must have to fetch data again from the table and show it in table.

    Originally posted by navneetkaur
    Updation Should Reflect at same time
    hi every1
    i am stuck at 1 place...i want whatever updation i do it should be reflected at the same time in table(i am using table to display all records)then if i delete 1 record frm this table it shd be reflected at the same time in table....bt its not working i am using following codeplz help me....m waiting fr ur response....


    code is
    Code:
            SqlCommand cmd;
            string str;
            conn.Open();
            str = "update cur_month_ach set active = 0 where rowid=" + rwid + "";
            cmd = new SqlCommand(str, conn);
            int temp = cmd.ExecuteNonQuery();
            conn.Close();
    'd there is 1 html table in which i displayed all these records 'd there is 1 command button deactivate....w hen i click on this i pass row id 'd above procedure is called.....

    Comment

    • navneetkaur
      New Member
      • Sep 2007
      • 45

      #3
      Originally posted by mohitkatariya
      After Updation you must have to fetch data again from the table and show it in table.
      hello mohit thanx a lot fr reply
      but i hv nt done so because it vl create overhead on system...this table creation is called during pageload event.....bt acc to me coz of page life cycle its nt updating....
      sp olz if u hv any idea help me...m waiting

      Comment

      • mohitkatariya
        New Member
        • Apr 2007
        • 26

        #4
        Hi Navneet
        You might hv written your code in
        if(!Page.IsPost Back)
        {
        //your code
        }
        So according to page's life cycle this code will not run again untill postback is not equal to false again. So after updating ur database u have to rebind ur table.
        I think thats the only way...

        Originally posted by navneetkaur
        hello mohit thanx a lot fr reply
        but i hv nt done so because it vl create overhead on system...this table creation is called during pageload event.....bt acc to me coz of page life cycle its nt updating....
        sp olz if u hv any idea help me...m waiting

        Comment

        • navneetkaur
          New Member
          • Sep 2007
          • 45

          #5
          Originally posted by mohitkatariya
          Hi Navneet
          You might hv written your code in
          if(!Page.IsPost Back)
          {
          //your code
          }
          So according to page's life cycle this code will not run again untill postback is not equal to false again. So after updating ur database u have to rebind ur table.
          I think thats the only way...
          hi mohit....
          i have jst added this fxn without checking ispostback it is executed at every time......its already rebuilding after every postback....nw plz if u hv solution tell me....

          Comment

          • Frinavale
            Recognized Expert Expert
            • Oct 2006
            • 9749

            #6
            Originally posted by navneetkaur
            hello mohit thanx a lot fr reply
            but i hv nt done so because it vl create overhead on system...this table creation is called during pageload event.....bt acc to me coz of page life cycle its nt updating....
            sp olz if u hv any idea help me...m waiting
            If you create the table (the data source for your GridView) with every page load your Update function will not work.

            You should only create the table (the data source for your GridView) only once...when it is Not Postback... and store the table somewhere (like Session) so that you don't have to recreate it every time your page is loaded.....this is good practice for Many reasons. (One reason being that all of the data a user provides for an Update will be lost when you recreate the DataSource for the GridView in the PageLoad....bec ause the original data is lost when the DataSource is changed...and the PageLoad event happens before the Update event occurs...)

            Once you have completed your Delete or Update you should recreate the table (the data source for your GridView)...the n DataBind your GridView to the Updated DataSource to reflect the changes.

            -Frinny

            Comment

            • mohitkatariya
              New Member
              • Apr 2007
              • 26

              #7
              Hi Navneet
              You told that you have not checked 'ispostback' property. I think your data might be updated because when you click update button first pageload event will be fired where u have wriiten your gridbind code, So when page load fires again it will take data from original data and the chngd values will be lost. Check your data in query analyzer that it is updatied or not.
              seq of event is Page_load->Button_Click
              you can check this by putting breakpoints in your code.
              So i think you must put ur grid binding proc. in postback event.
              If u still getting prblm then give me more details abt ur code.
              Hope it will work....
              Originally posted by navneetkaur
              hi mohit....
              i have jst added this fxn without checking ispostback it is executed at every time......its already rebuilding after every postback....nw plz if u hv solution tell me....

              Comment

              • navneetkaur
                New Member
                • Sep 2007
                • 45

                #8
                Originally posted by Frinavale
                If you create the table (the data source for your GridView) with every page load your Update function will not work.

                You should only create the table (the data source for your GridView) only once...when it is Not Postback... and store the table somewhere (like Session) so that you don't have to recreate it every time your page is loaded.....this is good practice for Many reasons. (One reason being that all of the data a user provides for an Update will be lost when you recreate the DataSource for the GridView in the PageLoad....bec ause the original data is lost when the DataSource is changed...and the PageLoad event happens before the Update event occurs...)

                Once you have completed your Delete or Update you should recreate the table (the data source for your GridView)...the n DataBind your GridView to the Updated DataSource to reflect the changes.

                -Frinny
                First of all Thanx Sir.....i got ur point bt i am not able to do like that coz whenever i place this table creation on PageLoad as ispostback property...it disappear from the page....sir i am going to write my code.....plz help me by providing coding.....

                [CODE=cpp]


                public partial class _Default : System.Web.UI.P age
                {
                Class1 cls;
                DataSet ds;

                int curyear, curmonth;
                string rowid;
                protected void Page_Load(objec t sender, EventArgs e)
                {
                cls = new Class1();
                ds = new DataSet();
                cur_proj_assign ed(); //this is where i am calling table
                // creation...bt it does nt update without response.redire ct(to same page);
                }
                //this module is creating table dynamically to display records....
                public void cur_proj_assign ed()
                {
                DataSet ds = new DataSet();//to show achievements fr current month
                curmonth = 09;
                curyear = 2007;
                ecode="012345";
                ds = cls.cur_month_a ch(ecode, curmonth, curyear);


                string project_title, project_descrip tion, div_name = "";
                string div_code;
                Modcur.EnableVi ewState = true;
                if (ds.Tables[0].Rows.Count > 0)
                {

                HtmlTableRow r3 = new HtmlTableRow();
                HtmlTableCell c5 = new HtmlTableCell() ;
                c5.ColSpan = 5;
                c5.Align = "left";
                c5.BgColor = "#ccffff";
                c5.Height = "23px";
                r3.Cells.Add(c5 );

                HtmlTableRow r4 = new HtmlTableRow();
                HtmlTableCell c6 = new HtmlTableCell() ;
                c6.InnerText = "Select";
                HtmlTableCell c7 = new HtmlTableCell() ;
                c7.InnerText = "Project Title ";
                HtmlTableCell c8 = new HtmlTableCell() ;
                c8.InnerText = "Project Description";
                HtmlTableCell c9 = new HtmlTableCell() ;
                c9.InnerText = "Status";
                HtmlTableCell c10 = new HtmlTableCell() ;
                c10.InnerText = " Project Division";

                r4.Cells.Add(c6 );
                r4.Cells.Add(c7 );
                r4.Cells.Add(c8 );
                r4.Cells.Add(c1 0);
                r4.Cells.Add(c9 );

                Modcur.Rows.Add (r3);
                Modcur.Rows.Add (r4);
                }
                int temp = ds.Tables[0].Rows.Count;

                for (int i = 0; i < temp; i++)
                {
                string div_name1;
                DataSet ds1 = new DataSet();
                rowid = ds.Tables[0].Rows[i][1].ToString();
                project_title = ds.Tables[0].Rows[i][4].ToString();
                project_descrip tion = ds.Tables[0].Rows[i][5].ToString();
                c = ds.Tables[0].Rows[i][6].ToString();
                div_code = ds.Tables[0].Rows[i][7].ToString();
                if (div_code != "")
                {
                div_name1 = cls.fetch_divis ion_name(div_co de);
                div_name = div_name1;
                }
                HtmlInputHidden idh = new HtmlInputHidden ();
                RadioButton rd = new RadioButton();
                rd.GroupName = "rdbtn_grp" ;
                rd.ID = "rid" + i;
                idh.ID = "hhd" + i;

                idh.Value = rowid.ToString( );//put database value here

                HtmlTableCell c1 = new HtmlTableCell() ;
                c1.ColSpan = 1;
                c1.Controls.Add (rd);
                c1.Controls.Add (idh);


                TextBox prjtitle = new TextBox();

                prjtitle.TextMo de = TextBoxMode.Mul tiLine;
                prjtitle.Text = project_title;
                prjtitle.ReadOn ly = true;
                HtmlTableCell c2 = new HtmlTableCell() ;
                c2.ColSpan = 1;
                c2.Controls.Add (prjtitle);

                TextBox prjdesc = new TextBox();

                prjdesc.TextMod e = TextBoxMode.Mul tiLine;
                prjdesc.Text = project_descrip tion;
                prjdesc.ReadOnl y = true;
                HtmlTableCell c3 = new HtmlTableCell() ;
                c3.ColSpan = 1;
                c3.Controls.Add (prjdesc);

                TextBox division_txt = new TextBox();

                division_txt.Te xtMode = TextBoxMode.Mul tiLine;
                division_txt.Te xt = div_name;
                division_txt.Re adOnly = true;
                HtmlTableCell c5 = new HtmlTableCell() ;
                c5.ColSpan = 1;
                c5.Controls.Add (division_txt);

                HtmlTableCell c4 = new HtmlTableCell() ;
                Label status1 = new Label();
                if (c == "True")
                {
                status1.Text = "Active";
                c4.BgColor = "99ccff";
                }
                else
                {
                status1.Text = "DeActive";
                c4.BgColor = "ccffcc";
                }
                c4.ColSpan = 1;
                c4.Controls.Add (status1);
                HtmlTableRow r1 = new HtmlTableRow();
                r1.Cells.Add(c1 );
                r1.Cells.Add(c2 );
                r1.Cells.Add(c3 );
                r1.Cells.Add(c5 );
                r1.Cells.Add(c4 );

                Modcur.Rows.Add (r1);
                }

                if (ds.Tables[0].Rows.Count > 0)
                {
                Button deactachievemen ts = new Button();

                deactachievemen ts.ID = "deactach";
                deactachievemen ts.Text = "DeActivate ";
                deactachievemen ts.Click += new EventHandler(de actachievements _Click);

                HtmlTableCell c4 = new HtmlTableCell() ;
                c4.ColSpan = 5;

                c4.Controls.Add (deactachieveme nts);

                HtmlTableRow r2 = new HtmlTableRow();
                r2.Cells.Add(c4 );
                Modcur.Rows.Add (r2);
                }

                }
                //click event of delete button of current month added project
                protected void deactachievemen ts_Click(object sender, System.EventArg s e)
                {
                int rowid = 0;
                RadioButton rowid1 = new RadioButton();
                HtmlInputHidden htt = new HtmlInputHidden ();
                int i = 0;
                foreach (Control rd in Modcur.Controls )
                {
                rowid1 = (RadioButton)Mo dcur.FindContro l("rid" + i);
                htt = (HtmlInputHidde n)Modcur.FindCo ntrol("hhd" + i);
                i += 1;
                if (rowid1 != null && rowid1.Checked)
                rowid = int.Parse(htt.V alue);
                }
                cls.cur_month_d eactivate(rowid );
                //Server.Transfer ("Default.aspx" );
                }
                }[/code]
                Sir i have placed my code this is jst a abstract of full code....sir like above 1 dynamic button DEACTIVATE i hv to attach so many buttons also....Sir plz help me....
                Last edited by Frinavale; Sep 20 '07, 01:05 PM. Reason: Fixed [code] tags so that post is viewable

                Comment

                • Frinavale
                  Recognized Expert Expert
                  • Oct 2006
                  • 9749

                  #9
                  Originally posted by navneetkaur
                  First of all Thanx Sir.....i got ur point bt i am not able to do like that coz whenever i place this table creation on PageLoad as ispostback property...it disappear from the page....sir i am going to write my code.....plz help me by providing coding.....

                  Sir i have placed my code this is jst a abstract of full code....sir like above 1 dynamic button DEACTIVATE i hv to attach so many buttons also....Sir plz help me....
                  You are doing Way too much work here.
                  A GridView has a method called DataBind.
                  What you do is set your GridView.Source = some Data Set (or something) and then call the GridView.DataBi nd to "Bind" that data to the GridView.

                  Please look at the GridView Class as described by MSDN...particul arly the following part:

                  Binding to Data

                  The GridView control can be bound to a data source control (such as SqlDataSource, ObjectDataSourc e, and so on), as well as any data source that implements the System.Collecti ons.IEnumerable interface (such as System.Data.Dat aView, System.Collecti ons.ArrayList, or System.Collecti ons.Hashtable).
                  .....
                  To bind to a data source that implements the System.Collecti ons.IEnumerable interface, programmaticall y set the DataSource property of the GridView control to the data source and then call the DataBind method. When using this method, the GridView control does not provide built-in sorting, updating, deleting, and paging functionality. You need to provide this functionality by using the appropriate event.


                  Using the DataBind method will get rid of most of your code.
                  All you need to do is create the DataSource (your data set) Once...in the page load when it is not postback...and store it in Session (or somewhere) so that you don't need to recreate it over and over again....

                  Please look at the GridView Class (documentation provided by MSDN). You will find a lot of tutorials on this page along with examples and anything else you would like to know.

                  Cheers!
                  -Frinny

                  Comment

                  • mohitkatariya
                    New Member
                    • Apr 2007
                    • 26

                    #10
                    hi navneet i read ur code.
                    as i said u r calling ur function "cur_proj_assig ned " without ispostback so ur values are not saved.
                    if u put ur function in postback it will run only one time and u hv to call it after updationation agn and it will not put extra overhead on ur programe. ur function is called with pageload everytime now but after postback it will be called only first time in pageload, and agn whn u called it after button click.
                    so there will be no prblm related to efficiency of the form.

                    Comment

                    • navneetkaur
                      New Member
                      • Sep 2007
                      • 45

                      #11
                      Originally posted by mohitkatariya
                      hi navneet i read ur code.
                      as i said u r calling ur function " without ispostback so ur values are not saved.
                      if u put ur function in postback it will run only one time and u hv to call it after updationation agn and it will not put extra overhead on ur programe. ur function is called with pageload everytime now but after postback it will be called only first time in pageload, and agn whn u called it after button click.
                      so there will be no prblm related to efficiency of the form.

                      hi mohitkatariya thanx for seeing this...mohit i agree to ur suggestion bt nw actually problem is on this page i am having near abt 12 buttons i have jst posted 1 just coz of convience...... then i hv to call this fxn "cur_proj_assig ned " on every button click...i vl try this if it works i vl tell u....thanx for spending ur valuable time on this problem.....

                      Comment

                      • navneetkaur
                        New Member
                        • Sep 2007
                        • 45

                        #12
                        Originally posted by Frinavale
                        You are doing Way too much work here.
                        A GridView has a method called DataBind.
                        What you do is set your GridView.Source = some Data Set (or something) and then call the GridView.DataBi nd to "Bind" that data to the GridView.

                        Please look at the GridView Class as described by MSDN...particul arly the following part:

                        Binding to Data

                        The GridView control can be bound to a data source control (such as SqlDataSource, ObjectDataSourc e, and so on), as well as any data source that implements the System.Collecti ons.IEnumerable interface (such as System.Data.Dat aView, System.Collecti ons.ArrayList, or System.Collecti ons.Hashtable).
                        .....
                        To bind to a data source that implements the System.Collecti ons.IEnumerable interface, programmaticall y set the DataSource property of the GridView control to the data source and then call the DataBind method. When using this method, the GridView control does not provide built-in sorting, updating, deleting, and paging functionality. You need to provide this functionality by using the appropriate event.


                        Using the DataBind method will get rid of most of your code.
                        All you need to do is create the DataSource (your data set) Once...in the page load when it is not postback...and store it in Session (or somewhere) so that you don't need to recreate it over and over again....

                        Please look at the GridView Class (documentation provided by MSDN). You will find a lot of tutorials on this page along with examples and anything else you would like to know.

                        Cheers!
                        -Frinny
                        thanx sir i do agree with u bt sir acc to my TL requirement it works like that coz sir there are 4 more click events on this table which is nt possible in gridview....so sir plz tell me solution....sir nw i have placed this code in ispostback property bt nw i am nt able to keep this table state 'd rebuild it again on click event..bt sir 2dy i vl complete it..then i let u knw...thanx fr spending ur valuable time....

                        Comment

                        • navneetkaur
                          New Member
                          • Sep 2007
                          • 45

                          #13
                          hi every1 thanx for responding to my problem 'd
                          according to ur suggestions i placed fxn "cur_proj_assig ned" on ispostback event....but nw 1 more problem is encountered that is click event of dynamically button is not fired because of dynamic controls event should be placed either on pageload or init event ... so plz tell me solution what i will do now......'d 1 thing more i am not able to again draw table.....

                          plz provide me coding if u knw.....
                          thanks
                          with warm regards
                          Navneet

                          Comment

                          • navneetkaur
                            New Member
                            • Sep 2007
                            • 45

                            #14
                            ....waiting fr ur respone...its urgent

                            Comment

                            • mohitkatariya
                              New Member
                              • Apr 2007
                              • 26

                              #15
                              I think you have seprate ur function in two parts one is for desiging table n other is for binding. desining part is called on page load so that events get registered, while binding function will be called on every button click.
                              May be it will work. Hope for best...
                              Originally posted by navneetkaur
                              ....waiting fr ur respone...its urgent

                              Comment

                              Working...