Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8736409
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T10:17:51+00:00 2026-06-13T10:17:51+00:00

This is probably the biggest noob question ever but I’m confused as to why

  • 0

This is probably the biggest noob question ever but I’m confused as to why my private variables, when they are set in one method, gets reset in another. I have something like this in my code:

namespace Project.Messages
{
public partial class Inbox : System.Web.UI.Page
{

    private static int selectedIndex;
    private static string messageIDString;
    private static int messageID; 

    //select message to view
    protected void viewMessage(object sender, GridViewCommandEventArgs e)
    {
            //get index
            selectedIndex = MsgInbox.SelectedIndex;
            if (int.TryParse(e.CommandArgument.ToString(), out selectedIndex))
            {
                //get selected dataKey, convert to a int
                messageIDString = MsgInbox.DataKeys[selectedIndex].Value.ToString();
                messageID = Convert.ToInt16(messageIDString);
            }
     }

    //select message to delete
    protected void delBTN_Click(object sender, EventArgs e)
    {

        SqlCommand com = new SqlCommand("DELETE FROM Messages WHERE MessageID =    @param1", conn);
        conn.Open();

        com.Parameters.AddWithValue("param1", messageID);
    }

So if I click a message, messageID will be set and the message will be displayed. When I click to delete message after that though, it looks like the variable is resetting/is not the same value as previously set. Do I need to use a static variable or something to achieve this?

Thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T10:17:52+00:00Added an answer on June 13, 2026 at 10:17 am

    That is the behaviour. When there is postback, all variables are reset and reassigned. You can use session or viewstate or store the value in a control on the page which is already part of the viewstate e.g. in an hidden field

    public int messageID
    {
       get 
       { 
          int retVal = 0;
          if(ViewState["messageID"] != null)
             Int32.TryParse(ViewState["messageID"].ToString(), out retVal); 
    
          return retVal;
       }
       set { ViewState["messageID"] = value; }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This probably another silly question - but is there an MKMapViewDelegate method that can
This probably is a dummy question but I cannot find a clear indication. I
Im sorry for this probably dumm question, but I want to simply open modals
This probably isn't a hard one but couldn't find any info on it. HTML5
This question is probably quite dumb, but I can't find an example and can't
I know this probably must be a newbie question, but since I don't know
This probably has a one click solution, i know debugging web pages in VS2010
This probably sounds like a terrible idea at first glance, but here is my
I know this probably really simple but Im not sure what im doing wrong...
I know this probably has been asked before but I am having issues with

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.