Avatar of angelsofgrace2006
angelsofgrace2006Flag for Portugal

asked on 

The name 'ID' does not exist in the current context

Hi,

I have this C#/ASP.NET .aspx page.

I have the following relevant code at the beginning:

<script runat="server">

public string ID="";

protected override void OnLoad(EventArgs e) {
 ID= "Mario";   // ID must be set here

(...)

}

</script>

After this, I have a form.

But in the end of the page, I need to know the ID value.
Problem is I get the error "The name 'ID' does not exist in the current context".

Does anyone know how can I get it ?

Any help would be appreciated.

Warm Regards,
Mário Gamito
.NET ProgrammingC#

Avatar of undefined
Last Comment
philipjonathan
ASKER CERTIFIED SOLUTION
Avatar of bradley2u
bradley2u

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of angelsofgrace2006

ASKER

Hi,

Thank you for your answer.

The ID comes from the following SELECT on a MS SQL SERVER as shown.
It works just right, but I can't access its value outside this part of the code:

<script runat="server">

protected override void OnLoad(EventArgs e) {


int ID = -1;

SqlConnection myConn = new SqlConnection("Data Source=192.168.1.50"; Initial Catalog=mydb; User=123; Password=123");


try
{
myConn.Open();
SqlCommand myCommand = new SqlCommand("SELECT MAX(PostID) from dbo.Posts", myConn);
ID = (int)myCommand.ExecuteScalar();
}
finally
{
if (myConn != null)
 {
   myConn.Close();
  }
}


}

</script>


Warm Regards,
Mário Gamito
SOLUTION
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
.NET Programming
.NET Programming

The .NET Framework is not specific to any one programming language; rather, it includes a library of functions that allows developers to rapidly build applications. Several supported languages include C#, VB.NET, C++ or ASP.NET.

137K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo