2 out of 10 rated this helpful - Rate this topic

Compiler Error CS0103

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

An attempt was made to use a name that does not exist in the class, namespace, or scope. Check the spelling of the name and check your using directives and assembly references to make sure that the name that you are trying to use is available.

This error frequently occurs if you declare a variable in a loop or a try or if block and then attempt to access it from an enclosing code block or a separate code block, as shown in the following example.

using System;

class MyClass1
{
    public static void Main()
    {
        try
        {
            // The following declaration is only available inside the try block.
            MyClass1 conn = new MyClass1();
        }
        catch (Exception e)
        {
            // The following expression causes error CS0103, because variable
            // conn only exists in the try block.
            if (conn != null) 
                Console.WriteLine("{0}", e);
        }
    }
}

The following example resolves the error.


using System;

class MyClass2
{
    public static void Main()
    {
        // To resolve the error in the example, the first step is to 
        // move the declaration of conn out of the try block. The following
        // declaration is available throughout the Main method.
        MyClass2 conn = null;
        try
        {
            // Inside the try block, use the conn variable that you declared
            // previously.
            conn = new MyClass2();
        }
        catch (Exception e)
        {
            // The following expression no longer causes an error, because
            // the declaration of conn is in scope.
            if (conn != null) 
                Console.WriteLine("{0}", e);
        }
    }
}


Did you find this helpful?
(1500 characters remaining)
Community Content Add
Annotations FAQ
Weird Errors
Hi

I'm working on a silverlight 3 application and I'm having kind of weird problems in the code for a login window.

Here's the code for login.xaml:

<controls:ChildWindow x:Class="network.login"
           xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
           xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
           xmlns:controls="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls"
           Width="200" Height="160"
           Title="Log In" Name="LoginForm" xmlns:dataInput="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Data.Input">
    <Grid x:Name="LayoutRoot" Margin="2">
        <Grid.RowDefinitions>
        </Grid.RowDefinitions>

        <Button x:Name="CancelButton" Content="Cancel" Click="CancelButton_Click" Height="23" Margin="97,0,12,12" VerticalAlignment="Bottom" />
        <Button x:Name="OKButton" Content="Log In" Click="OKButton_Click" Height="23" Margin="12,0,93,12" VerticalAlignment="Bottom" />
        <dataInput:Label Height="19" Margin="12,30,93,0" x:Name="userNameLabel" VerticalAlignment="Top" Content="User Name:" />
        <TextBox Height="19" Margin="97,30,12,0" x:Name="username" VerticalAlignment="Top" />
        <dataInput:Label Height="19" Margin="12,55,0,0" x:Name="passwordLabel" VerticalAlignment="Top" Content="Password:" HorizontalAlignment="Left" Width="79" />
        <PasswordBox Height="19" Margin="97,55,12,0" x:Name="password" VerticalAlignment="Top" />
    </Grid>
</controls:ChildWindow>

Here's the code-behind login.xaml.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Browser;

namespace network
{
    public partial class login : ChildWindow
    {
        public login()
        {
            InitializeComponent();
        }
        private void OKButton_Click(object sender, RoutedEventArgs e)
        {
            ScriptObject login = (ScriptObject)HtmlPage.Document.Body.GetProperty("checkLogin");
            bool result = (bool)login.InvokeSelf(username.Text, password.Password);
            if (result)
            {
                DialogResult = true;
            }
            else
            {
                DialogResult = false;
            }
            Close();
        }
        private void CancelButton_Click(object sender, RoutedEventArgs e)
        {
           
        }
    }
}

The JavaScript method 'checklogin' calls a server application in the background. Here's what the bottom of my error list looks like:



The code that I underlined caused the errors
insert user information from wizard to database error "the name 'identifier' does not exist ..."

Hi guys, Im a PHP programmer and now I'm try to do some work with ASP.NET but I got into trouble just in the first stage of my application.

Can Anyone help me find out how to get the user inserted values from CreateUserWizard and insert it to sql database. I have implemented using the following codes, but I keep getting the error message "The name 'identifier' does not exist in the current context". I have tried different approaches but any of them worked.

Codebehind: .aspx.cs using System;

using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Data.SqlTypes;
using System.Text;
using System.Configuration;
using System.Data.Sql;


namespace PaperReviewSystem.Account
{
public partial class Register : System.Web.UI.Page
{
public string GetConnectionString()
{
return System.Configuration.ConfigurationManager.ConnectionStrings["ApplicationServices"].ConnectionString;
}

protected void Page_Load(object sender, EventArgs e)
{
RegisterUser.ContinueDestinationPageUrl = Request.QueryString["ReturnUrl"];
}

protected void RegisterUser_CreatedUser(object sender, WizardNavigationEventArgs e)
{
//FormsAuthentication.SetAuthCookie(RegisterUser.UserName, false /* createPersistentCookie */);
try
{
SqlConnection con = new SqlConnection(GetConnectionString());
SqlCommand cmd = new SqlCommand(" INSERT INTO [peerReview].[users]([username], [userPassword], [firstname], [lastname], [email]) VALUES(@userName, @passWord, @firstName, @lastName, @email)", con);

//cmd.CommandType= CommandType.Text;
cmd.Parameters.AddWithValue("@userName", UserName.Text);
cmd.Parameters.AddWithValue("@passWord", FirstName.Text);
cmd.Parameters.AddWithValue("@firstName", LastName.Text);
cmd.Parameters.AddWithValue("@lastName", Email.Text);
cmd.Parameters.AddWithValue("@email", Password.Text);
con.Open();
cmd.ExecuteNonQuery();
con.Close();

string continueUrl = RegisterUser.ContinueDestinationPageUrl;
if (String.IsNullOrEmpty(continueUrl))
{
continueUrl = "~/";
}
Response.Redirect(continueUrl);
}
catch(Exception ex)
{
Response.Write(ex.Message);
}
}
}
}

Any help is welcome,

Thanks in advance!



Edit from SJ at MSFT: The Forums are a better place to look for this sort of help. Here is a place to start: http://forums.asp.net/.