Asked by:
CS0103 C# The name does not exist in the current context

Question
-
User1201429566 posted
Hello everyone, I am not sure why am I getting the name 'listID' does now exist in the current context. As I am still new to ASP.NET, any help would be greatly appreciated! Thanks!my aspx.cs file using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Data; using System.Data.SqlClient; using System.Configuration; namespace CustomerHandyApplication { public partial class ServiceView : System.Web.UI.Page { // create a new class by initializing service ServiceInfo aService = new ServiceInfo(); ServiceInfo bService = new ServiceInfo(); ServiceInfo list = new ServiceInfo(); protected void Page_Load(object sender, EventArgs e) { // create instance where ServiceInfo = a ServiceInfo a = new ServiceInfo(); List listingsService = new List (); if (!IsPostBack) { //store the list of ServiceNo into the nameList List nameList = a.getListingNameAll(); List handyService = listingsService; } } // bind the details in the grid view together // create a new class ServiceList protected void bind() { List serviceData = new List(); serviceData = aService.getServiceInfoAll(); gvService.DataSource = serviceData; gvService.DataBind(); List serviceList = new List(); serviceList = bService.getListingNameAll(); gvService.DataSource = serviceList; gvService.DataBind(); List listingsService = new List(); listingsService = list.getListings(listID); gvService.DataSource = listingsService; gvService.DataBind(); } protected void gvService_SelectedIndexChanged(object sender, EventArgs e) { // Get the currently selected row // Display by the row GridViewRow row = gvService.SelectedRow; // Get Listing_Name from the selected row, // which is the first row, i.e. index 0 string listName = row.Cells[0].Text; decimal price = decimal.Parse(row.Cells[1].Text); string handyName = row.Cells[2].Text; Response.Redirect("Pre-BookAppointment.aspx"); } protected void ddl_List_SelectedIndexChanged(object sender, EventArgs e) { string listID = ddl_List.SelectedValue; Response.Redirect("ViewService.aspx?ListID=" + listID); bind(); } } } //my cs file: using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Data.SqlClient; using System.Configuration; using System.Data; namespace CustomerHandyApplication { public class ServiceInfo { //Private string _connStr = Properties.Settings.Default.DBConnStr; // set the values to each of the attributes //System.Configuration.ConnectionStringSettings _connStr; string _connStr = ConfigurationManager.ConnectionStrings["CustomerContext"].ConnectionString; // private attributes of attributes in the database table private string _listID = ""; private string _listName = ""; private decimal _price = 0; private string _handyName = ""; // Default constructor public ServiceInfo() { } // Constructor that take in all data required to build a ServiceInfo object // ServiceInfo class that takes 3 arguments // set the attributes to each of the variables by making it private public ServiceInfo(string listID, string listName, decimal price, string handyName) { _listID = listID; _listName = listName; _price = price; _handyName = handyName; } // Constructor that take in all except Listing_ID // create all variables as the attributes // ID – unique key of service = Listing_ID // auto constructor: it generates an auto listName from the database to access to all the attributes of the ServiceInfo table. // Hence it does not pass in the service_No, but the rest of the attributes in the table unless it is specified by the rules of the company. public ServiceInfo(string listName, decimal price, string handyName) : this("", "", price, handyName) { } // Constructor that take in only Listing_ID. The other attributes will be set to 0 or empty. public ServiceInfo(string listID) : this(listID, "", 0, "") { } // set the attributes under the object // by creating the class instances of each attributes in the object // create the Get/Set the attributes of the Product object. // Note the attribute name (e.g. Listing_Name) is same as the actual database field name. // This is for ease of referencing. public string Listing_ID { get { return _listID; } set { _listID = value; } } public string Listing_Name { get { return _listName; } set { _listName = value; } } public decimal Price { get { return _price; } set { _price = value; } } public string Handyman_Name { get { return _handyName; } set { _handyName = value; } } //Below as the Class methods for some DB operations. // getServiceInfo(Listing_Name) - to retrieve listName data from the gvService public ServiceInfo getSeviceInfo(string listID) { ServiceInfo handylistDetail = null; string list_Name,handy_Name; decimal price; // query the attributes by selecting each of the attributes in the CustomerContext database string queryStr = "SELECT * FROM Lisitngs WHERE Listing_ID = @ListID "; // request the connection by getting the database of the attributes from the CustomerContext database using the connStr variable SqlConnection conn = new SqlConnection(_connStr); SqlCommand cmd = new SqlCommand(queryStr, conn); cmd.Parameters.AddWithValue("@ListID", listID); // open the connection by starting to read and execute each of the attibutes in the CustomerContext database conn.Open(); SqlDataReader dr = cmd.ExecuteReader(); // read each of the attributes in the CustomerContext in the string format // exclude listID which is primary key if (dr.Read()) { list_Name = dr["Listing_Name"].ToString(); price = decimal.Parse(dr["Price"].ToString()); handy_Name = dr["Handyman_Name"].ToString(); handylistDetail = new ServiceInfo(listID, list_Name, price, handy_Name); } else { handylistDetail = null; } conn.Close(); dr.Close(); dr.Dispose(); return handylistDetail; } //end of retrieve // getServiceInfoAll()-to retrieve all the data from the gvService public List getServiceInfoAll() { List handyList = new List(); // include listName which is a primary key string list_ID, list_Name, handy_Name; decimal price; string queryStr = "SELECT * FROM Listings"; SqlConnection conn = new SqlConnection(_connStr); SqlCommand cmd = new SqlCommand(queryStr, conn); conn.Open(); SqlDataReader dr = cmd.ExecuteReader(); // include listName which is a primary key while (dr.Read()) { list_ID = dr["Listing_ID"].ToString(); list_Name = dr["Listing_Name"].ToString(); price = decimal.Parse(dr["Price"].ToString()); handy_Name = dr["Handyman_Name"].ToString(); ServiceInfo a = new ServiceInfo(list_ID, list_Name, price, handy_Name); handyList.Add(a); } conn.Close(); dr.Close(); dr.Dispose(); return handyList; } //getListingNameAll() - to retrieve all the data from the ddl_List public List getListingNameAll() { List nameList = new List(); string list_ID, list_Name, handy_Name; decimal price; string queryStr = "SELECT * FROM Listings"; SqlConnection conn = new SqlConnection(_connStr); SqlCommand cmd = new SqlCommand(queryStr, conn); conn.Open(); SqlDataReader dr = cmd.ExecuteReader(); // include listID and servNo which are primary keys while (dr.Read()) { list_ID = dr["Listing_ID"].ToString(); list_Name = dr["Listing_Name"].ToString(); price = decimal.Parse(dr["Price"].ToString()); handy_Name = dr["Handyman_Name"].ToString(); ServiceInfo a = new ServiceInfo(list_ID, list_Name, price, handy_Name); nameList.Add(a); } conn.Close(); dr.Close(); dr.Dispose(); return nameList; } //getListings() - to get the listID from the ddl_List public ServiceInfo getListings(string listID) { ServiceInfo handyService = null; string list_Name, handy_Name; decimal price; // query the attributes by selecting each of the attributes in the CustomerContext database string queryStr = "SELECT * FROM Lisitngs WHERE Listing_ID = @ListID "; // request the connection by getting the database of the attributes from the CustomerContext database using the connStr variable SqlConnection conn = new SqlConnection(_connStr); SqlCommand cmd = new SqlCommand(queryStr, conn); cmd.Parameters.AddWithValue("@ListID", listID); // open the connection by starting to read and execute each of the attibutes in the CustomerContext database conn.Open(); SqlDataReader dr = cmd.ExecuteReader(); // read each of the attributes in the CustomerContext in the string format // exclude listID which is primary key if (dr.Read()) { list_Name = dr["Listing_ID"].ToString(); price = decimal.Parse(dr["Price"].ToString()); handy_Name = dr["Handyman_Name"].ToString(); handyService = new ServiceInfo(listID, list_Name, price, handy_Name); } else { handyService = null; } conn.Close(); dr.Close(); dr.Dispose(); return handyService; } //end of retrieve
Sunday, February 14, 2021 2:23 PM
All replies
-
User1535942433 posted
Hi Teck Loong,
According to your codes,I have created a test and I find "listID" is a parameter of getListings() method. So you need to define the "listID". Just like this:
protected void bind() { List<ServiceInfo> serviceData = new List<ServiceInfo>(); serviceData = aService.getServiceInfoAll(); gvService.DataSource = serviceData; gvService.DataBind(); List<ServiceInfo> serviceList = new List<ServiceInfo>(); serviceList = bService.getListingNameAll(); gvService.DataSource = serviceList; gvService.DataBind(); List<ServiceInfo> listingsService = new List<ServiceInfo>(); string listID = ""; listingsService.Add(list.getListings(listID)); gvService.DataSource = listingsService; gvService.DataBind(); }
By the way, please use {;} to post your codes to make it as a code format.
Best regards,
Yijing Sun
Monday, February 15, 2021 4:00 AM -
User753101303 posted
Hi,
You have a {;} button in the edit bar so that you can include code in a much more legible way. See https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/compiler-messages/cs0103
It means that you are trying to use an identifier which is not declared. The actual name should be part of the error message.
Monday, February 15, 2021 10:29 AM