Solved

C# Updating public static string

Posted on 2008-06-18
17
334 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
  • 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
 

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
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
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:
anuragal 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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

744 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now