[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

C# Variable doesn't seem to get set

Posted on 2003-12-09
7
Medium Priority
?
1,360 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 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
High user turnover can cause old/redundant user data to consume valuable space. UserResourceCleanup was developed to address this by automatically deleting user folders when the user account is deleted.
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
Suggested Courses
Course of the Month19 days, 13 hours left to enroll

873 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