Refreshing Data After Update in Code

After performing an update of the database (in a Server_Change event), the database is being updated successfully, but I cannot see the update (on the ASPxGrid), until I press Refresh on the browser, and I press the link of the page on the left frame.......then the page would reappear with the ASPxGrid updated......but I want that to happen automatically.....any help??
LVL 3
mte01Asked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
mmarinovConnect With a Mentor Commented:
mte01,

you are correct. thanks for the correction
and have you tried
command2.ExecuteNonQuery();
sqlConnection1.Close();

 sqlConnection1.ConnectionString = DatabaseChoser.connString;
sqlConnection1.Open();
this.sqlDataAdapter1.Fill(dataSet21);
sqlConnection1.Close();

ASPxGrid2.DataSource = dataSet21;
ASPxGrid2.DataBind();
Regards!
B..M
mmarinov
0
 
mmarinovCommented:
Hi mte01,

after update the database ( in the updatecommand or itemcommand event of the datagrid ) you have to rebind it ( get data from db and DataBind the grid )

Regards!
B..M
mmarinov
0
 
mte01Author Commented:
I am updating the db using my own updatecommand,...............and although I am calling the Page-Load event again in my code, it's also not working........
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
mmarinovCommented:
mte01,

your scenario MUST be

Page_load
 if Not IsPostBack Then
    bind the grid
 End if

UpdateCommand
 update db
 bind the grid

Regards!
B..M
mmarinov
0
 
mte01Author Commented:
It didn't work,............and it also caused an error by not displaying part of my data..:)

I'll post here the whole code of my page:

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

namespace CompanyApp
{
      /// <summary>
      /// Summary description for EmpAcct.
      /// </summary>
      public class EmpAcct : System.Web.UI.Page
      {
            protected System.Data.SqlClient.SqlDataAdapter sqlDataAdapter1;
            protected System.Data.SqlClient.SqlConnection sqlConnection1;
            protected System.Data.SqlClient.SqlCommand sqlSelectCommand1;
            protected DevExpress.Web.ASPxGrid.ASPxGrid ASPxGrid1;
            protected System.Web.UI.WebControls.TextBox txtvalue;
            protected System.Web.UI.HtmlControls.HtmlInputHidden hdnValue;
            protected System.Web.UI.WebControls.Button btnpress;
            protected DevExpress.Web.ASPxGrid.ASPxGrid ASPxGrid2;
            protected CompanyApp.DataSet2 dataSet21;
      
            private void Page_Load(object sender, System.EventArgs e)
            {
                  sqlConnection1.ConnectionString = DatabaseChoser.connString;
                  sqlConnection1.Open();
                  this.sqlDataAdapter1.Fill(dataSet21);
                  sqlConnection1.Close();


            }

            #region Web Form Designer generated code
            override protected void OnInit(EventArgs e)
            {
                  //
                  // CODEGEN: This call is required by the ASP.NET Web Form Designer.
                  //
                  InitializeComponent();
                  base.OnInit(e);
            }
            
            /// <summary>
            /// Required method for Designer support - do not modify
            /// the contents of this method with the code editor.
            /// </summary>
            private void InitializeComponent()
            {    
                  this.sqlDataAdapter1 = new System.Data.SqlClient.SqlDataAdapter();
                  this.sqlSelectCommand1 = new System.Data.SqlClient.SqlCommand();
                  this.sqlConnection1 = new System.Data.SqlClient.SqlConnection();
                  this.dataSet21 = new CompanyApp.DataSet2();
                  ((System.ComponentModel.ISupportInitialize)(this.dataSet21)).BeginInit();
                  //
                  // sqlDataAdapter1
                  //
                  this.sqlDataAdapter1.SelectCommand = this.sqlSelectCommand1;
                  this.sqlDataAdapter1.TableMappings.AddRange(new System.Data.Common.DataTableMapping[] {
                                                                                                                                                        new System.Data.Common.DataTableMapping("Table", "UPR00100", new System.Data.Common.DataColumnMapping[] {
                                                                                                                                                                                                                                                                              new System.Data.Common.DataColumnMapping("EMPLOYID", "EMPLOYID"),
                                                                                                                                                                                                            new System.Data.Common.DataColumnMapping("LASTNAME", "LASTNAME"),
                                                                                                                                                                                                                                                                                new System.Data.Common.DataColumnMapping("FRSTNAME", "FRSTNAME"),
                                                                                                                                                                                                                                                                                new System.Data.Common.DataColumnMapping("ACTNUMST", "ACTNUMST"),
                                                                                                                                                                                                                                                                              new System.Data.Common.DataColumnMapping("ACTDESCR", "ACTDESCR")})});
                  //
                  // sqlSelectCommand1
                  //
                  this.sqlSelectCommand1.CommandText = "SELECT UPR00100.EMPLOYID, UPR00100.LASTNAME, UPR00100.FRSTNAME, GL00105.ACTNUMST," +
                        " GL00100.ACTDESCR FROM UPR00100 INNER JOIN GL00100 ON UPR00100.ACTINDX = GL00100" +
                        ".ACTINDX INNER JOIN GL00105 ON UPR00100.ACTINDX = GL00105.ACTINDX";
                  this.sqlSelectCommand1.Connection = this.sqlConnection1;
                  //
                  // sqlConnection1
                  //
                  //this.sqlConnection1.ConnectionString = "workstation id=\"AUB-MOHD\";packet size=4096;integrated security=SSPI;data source=\"" +
                  //      "AUB-MOHD\";persist security info=False;initial catalog=IDS";
                  //
                  // dataSet21
                  //
                  this.dataSet21.DataSetName = "DataSet2";
                  this.dataSet21.Locale = new System.Globalization.CultureInfo("en-US");
                  this.btnpress.Click += new System.EventHandler(this.btnpress_Click);
                  this.hdnValue.ServerChange += new EventHandler(this.hdnValue_ServerChange);
                  this.Load += new System.EventHandler(this.Page_Load);
                  ((System.ComponentModel.ISupportInitialize)(this.dataSet21)).EndInit();

            }
            #endregion

            private void btnpress_Click(object sender, System.EventArgs e)
            {
                  txtvalue.Text = hdnValue.Value;
            }
            
            private void hdnValue_ServerChange(object sender, System.EventArgs e)
            {
                  if(hdnValue.Value.CompareTo("undefined")==0)
                        return;
                  int curvalue=-1;
                  int selindex = ASPxGrid1.GetSelectedRow(0).Index;
                  String selid = ASPxGrid1.Rows[selindex].DataControllerRow[0].ToString();
                  String comstring= "SELECT GL00105.ACTINDX " +
                        "FROM GL00105 " +
                        "WHERE GL00105.ACTNUMST = '" + hdnValue.Value + "' ";

                  sqlConnection1.Open();
                  SqlCommand command = new SqlCommand();
                  command.Connection= sqlConnection1;
                  command.CommandText= comstring;
                  
                  SqlDataAdapter dataAdapter = new SqlDataAdapter();
                  dataAdapter.SelectCommand = command;
                         
                  DataSet valdataSet = new DataSet();
                  dataAdapter.Fill(valdataSet,"Value");
                  DataTable valdataTable = valdataSet.Tables["Value"];

                  curvalue = (int) valdataTable.Rows[0][0];
                  
                  String comstring2 = "UPDATE UPR00100 " +
                  "SET UPR00100.ACTINDX = '" + curvalue + "' " +
                  "WHERE UPR00100.EMPLOYID = '" + selid + "' ";

                  SqlCommand command2 = new SqlCommand();
                  command2.Connection= sqlConnection1;
                  command2.CommandText= comstring2;
                  command2.ExecuteNonQuery();

                  ASPxGrid1.DataBind();

                  sqlConnection1.Close();
                  //this.Page_Load(null,null);
                  return;
            }
            
      }
}
0
 
mte01Author Commented:
I am making the update in the hdnValue_ServerChange function, and I am setting the connection string for the SqlConnection object dynamically using a string from another page.
0
 
mmarinovCommented:
mte01,

to bind the grid you have to refilled the dataset
so i think that the right code should be this:

private void hdnValue_ServerChange(object sender, System.EventArgs e)
          {
               if(hdnValue.Value.CompareTo("undefined")==0)
                    return;
               int curvalue=-1;
               int selindex = ASPxGrid1.GetSelectedRow(0).Index;
               String selid = ASPxGrid1.Rows[selindex].DataControllerRow[0].ToString();
               String comstring= "SELECT GL00105.ACTINDX " +
                    "FROM GL00105 " +
                    "WHERE GL00105.ACTNUMST = '" + hdnValue.Value + "' ";

               sqlConnection1.Open();
               SqlCommand command = new SqlCommand();
               command.Connection= sqlConnection1;
               command.CommandText= comstring;
               
               SqlDataAdapter dataAdapter = new SqlDataAdapter();
               dataAdapter.SelectCommand = command;
                     
               DataSet valdataSet = new DataSet();
               dataAdapter.Fill(valdataSet,"Value");
               DataTable valdataTable = valdataSet.Tables["Value"];

               curvalue = (int) valdataTable.Rows[0][0];
               
               String comstring2 = "UPDATE UPR00100 " +
               "SET UPR00100.ACTINDX = '" + curvalue + "' " +
               "WHERE UPR00100.EMPLOYID = '" + selid + "' ";

               SqlCommand command2 = new SqlCommand();
               command2.Connection= sqlConnection1;
               command2.CommandText= comstring2;
               command2.ExecuteNonQuery();

               sqlConnection1.ConnectionString = DatabaseChoser.connString;
               sqlConnection1.Open();
               this.sqlDataAdapter1.Fill(dataSet21);
               sqlConnection1.Close();

               sqlConnection1.Close();
               //this.Page_Load(null,null);
               return;
          }


Regards!
B..M
mmarinov
0
 
mte01Author Commented:
I had already tried it (before asking the question), and it didn't work, and as a correction for your syntax:

command2.ExecuteNonQuery();
sqlConnection1.Close();

 sqlConnection1.ConnectionString = DatabaseChoser.connString;
sqlConnection1.Open();
this.sqlDataAdapter1.Fill(dataSet21);
sqlConnection1.Close();


becuase your syntax would cause a runtime error (because of changing the connection string property while the connection is opne)
0
 
mte01Author Commented:
Just did the trick!!!.....with some modifications..:))....and unecessary code elimination.......here's the correct syntax for archiving purposes:


command2.ExecuteNonQuery();

dataSet21.Reset();
this.sqlDataAdapter1.Fill(dataSet21);
               
ASPxGrid1.DataBind();
ASPxGrid2.DataBind();




Thanks!!
0
All Courses

From novice to tech pro — start learning today.