Izzy
Izzy

Reputation: 6866

Pass List to Directory

I'm currently working on a website which is being developed in asp.net and c#. I'm currently facing a slight problem. I am returning a list of the file location which is from a database using the following code.

public List<FileDetails> GetFiles()
{
    string userName = HttpContext.Current.User.Identity.Name;

    string sqlQuery = @"SELECT  FileName,
                                FileLocation
                        FROM    File
                        WHERE   UserName=@UserName";

    List<FileDetails> FileDetailsList = new List<FileDetails>();

    using (SqlConnection connection = new SqlConnection(Common.ConnectionString))
    {
        using (SqlCommand cmd = new SqlCommand(sqlQuery))
        {
            cmd.Parameters.AddWithValue("UserName", userName);
            cmd.Connection = connection;
            connection.Open();

            using (SqlDataReader reader = cmd.ExecuteReader())
            {
                while (reader.Read())
                {
                    FileDetails f = new FileDetails();

                    f.FileName = reader["FileName"].ToString();
                    f.FileLocation = reader["FileLocation"].ToString();

                    FileDetailsList.Add(f);
                }
            }
        }
    }
    return FileDetailsList;
}

Now I need to display them to a gridview on a different page. I am using the following code to achieve this

FileDetails info = new FileDetails();
    if (info.GetFiles() != null)
    {
        List<string> filePaths = Directory.GetFiles(info.FileLocation).ToList(); //Error is at this line
        List<FileDetails> files = new List<FileDetails>();
        foreach (string filePath in filePaths) 
        {
            string filename = Path.GetFileName(filePath);
            files.Add(new FileDetails()
            {
                FileName = filename,
                FileLocation = filePath
            });
        }
        GridView1.DataSource = info.GetFiles();
        GridView1.DataBind();
    }

Problem is I keep getting an error System.ArgumentNullException: Value cannot be null. Parameter name: path Does anyone know where I'm going wrong?

If I do Directory.GetFiles(@"C:\Test\Files").ToList(); it works perfectly fine. But the clients requirement is to get the file location path from the database.

Thanks in advance for your help

Upvotes: 0

Views: 277

Answers (2)

Mairaj Ahmad
Mairaj Ahmad

Reputation: 14604

You are trying to get filelocation from info.GetFilelocation and you have created new object of FileDetails so there will be now values associated with this object hence all values will be null.So you need to assign somevalue to this object than you can access FileLocation.May be somthing like this

FileDetails info = new FileDetails();
List<FileDetails> list=info.GetFiles();

And than

List<string> filePaths = Directory.GetFiles(list[0].FileLocation).ToList();

Similarly you can loop through the list

foreach(FileDetails f in list)
List<string> filePaths = Directory.GetFiles(f.FileLocation).ToList();

Upvotes: 1

Tod
Tod

Reputation: 2524

Assumptions: info is a class. info.GetFiles() returns a list of records from your database. (code at the top)

At which point does info.FileLocation get set?

surely:

var filelist = info.GetFiles();
foreach(var f in filelist)
{
    List<string> filePaths = Directory.GetFiles(f.FileLocation).ToList();
    ...
}  

Upvotes: 1

Related Questions