[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

C# Variable doesn't seem to get set

Posted on 2003-12-09
7
Medium Priority
?
1,356 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 375 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

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 375 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

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

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

In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
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…
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…

649 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