Data inconsistency in MS Access and OleDb

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • akshaycjoshi
    New Member
    • Jan 2007
    • 153

    #1

    Data inconsistency in MS Access and OleDb

    I am making one app. in which i am updating the treeview by reading oen access database.

    There is one option of renaming nodes of particular level.

    When certain action happens (like renaming the nodes etc) I refresh the treeview.

    When i perform renaming it gets renamed in the Access database but the changes are not reflected in the refreshing function.

    For proof i am attaching the picture.

    P.S. For updating the database i use the connected version(Execute NonQuery) and while refreshing, i use the disconencted version.(DataAd apter.Fill)

    When i restart the app (when refreshing is done once in the formload event) I can see the actual nodes (as in the database) !

    Where am I going wrong ?

    Real time:


    After restart of app:
  • akshaycjoshi
    New Member
    • Jan 2007
    • 153

    #2
    Seems like i will have to do the following to get rid of it in the AfterLabelEdit event.


    e.CancelEdit =true
    treeexchange.La belEdit = false;
    treeexchange.Se lectedNode.EndE dit(false);

    Working OK after this , will get back if anything bad happens :)

    Thanks !

    Comment

    • akshaycjoshi
      New Member
      • Jan 2007
      • 153

      #3
      It has sorted out the problem but still I am confused about what is actually happening.
      Here is my code

      [SIZE=2][/SIZE]
      Code:
       
      if (e.Label == null || e.Label.Trim()=="")
      {
      e.CancelEdit = true;
      return;
      }
      foreach (char c in e.Label)
      {
      if ((char.IsLetter(c) == false & char.IsDigit(c) == false) | (char.IsWhiteSpace(c)==true) )
      {
      MessageBox.Show("Only letters and digits are allowed ", "Call Analyzer", MessageBoxButtons.OK, MessageBoxIcon.Information);
      e.CancelEdit = true;
      treeexchange.SelectedNode.BeginEdit();
      return;
      }
      }
      e.CancelEdit = true;
      treeexchange.LabelEdit = false;
      treeexchange.SelectedNode.EndEdit(false);
      OleDbConnection renamegroupcon = null;
      OleDbCommand renamegroupextcom = null;
      OleDbCommand renamegroupgrpcom = null;
      try
      {
      renamegroupcon = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Application.StartupPath + @"\exchangesandtrunks.mdb;Persist Security Info=False");
      renamegroupcon.Open();
      renamegroupgrpcom = new OleDbCommand("update t_extensiongroups set group_name='" + e.Label + "' where group_name='" + treeexchange.SelectedNode.Text + "'", renamegroupcon);
      renamegroupgrpcom.ExecuteNonQuery();
      renamegroupextcom = new OleDbCommand("update t_extensions set group_name='" + e.Label + "' where group_name='" + treeexchange.SelectedNode.Text + "'", renamegroupcon);
      renamegroupextcom.ExecuteNonQuery();
      }
      catch (Exception ex)
      {
      MessageBox.Show("An error occured while trying to rename the group.(Group already exists ?)", "Call Analyzer", MessageBoxButtons.OK, MessageBoxIcon.Warning);
      try
      {
      string error = Environment.NewLine + "*****error****" + System.Environment.NewLine + DateTime.Now.ToString() + System.Environment.NewLine + ex.Message + System.Environment.NewLine + ex.StackTrace;
      File.AppendAllText(Application.StartupPath + @"\application_data\error.txt", error);
      }
      catch
      {
      MessageBox.Show("Call analyzer has been tempered with.Please contact author", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Stop);
      }
      }
      finally
      {
      if (renamegroupcon.State == ConnectionState.Open)
      renamegroupcon.Close();
       
      refreshtree();
      treeexchange.Nodes[0].Expand();
      foreach (TreeNode n in treeexchange.Nodes[0].Nodes)
      if (n.Text == e.Label)
      n.Expand();
      }
      The refreshtree function is as before.

      Code:
       
      {
                 treeexchange.Nodes.Clear();
                  TreeNode extensionsnode = new TreeNode("Extensions");
                  treeexchange.Nodes.Add(extensionsnode);
                  DataTable dtgroups = new DataTable(); //get groups
                  OleDbDataAdapter adp = new OleDbDataAdapter("select group_name,group_date_of_creation ,group_no_of_extensions from t_extensiongroups", @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Application.StartupPath + @"\exchangesandtrunks.mdb;Persist Security Info=False");
                  adp.Fill(dtgroups);
                  //when the user points to this it shows the details of the extension
                  extensionsnode.ToolTipText = "No of groups: " + dtgroups.Rows.Count.ToString();
                  int totalextensions = 0;
                  for (int i = 0; i < dtgroups.Rows.Count; i++)//for all the groups
                  {
                      //create a groupnode with the group name in it
                      TreeNode groupnode = new TreeNode(dtgroups.Rows[i]["group_name"].ToString());
                      groupnode.ToolTipText = "Group name: " + dtgroups.Rows[i]["group_name"].ToString();
      
                      //get no of extensions corresposnding to that group
                      adp = new OleDbDataAdapter("select ext_name,group_name,ext_number,ext_date_of_creation from t_extensions where group_name='" + dtgroups.Rows[i]["group_name"].ToString() + "'", @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Application.StartupPath + @"\exchangesandtrunks.mdb;Persist Security Info=False");
                      DataTable dtextensions = new DataTable("Extensions");
                      adp.Fill(dtextensions);
                      int totalgrpextensions = 0;
                      //add all the extension nodes to the groupnode
                      //
                      for (int j = 0; j < dtextensions.Rows.Count; j++)
                      {
                          TreeNode extensionnode = new TreeNode(dtextensions.Rows[j]["ext_number"].ToString() + " (" + dtextensions.Rows[j]["ext_name"].ToString()+ ")");
                          groupnode.Nodes.Add(extensionnode);
                          extensionnode.ToolTipText = "Extension name: " + dtextensions.Rows[j]["ext_name"].ToString() + "\nExtension no: " + dtextensions.Rows[j]["ext_number"].ToString() + "\nExtension group: " + dtextensions.Rows[j]["group_name"].ToString() + "\nCreated: " + dtextensions.Rows[j]["ext_date_of_creation"].ToString();
                          totalextensions++;
                          totalgrpextensions++;//for setting the tool tip groups wise
                      }
                      //add the groupnode
                      extensionsnode.Nodes.Add(groupnode);
                      //add the tooltip to this groupnode
                      groupnode.ToolTipText += "\nNo of extensions: " + totalgrpextensions.ToString() + "\n" + "Created: " + dtgroups.Rows[i]["group_date_of_creation"].ToString();
                  }
      
                  //got all the details about the extension now fill it !
                  extensionsnode.ToolTipText += "\nNo of extensions: " + totalextensions.ToString();
      
                  //create a trunk node
                  TreeNode trunksnode = new TreeNode("Trunks");
                  //get trunks
                  adp = new OleDbDataAdapter("select trunk_connected_trunk,trunk_name,trunk_virtual_number,trunk_date_of_creation from t_trunk_lines", @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Application.StartupPath + @"\exchangesandtrunks.mdb;Persist Security Info=False");
                  DataTable dttrunks = new DataTable("Trunks");
                  adp.Fill(dttrunks);
                  //add all trunks to the "trunks" node
                  for (int i = 0; i < dttrunks.Rows.Count; i++)
                  {
                      TreeNode trunknode = new TreeNode(dttrunks.Rows[i]["trunk_virtual_number"].ToString() + " (" + dttrunks.Rows[i]["trunk_connected_trunk"].ToString() + ")");
                      trunksnode.Nodes.Add(trunknode);
                      trunknode.ToolTipText = "Trunk name: " + dttrunks.Rows[i]["trunk_name"].ToString() + "\nConnected trunk: " + dttrunks.Rows[i]["trunk_connected_trunk"].ToString() + "\nVirtual trunk no:" + dttrunks.Rows[i]["trunk_virtual_number"].ToString() + "\nCreated:" + dttrunks.Rows[i]["trunk_date_of_creation"].ToString();
                  }
                  //add the trunk node
                  treeexchange.Nodes.Add(trunksnode);
                  //add the tag to this "trunks" node
                  trunksnode.ToolTipText = "Number of trunks: " + dttrunks.Rows.Count.ToString();
                  //add the pbx node
                  TreeNode pbxnode = new TreeNode("Settings");
                  pbxnode.ToolTipText = "Serial port and other settings";
                  treeexchange.Nodes.Add(pbxnode);
                  //add the User node
                  TreeNode usernode = new TreeNode("User");
                  usernode.ToolTipText = "User";
                  treeexchange.Nodes.Add(usernode);
      }
      If I remove those three lines it gets messed up even when i am writing
      treeexchange.No des.Clear(); in the function.

      Can anyone point some light on it ?

      Also is there are other event where i can call refreshtree() ??
      After label edit fires before the changes are committed on the treeview.
      I just need to cancel all editing and then call refreshtree() after all database updating has been done.

      Comment

      Working...