Solved

C# Variable doesn't seem to get set

Posted on 2003-12-09
7
1,343 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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
This video discusses moving either the default database or any database to a new volume.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

746 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

15 Experts available now in Live!

Get 1:1 Help Now