Solved

C# Variable doesn't seem to get set

Posted on 2003-12-09
7
1,349 Views
Last Modified: 2011-10-03
Hello, in my C# code, I have the following:

public class someClass : Page {
  private string idKey;

  override protected void OnInit(...) {
     (stuff)
  }

  onCommand_CallSQLCommand() {
    sqlConnection.Open();
    (Run SQL query)
    SqlDataAdapter.Read();
    idKey = SqlDataAdapter.GetValue(0).ToString();
    sqlConnection.Close();
  }

  onCommand_CallSQLCommand2() {
    string queryString = "Select * FROM someTable WHERE id = '" + idKey + "'";
   (Run queryString through to SQL)
  }
}

The problem is that once I have ran sqlConnection.Close();, my "idKey" variable seems to lose it's value.  How do I get it to stay around?  

Also, onCommand_CallSQLCommand and onCommand_CallSQLCommand2 are both Command methods for separate ImageButtons on my .aspx page.

Thanks for any help.
0
Comment
Question by:whoiswho
[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 Comments
 
LVL 6

Assisted Solution

by:DaniPro
DaniPro earned 125 total points
ID: 9910524
Did you try to show the idKey before the Close of connection and after the close of the connection?
I think that the problem isn't the close method
0
 

Expert Comment

by:sentosa_gani
ID: 9910527
Are you sure 100% that in the line
idKey = SqlDataAdapter.GetValue(0).ToString()
has assigned the value to idkey appropriately?

I suspect that the command assigned a "" or empty value into idKey
And I think rather than using SqlDataAdapter, you might want to consider using SqlDataReader as DataAdapter is consuming more memory resources.

SqlCommand cmd = new SqlCommand(query,sqlConnection);
SqlDataReader sdr = cmd.ExecuteReader();
sdr.Read();
idKey = sdr[0].ToString();
sdr.Close();
sqlConnection.Close();

Hopefully I answer your question =)
0
 
LVL 10

Expert Comment

by:Duy Pham
ID: 9910581
Hi whoiswho,
SqlDataAdapter class does not have Read(), and GetValue() methods. Please check your code and maybe everyone could help you easier.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:whoiswho
ID: 9910839
Sorry I meant to write DataReader and I am using ODBC though I don't see how that could be a problem because it seems to read all other data just fine.
I'm not sure if it is the Close() method is the cause but I am sure that it has assigned the idKey appropriately because I output the value to a Label in my .aspx page and it did show it.

Here's a more detailed corrected version of my code:
public class someClass : Page {
  OdbcConnection odbcConnection;
  OdbcCommand odbcCommand;
  OdbcDataReader odbcDataReader;
  private string idKey;

  protected ImageButton webButton01, webButton02;

  override protected void OnInit(...) {
     odbcConnection = new OdbcConnection(...connection string...);
    webButton01 += new CommandEventHandler(onCommand_CallSQLCommand);
    webButton02 += new CommandEventHandler(onCommand_CallSQLCommand2);
  }

  onCommand_CallSQLCommand() {  //Method attached to webButton01
    string querystring = "Select * from someTable where name = \"mary\"";
    try {
      odbcCommand = new OdbcCommand(queryString, odbcConnection);
      odbcCommand.CommandType = CommandType.Text;
      odbcConnection.Open();
      odbcDataReader = odbcCommand.ExecuteReader();

      if (odbcDataReader.HasRows) {
        idKey = odbcDataReader.GetValue(0).ToString();
        //Also tried idKey = (string)(odbcDataReader.GetValue(0))
        //When I also add the line webLabel.Text = odbcDataReader.GetValue(0).ToString() in here, I get the value on my webpage
      }
      odbcConnection.Close();
      //I also tried inserting the line webLabel.Text = odbcDataReader.GetValue(0).ToString() here and it also shows on the page.
    }  catch (InvalidCastException cast) {

    }
 }

  onCommand_CallSQLCommand2() {
     //Just to simplify, I entered the following line and nothing came up when I click on webButton02 after I clicked on webButton01
     webLabel.Text = idKey;
  }
}

So I guess the value in idKey has somehow lost it's value after the end of the onCommand_CallSQLCommand() method.  I haven't got a clue why this is happening.  Is it because I'm also using OnInit()?  I usually use Page_Init but have never figured out what the difference between those two are.
0
 
LVL 6

Accepted Solution

by:
DaniPro earned 125 total points
ID: 9911260
In the OnInit event the server control initialize all the istance of the class .
I suggest you of use Load event of your page in witch you can verify also the IsPostBack property.
0
 

Author Comment

by:whoiswho
ID: 9916291
Hmm, ok, it turns out I was under the assumption that the class variables would stay around even after the page request has been processed which is not the case.  Any variables not  in the class gets lost since it is only valid for each page request, unless it is passed to the .aspx page through webcontrols or other state management items such as session or application.  So I just put the idKey in a session instead.
0
 

Author Comment

by:whoiswho
ID: 9916314
Hmm, ok, it turns out I was under the assumption that the class variables would stay around even after the page request has been processed which is not the case.  Any variables not  in the class gets lost since it is only valid for each page request, unless it is passed to the .aspx page through webcontrols or other state management items such as session or application.  So I just put the idKey in a session instead.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

749 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