Updating the Data into Database from the Textboxes

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • nandithadevaraj
    New Member
    • Feb 2008
    • 16

    Updating the Data into Database from the Textboxes

    Hai All,
    Please help me on this
    How to Update the Data which is present in the textboxes, the data is retrived from the database to textboxes, Just i want to modify and to update in the Database i am trying this in C# language please help me out

    Thank u
  • dip_developer
    Recognized Expert Contributor
    • Aug 2006
    • 648

    #2
    Originally posted by nandithadevaraj
    Hai All,
    Please help me on this
    How to Update the Data which is present in the textboxes, the data is retrived from the database to textboxes, Just i want to modify and to update in the Database i am trying this in C# language please help me out

    Thank u
    read thoroughly basic database operation in ADO.NET......



    Comment

    • nandithadevaraj
      New Member
      • Feb 2008
      • 16

      #3
      I tried by using following Code but its taking only direct values but its not updating whatever the content Present in textboxes.. plz tell me how to Update the content of textbox



      string sql = "Update UserGroups SET grpDescription= @grpDescription , grpActive=@grpA ctive, grpToDate=@grpT oDate where(grpID =@grpID)";
      SqlCommand cm = new SqlCommand(sql, myConnection);
      //cm.CommandText = sql;
      //cm.Connection = myConnection;
      cm.Parameters.A dd(new SqlParameter("@ grpID", SqlDbType.Int, 255));
      cm.Parameters["@grpID"].Value ='3';
      //cm.Parameters.A dd("@grpID", SqlDbType.Int, 255).Value =Request.QueryS tring["id"];
      cm.Parameters.A dd(new SqlParameter("@ grpDescription" , SqlDbType.VarCh ar, 50));
      cm.Parameters["@grpDescriptio n"].Value =txtGrpDescript ion.Text;
      cm.Parameters.A dd(new SqlParameter("@ grpActive", SqlDbType.VarCh ar, 50));
      cm.Parameters["@grpActive "].Value = rdbValid.Select edItem.Text;
      // cm.Parameters.A dd("@grpFrmDate ", SqlDbType.DateT ime).Value = txtFrmDate.Text ;
      cm.Parameters.A dd(new SqlParameter("@ grpToDate", SqlDbType.DateT ime));
      cm.Parameters["@grpToDate "].Value = txtToDate.Text;
      //I THINK I AM LOSSING HERE

      try
      {

      myConnection.Op en();
      //myConnection.Op en();
      cm.ExecuteNonQu ery();
      myConnection.Cl ose();
      }
      catch(Exception ex)
      {
      Console.WriteLi ne(ex.Message);
      }

      Comment

      • enggwaqas
        New Member
        • Jun 2007
        • 19

        #4
        Originally posted by nandithadevaraj
        I tried by using following Code but its taking only direct values but its not updating whatever the content Present in textboxes.. plz tell me how to Update the content of textbox



        string sql = "Update UserGroups SET grpDescription= @grpDescription , grpActive=@grpA ctive, grpToDate=@grpT oDate where(grpID =@grpID)";
        SqlCommand cm = new SqlCommand(sql, myConnection);
        //cm.CommandText = sql;
        //cm.Connection = myConnection;
        cm.Parameters.A dd(new SqlParameter("@ grpID", SqlDbType.Int, 255));
        cm.Parameters["@grpID"].Value ='3';
        //cm.Parameters.A dd("@grpID", SqlDbType.Int, 255).Value =Request.QueryS tring["id"];
        cm.Parameters.A dd(new SqlParameter("@ grpDescription" , SqlDbType.VarCh ar, 50));
        cm.Parameters["@grpDescriptio n"].Value =txtGrpDescript ion.Text;
        cm.Parameters.A dd(new SqlParameter("@ grpActive", SqlDbType.VarCh ar, 50));
        cm.Parameters["@grpActive "].Value = rdbValid.Select edItem.Text;
        // cm.Parameters.A dd("@grpFrmDate ", SqlDbType.DateT ime).Value = txtFrmDate.Text ;
        cm.Parameters.A dd(new SqlParameter("@ grpToDate", SqlDbType.DateT ime));
        cm.Parameters["@grpToDate "].Value = txtToDate.Text;
        //I THINK I AM LOSSING HERE

        try
        {

        myConnection.Op en();
        //myConnection.Op en();
        cm.ExecuteNonQu ery();
        myConnection.Cl ose();
        }
        catch(Exception ex)
        {
        Console.WriteLi ne(ex.Message);
        }
        What is the exact error you are receving?

        Comment

        • saran23
          New Member
          • Mar 2008
          • 28

          #5
          Originally posted by nandithadevaraj
          I tried by using following Code but its taking only direct values but its not updating whatever the content Present in textboxes.. plz tell me how to Update the content of textbox



          string sql = "Update UserGroups SET grpDescription= @grpDescription , grpActive=@grpA ctive, grpToDate=@grpT oDate where(grpID =@grpID)";
          SqlCommand cm = new SqlCommand(sql, myConnection);
          //cm.CommandText = sql;
          //cm.Connection = myConnection;
          cm.Parameters.A dd(new SqlParameter("@ grpID", SqlDbType.Int, 255));
          cm.Parameters["@grpID"].Value ='3';
          //cm.Parameters.A dd("@grpID", SqlDbType.Int, 255).Value =Request.QueryS tring["id"];
          cm.Parameters.A dd(new SqlParameter("@ grpDescription" , SqlDbType.VarCh ar, 50));
          cm.Parameters["@grpDescriptio n"].Value =txtGrpDescript ion.Text;
          cm.Parameters.A dd(new SqlParameter("@ grpActive", SqlDbType.VarCh ar, 50));
          cm.Parameters["@grpActive "].Value = rdbValid.Select edItem.Text;
          // cm.Parameters.A dd("@grpFrmDate ", SqlDbType.DateT ime).Value = txtFrmDate.Text ;
          cm.Parameters.A dd(new SqlParameter("@ grpToDate", SqlDbType.DateT ime));
          cm.Parameters["@grpToDate "].Value = txtToDate.Text;
          //I THINK I AM LOSSING HERE

          try
          {

          myConnection.Op en();
          //myConnection.Op en();
          cm.ExecuteNonQu ery();
          myConnection.Cl ose();
          }
          catch(Exception ex)
          {
          Console.WriteLi ne(ex.Message);
          }

          Hi Nanditha,

          Ur problem is simple,The code u have used is absolutely correct, no problem in that, the problem is ur binding the values in textboxes from Database, Ur doing this in PageLoad event right?

          When u change the values in textboxes and click Save button the page gets PostBack and again the values in Database is binded in textboxes, ur updated values are lost.

          To avoid this,In the Pageload, put the whole code which gets the values from Database inside the
          Code:
          (!IsPostback)


          just do this in PageLoad

          Code:
          if(!IsPostBack)
          {
          //ur code to get values from databse
          }
          this will prevent the values from DB binding to textbox each time u click save.
          Hope u can proceed with this
          If ur not clear feel free to ask me.

          Thanks
          Saravanan.

          Comment

          • nandithadevaraj
            New Member
            • Feb 2008
            • 16

            #6
            Originally posted by enggwaqas
            What is the exact error you are receving?
            I am not getting any error but the data is not updated thats all

            Comment

            • saran23
              New Member
              • Mar 2008
              • 28

              #7
              Originally posted by nandithadevaraj
              I am not getting any error but the data is not updated thats all
              I think ur not getting me right,
              U wont get any error, but when u Click the save button, the values u changed will be losed,and values from the database will be got again and the textboxes will hold this old value, this old value will be updated in the Database,

              See the below code for reference.
              I will bet money, this will work.

              Code:
              protected void Page_Load(object sender, EventArgs e)
                  {
                                  if (!IsPostBack)
                                       {
                                           //Ur code to get values from Database
                                       }
                            
                   }
                  }

              Comment

              • nandithadevaraj
                New Member
                • Feb 2008
                • 16

                #8
                No NO.. I got it.. its working now.. ya i was doing same mistake what u told

                Once again Thank u..

                Comment

                Working...