Solved

C# Updating public static string

Posted on 2008-06-18
17
350 Views
Last Modified: 2010-04-21
I have a few public static string that derive from a Dataset on my Master Page and they work just fine
I have a page that up dates the these values in the data base however when I up date the page it still shows the old values like it is in so kind of state.

I know how to solve this problem if the update and select query was in the same page but the Update query is on one page and the Select query is on the master page.

How can I get the Select query to to show the upaded data from the data base see code below.
//////////The Masterpage///////////////////////////////////////////////
public partial class MasterPage : System.Web.UI.MasterPage
{
    public class userinfo
    {
        public const string appname = "MyDesktop 2.0";
 
        // DataSet ////////////////////////////////////////
        private static DataSet memberData = null;
 
        private static void LoadData()
        {
            string connectionString = ConfigurationManager.ConnectionStrings["MyAccess"].ConnectionString;
            OleDbConnection dbConnection = new OleDbConnection(connectionString);
            OleDbCommand cmd = new OleDbCommand("select * from userinfo", dbConnection);
 
            OleDbDataAdapter da = new OleDbDataAdapter(cmd);
            memberData = new DataSet();
            da.Fill(memberData);
        }
        // DataSet ///////////////////////////////////////
 
        public static string fname
        {
            get
            {
                if (memberData == null) LoadData();
                if (memberData.Tables[0].Rows.Count != 0)
                {
                    if (memberData.Tables[0].Rows[0]["fname"].ToString().Length == 0)
                    {
                        return "fname";
                    }
                    else
                    {
                        return memberData.Tables[0].Rows[0]["fname"].ToString();
                    }
                }
                else
                {
                    return "Herbert";
                }
            }
        }
 
//////////The update page///////////////////////////////////////////////
 protected void Button1_Click(object sender, EventArgs e)
    {
        String fname = tb_fname.Text;
        String lname = tb_lname.Text;
        String email = tb_email.Text;
        String uid = tb_uid.Text;
        String sql_user = tb_sql_user.Text;
        String sql_pass = tb_sql_pass.Text;
 
        string connectionString = ConfigurationManager.ConnectionStrings["MyAccess"].ConnectionString;
        OleDbConnection dbConnection = new OleDbConnection(connectionString);
        dbConnection.Open();
        OleDbCommand cmd2 = new OleDbCommand("delete from userinfo", dbConnection);
        OleDbCommand cmd = new OleDbCommand("insert into userinfo (fname,lname,email,uid,sql_user,sql_pass) values (@fname,@lname,@email,@uid,@sql_user,@sql_pass)", dbConnection);
        cmd.Parameters.AddWithValue("@fname", fname);
        cmd.Parameters.AddWithValue("@lname", lname);
        cmd.Parameters.AddWithValue("@email", email);
        cmd.Parameters.AddWithValue("@uid", uid);
        cmd.Parameters.AddWithValue("@sql_user", sql_user);
        cmd.Parameters.AddWithValue("@sql_pass", sql_pass);
 
        cmd2.ExecuteNonQuery();
        cmd.ExecuteNonQuery();
        dbConnection.Close();
 
        Label10.Text = "Your User Information has been updated. You may need to restart this app. for changes to take effect.";
    }

Open in new window

0
Comment
Question by:quest_capital
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 5
  • 4
  • +1
17 Comments
 
LVL 14

Expert Comment

by:ajitha75
ID: 21814933
Make the LoadData() method a public method and call it from content page as

Master.LoadData()

after updating the database
0
 

Author Comment

by:quest_capital
ID: 21815381
ajitha75:
I did that and it still does not update the MasterPage Data.
0
 
LVL 18

Expert Comment

by:jcoehoorn
ID: 21815617
The class I provided for you included a .Reset() method you could call for this purpose.  To use that method just call userinfo.Reset();  
0
Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

 

Author Comment

by:quest_capital
ID: 21816146
jcoehoorn:

I tryed that and it did not up date the data on the Master page


///////////////////////////////// Updaa page
MasterPage.userinfo.Refresh();
 
///////////////////////////////// MasterPage
public static void Refresh()
        {
            memberData = null;
        }

Open in new window

0
 
LVL 18

Expert Comment

by:jcoehoorn
ID: 21816352
Remember, with an ASP.Net page lifecycle your button click code runs _after_ the other things like a page load or property initialization.  So if you call userinfo.Refresh()  after your button click code most of the data for display on your page was already set, and will show old values.  
0
 
LVL 18

Expert Comment

by:jcoehoorn
ID: 21816364
Here's a page lifecycle overview:
http://msdn.microsoft.com/en-us/library/ms178472.aspx

To see if this is really what's going on do something on the page to cause another postback and you should see the new data.
0
 

Author Comment

by:quest_capital
ID: 21816552
jcoehoorn:

I get the conept by I don't know how to implement it.

No matter how maany time I click on the refresh button it will not update I have to close IE and open it back up again before the new info show.
0
 
LVL 18

Expert Comment

by:jcoehoorn
ID: 21816595
Try holding down the shift key while clicking the refresh button with your mouse to force the browser to create a new request. Otherwise you might be seeing a cached page.

The thing to remember here is that you need to manually update the anything that changed on your page _after your button click function refreshes the data_.
0
 
LVL 14

Expert Comment

by:ajitha75
ID: 21816726
where exactly are you trying to see the difference - in the textboxes in the content page or in the value returned by fname property in MasterPage.userinfo?
0
 

Author Comment

by:quest_capital
ID: 21817462
ajitha75:

I see the change in the updated fields which comes from the MasterPage
However no update on the info on the MasterPage it self.


/////////////// Update Page
protected void Button1_Click(object sender, EventArgs e)
    {
        String fname = tb_fname.Text;
        String lname = tb_lname.Text;
        String email = tb_email.Text;
        String uid = tb_uid.Text;
        String sql_user = tb_sql_user.Text;
        String sql_pass = tb_sql_pass.Text;
 
        string connectionString = ConfigurationManager.ConnectionStrings["MyAccess"].ConnectionString;
        OleDbConnection dbConnection = new OleDbConnection(connectionString);
        dbConnection.Open();
        OleDbCommand cmd2 = new OleDbCommand("delete from userinfo", dbConnection);
        OleDbCommand cmd = new OleDbCommand("insert into userinfo (fname,lname,email,uid,sql_user,sql_pass) values (@fname,@lname,@email,@uid,@sql_user,@sql_pass)", dbConnection);
        cmd.Parameters.AddWithValue("@fname", fname);
        cmd.Parameters.AddWithValue("@lname", lname);
        cmd.Parameters.AddWithValue("@email", email);
        cmd.Parameters.AddWithValue("@uid", uid);
        cmd.Parameters.AddWithValue("@sql_user", sql_user);
        cmd.Parameters.AddWithValue("@sql_pass", sql_pass);
 
        cmd2.ExecuteNonQuery();
        cmd.ExecuteNonQuery();
        dbConnection.Close();
 
        MasterPage.userinfo.Refresh();
        MasterPage.userinfo.LoadData();
        Label10.Text = "Your User Information has been updated. You may need to restart this app. for changes to take effect.";
    }
 
////////////// MasterPage
protected void Page_Load(object sender, EventArgs e)
    {
        MasterPage.userinfo.Refresh();
        Label1.Text = "<a href=project_user.aspx>" + userinfo.name.ToString() + "</a>";
    }

Open in new window

0
 
LVL 14

Expert Comment

by:ajitha75
ID: 21818692
I am sorry. I did not get it. what did you mean by "However no update on the info on the MasterPage it self". Which info on the master page is not updated.. you have shown userinfo.name.ToString()... do you have a name property in userinfo?
0
 

Author Comment

by:quest_capital
ID: 21818870
ajitha75:

name is fname + lname it is called on the Pageload of the Masterpage
0
 
LVL 11

Accepted Solution

by:
Anurag Agarwal earned 250 total points
ID: 21819043
When you are concating fname + lname... please make sure that either of the 2 should not be null otherwise the result of concatenation will also be null.

Try your code by returning only fname value in property name then see. (I mean for testing don't concatenate just return fname).

Anurag
0
 
LVL 14

Assisted Solution

by:ajitha75
ajitha75 earned 250 total points
ID: 21821083
Ok.... you cannot refresh in Page_Load ... so write a method in Master Page (not in userinfo) and call it in button_click ...

/////In MasterPage.cs
    public void Reload()
    {
        MasterPage.userinfo.LoadData();
        Label1.Text = "<a href=project_user.aspx>" + userinfo.name.ToString() + "</a>";
    }


/////in ContentPage
/////////////// Update Page
protected void Button1_Click(object sender, EventArgs e)
    {
        String fname = tb_fname.Text;
        String lname = tb_lname.Text;
        String email = tb_email.Text;
        String uid = tb_uid.Text;
        String sql_user = tb_sql_user.Text;
        String sql_pass = tb_sql_pass.Text;
 
        string connectionString = ConfigurationManager.ConnectionStrings["MyAccess"].ConnectionString;
        OleDbConnection dbConnection = new OleDbConnection(connectionString);
        dbConnection.Open();
        OleDbCommand cmd2 = new OleDbCommand("delete from userinfo", dbConnection);
        OleDbCommand cmd = new OleDbCommand("insert into userinfo (fname,lname,email,uid,sql_user,sql_pass) values (@fname,@lname,@email,@uid,@sql_user,@sql_pass)", dbConnection);
        cmd.Parameters.AddWithValue("@fname", fname);
        cmd.Parameters.AddWithValue("@lname", lname);
        cmd.Parameters.AddWithValue("@email", email);
        cmd.Parameters.AddWithValue("@uid", uid);
        cmd.Parameters.AddWithValue("@sql_user", sql_user);
        cmd.Parameters.AddWithValue("@sql_pass", sql_pass);
 
        cmd2.ExecuteNonQuery();
        cmd.ExecuteNonQuery();
        dbConnection.Close();
 
         ((MasterPage)Master).Reload();
        Label10.Text = "Your User Information has been updated. You may need to restart this app. for changes to take effect.";
    }


0
 

Author Comment

by:quest_capital
ID: 21821826
ajitha75:
Error: 'MasterPage' does not contain a definition for 'Reload' and no extension method 'Reload' accepting a first argument of type 'MasterPage' could be found (are you missing a using directive or an assembly reference?)
0
 
LVL 14

Expert Comment

by:ajitha75
ID: 21821918
could you paste your full code here?
0
 

Author Closing Comment

by:quest_capital
ID: 31468471
Just returning the fname instead of the combinded name worked.
also
the MasterPage.userinfo.LoadData();
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

739 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question