Solved

C# Variable doesn't seem to get set

Posted on 2003-12-09
7
1,346 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
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
Delivering innovative fully-managed cloud services for mission-critical applications requires expertise in multiple areas plus vision and commitment. Meet a few of the people behind the quality services of Concerto.
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

937 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

10 Experts available now in Live!

Get 1:1 Help Now