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.
– George Wurthmann