Name does not exist in the current context

Asked

Viewed 1,093 times

0

I’m in the fight here on this project with asp.net, but the classes are right as I took a tutorial here on the net, they don’t give any error only when running the project ai shows.

CS0103 error The name "gvFiles" does not exist in the current context

have the class CS.aspx.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using System.IO;
using System.Net;
using System.Data;

public partial class CS : System.Web.UI.Page
{


    protected void Page_Load(object sender, EventArgs e)
    {
        //FTP Server URL.
        string ftp = "ftp://10.0.0.101:2323";

        //FTP Folder name. Leave blank if you want to list files from root folder.
        string ftpFolder = "Uploads/";

        try
        {
            //Create FTP Request.
            FtpWebRequest request = (FtpWebRequest)WebRequest.Create(ftp + ftpFolder);
            request.Method = WebRequestMethods.Ftp.ListDirectoryDetails;

            //Enter FTP Server credentials.
           // request.Credentials = new NetworkCredential("Username", "Password");
            request.UsePassive = true;
            request.UseBinary = true;
            request.EnableSsl = false;

            //Fetch the Response and read it using StreamReader.
            FtpWebResponse response = (FtpWebResponse)request.GetResponse();
            List<string> entries = new List<string>();
            using (StreamReader reader = new StreamReader(response.GetResponseStream()))
            {
                //Read the Response as String and split using New Line character.
                entries = reader.ReadToEnd().Split(new string[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries).ToList();
            }
            response.Close();

            //Create a DataTable.
            DataTable dtFiles = new DataTable();
            dtFiles.Columns.AddRange(new DataColumn[3] { new DataColumn("Name", typeof(string)),
                                                    new DataColumn("Size", typeof(decimal)),
                                                    new DataColumn("Date", typeof(string))});

            //Loop and add details of each File to the DataTable.
            foreach (string entry in entries)
            {
                string[] splits = entry.Split(new string[] { " ", }, StringSplitOptions.RemoveEmptyEntries);

                //Determine whether entry is for File or Directory.
                bool isFile = splits[0].Substring(0, 1) != "d";
                bool isDirectory = splits[0].Substring(0, 1) == "d";

                //If entry is for File, add details to DataTable.
                if (isFile)
                {
                    dtFiles.Rows.Add();
                    dtFiles.Rows[dtFiles.Rows.Count - 1]["Size"] = decimal.Parse(splits[4]) / 1024;
                    dtFiles.Rows[dtFiles.Rows.Count - 1]["Date"] = string.Join(" ", splits[5], splits[6], splits[7]);
                    string name = string.Empty;
                    for (int i = 8; i < splits.Length; i++)
                    {
                        name = string.Join(" ", name, splits[i]);
                    }
                    dtFiles.Rows[dtFiles.Rows.Count - 1]["Name"] = name.Trim();
                }
            }

            //Bind the GridView.
            gvFiles.DataSource = dtFiles;
            gvFiles.DataBind();
        }
        catch (WebException ex)
        {
            throw new Exception((ex.Response as FtpWebResponse).StatusDescription);
        }
    }

    protected void DownloadFile(object sender, EventArgs e)
    {
        string fileName = (sender as LinkButton).CommandArgument;

        //FTP Server URL.
        string ftp = "ftp://10.0.0.101:2323";

        //FTP Folder name. Leave blank if you want to Download file from root folder.
        string ftpFolder = "Uploads/";

        try
        {
            //Create FTP Request.
            FtpWebRequest request = (FtpWebRequest)WebRequest.Create(ftp + ftpFolder + fileName);
            request.Method = WebRequestMethods.Ftp.DownloadFile;

            //Enter FTP Server credentials.
           // request.Credentials = new NetworkCredential("Username", "Password");
            request.UsePassive = true;
            request.UseBinary = true;
            request.EnableSsl = false;

            //Fetch the Response and read it into a MemoryStream object.
            FtpWebResponse response = (FtpWebResponse)request.GetResponse();
            using (MemoryStream stream = new MemoryStream())
            {
                //Download the File.
                response.GetResponseStream().CopyTo(stream);
                Response.AddHeader("content-disposition", "attachment;filename=" + fileName);
                Response.Cache.SetCacheability(HttpCacheability.NoCache);
                Response.BinaryWrite(stream.ToArray());
                Response.End();
            }
        }
        catch (WebException ex)
        {
            throw new Exception((ex.Response as FtpWebResponse).StatusDescription);
        }
    }
}

and I have the other class called CS.aspx

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using System.IO;
using System.Net;
using System.Data;

public partial class CS : System.Web.UI.Page
{


    protected void Page_Load(object sender, EventArgs e)
    {
        //FTP Server URL.
        string ftp = "ftp://10.0.0.101:2323";

        //FTP Folder name. Leave blank if you want to list files from root folder.
        string ftpFolder = "Uploads/";

        try
        {
            //Create FTP Request.
            FtpWebRequest request = (FtpWebRequest)WebRequest.Create(ftp + ftpFolder);
            request.Method = WebRequestMethods.Ftp.ListDirectoryDetails;

            //Enter FTP Server credentials.
           // request.Credentials = new NetworkCredential("Username", "Password");
            request.UsePassive = true;
            request.UseBinary = true;
            request.EnableSsl = false;

            //Fetch the Response and read it using StreamReader.
            FtpWebResponse response = (FtpWebResponse)request.GetResponse();
            List<string> entries = new List<string>();
            using (StreamReader reader = new StreamReader(response.GetResponseStream()))
            {
                //Read the Response as String and split using New Line character.
                entries = reader.ReadToEnd().Split(new string[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries).ToList();
            }
            response.Close();

            //Create a DataTable.
            DataTable dtFiles = new DataTable();
            dtFiles.Columns.AddRange(new DataColumn[3] { new DataColumn("Name", typeof(string)),
                                                    new DataColumn("Size", typeof(decimal)),
                                                    new DataColumn("Date", typeof(string))});

            //Loop and add details of each File to the DataTable.
            foreach (string entry in entries)
            {
                string[] splits = entry.Split(new string[] { " ", }, StringSplitOptions.RemoveEmptyEntries);

                //Determine whether entry is for File or Directory.
                bool isFile = splits[0].Substring(0, 1) != "d";
                bool isDirectory = splits[0].Substring(0, 1) == "d";

                //If entry is for File, add details to DataTable.
                if (isFile)
                {
                    dtFiles.Rows.Add();
                    dtFiles.Rows[dtFiles.Rows.Count - 1]["Size"] = decimal.Parse(splits[4]) / 1024;
                    dtFiles.Rows[dtFiles.Rows.Count - 1]["Date"] = string.Join(" ", splits[5], splits[6], splits[7]);
                    string name = string.Empty;
                    for (int i = 8; i < splits.Length; i++)
                    {
                        name = string.Join(" ", name, splits[i]);
                    }
                    dtFiles.Rows[dtFiles.Rows.Count - 1]["Name"] = name.Trim();
                }
            }

            //Bind the GridView.
            gvFiles.DataSource = dtFiles;
            gvFiles.DataBind();
        }
        catch (WebException ex)
        {
            throw new Exception((ex.Response as FtpWebResponse).StatusDescription);
        }
    }

    protected void DownloadFile(object sender, EventArgs e)
    {
        string fileName = (sender as LinkButton).CommandArgument;

        //FTP Server URL.
        string ftp = "ftp://10.0.0.101:2323";

        //FTP Folder name. Leave blank if you want to Download file from root folder.
        string ftpFolder = "Uploads/";

        try
        {
            //Create FTP Request.
            FtpWebRequest request = (FtpWebRequest)WebRequest.Create(ftp + ftpFolder + fileName);
            request.Method = WebRequestMethods.Ftp.DownloadFile;

            //Enter FTP Server credentials.
           // request.Credentials = new NetworkCredential("Username", "Password");
            request.UsePassive = true;
            request.UseBinary = true;
            request.EnableSsl = false;

            //Fetch the Response and read it into a MemoryStream object.
            FtpWebResponse response = (FtpWebResponse)request.GetResponse();
            using (MemoryStream stream = new MemoryStream())
            {
                //Download the File.
                response.GetResponseStream().CopyTo(stream);
                Response.AddHeader("content-disposition", "attachment;filename=" + fileName);
                Response.Cache.SetCacheability(HttpCacheability.NoCache);
                Response.BinaryWrite(stream.ToArray());
                Response.End();
            }
        }
        catch (WebException ex)
        {
            throw new Exception((ex.Response as FtpWebResponse).StatusDescription);
        }
    }
}

in a class I have the ID with the name that is giving the error gvFiles, and the other calls this ID to mount the GridView.

some solution?

  • Welcome to Stackoverflow. In your second code you pasted it wrong. It was supposed to be an aspx page as you specified and not the . Cs that has pasted previously.

1 answer

1


Although you haven’t posted yours aspx still its error is quite obvious. It explicitly states that the gvFiles does not exist in the current context.

It would appear gvFiles is a Grid View, due to Gv at the beginning of the name and the DataSource that is being assigned to him.

If you created a Grid View in your aspx it is with the wrong name. Check if there is something like this in your aspx:

<asp:GridView ID="gvFiles" runat="server" Width="600px"></asp:GridView>

Note that if there is an attribute ID of him is not as gvFiles and so you get the error:

CS0103 error The name "gvFiles" does not exist in the current context

  • then I could solve after hours analyzing, it was that the class was not seeing the other

Browser other questions tagged

You are not signed in. Login or sign up in order to post.