Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 414
  • Last Modified:

C# Updating public static string

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
quest_capital
Asked:
quest_capital
  • 7
  • 5
  • 4
  • +1
2 Solutions
 
ajitha75Commented:
Make the LoadData() method a public method and call it from content page as

Master.LoadData()

after updating the database
0
 
quest_capitalAuthor Commented:
ajitha75:
I did that and it still does not update the MasterPage Data.
0
 
Joel CoehoornDirector of Information TechnologyCommented:
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
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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.

 
quest_capitalAuthor Commented:
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
 
Joel CoehoornDirector of Information TechnologyCommented:
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
 
Joel CoehoornDirector of Information TechnologyCommented:
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
 
quest_capitalAuthor Commented:
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
 
Joel CoehoornDirector of Information TechnologyCommented:
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
 
ajitha75Commented:
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
 
quest_capitalAuthor Commented:
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
 
ajitha75Commented:
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
 
quest_capitalAuthor Commented:
ajitha75:

name is fname + lname it is called on the Pageload of the Masterpage
0
 
Anurag AgarwalPython DeveloperCommented:
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
 
ajitha75Commented:
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
 
quest_capitalAuthor Commented:
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
 
ajitha75Commented:
could you paste your full code here?
0
 
quest_capitalAuthor Commented:
Just returning the fname instead of the combinded name worked.
also
the MasterPage.userinfo.LoadData();
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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.

  • 7
  • 5
  • 4
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now