I have to read files from a folder and save the file details like filepath, name etc in my SQL database. Below is my code for doing so which returns compile time errors:
path is saved in appsettings.json and retrieved in constructor of class as: path = config["FilePath:SharedFolderPath"];
public void PostValuesIntoDB()
{
DirectoryInfo info = new DirectoryInfo(path);
List<RsbfileDetail> details = new List<RsbfileDetail>();
FileInfo[] fileInfos = info.GetFiles("*.trm");
if (fileInfos != null)
{
foreach (FileInfo fileInfo in fileInfos)
{
RsbfileDetail detail = new RsbfileDetail();
detail.Filename = fileInfo.Name;
detail.FileUrl = fileInfo.FullName;
detail.FileType = fileInfo.Extension;
detail.FileReferenceId = int.Parse(fileInfo.Name.Split('_')[0]);
detail.CreatedDate = fileInfo.CreationTime;
details.Add(detail);
}
}
var newFile = new RsbfileDetail();
_rSBRepository.Add(newFile);
await _rSBRepository.SaveChangesAsync();
}
newFile: The type arguments for method "ConfigurationExtensions.Add<TSource>(IConfigurationBuilder,Action<TSource>)" can not be inerred from the usage. Try specifying type arguments explicitly.
SaveChangesAsync(): IRSBRepository does not contain a definition for SaveChangesAsync and no accessible extension method 'SaveChangesAsync' accepting a first argument of type 'IRSBRepository' could be found.
I do not understand what I am missing. Please help me. My IRSBRepository code is:
using RSBSpeechToText.Models;
using System.Threading.Tasks;
namespace RSBSpeechToText.Repositories
{
public interface IRSBRepository : IRepository<RsbfileDetail>
{
Task<RsbfileDetail> UploadFiles(string filename);
public void GetNewFileFromFolderToDB();
}
}
RSBRepository which implements above interface is:
public class RSBRepository : Repository<RsbfileDetail>, IRSBRepository
{
private readonly rsbsrdbContext _dbContext;
public RSBRepository(rsbsrdbContext dbContext) : base(dbContext)
{
_dbContext = dbContext;
}
Task<RsbfileDetail> IRSBRepository.UploadFiles(string filename)
{
throw new NotImplementedException();
}
public void GetNewFileFromFolderToDB()
{
List<RsbfileDetail> result1 = new List<RsbfileDetail>();
result1 = _dbContext.RsbfileDetails.Where(j => (j.Filename == "sample.txt")).ToList();
Console.WriteLine(result1);
}
}
The error messages are very clear but it is very difficult to understand what you are trying to do by reading the code. The code fills a collection named "details" and does nothing with the collection. After the loop there's, what looks like, a repository
that saves an empty instance named newFile.
It looks like might have copied code from the Internet but do not understand how the code works.
Member
9 Points
33 Posts
pick files from a folder and save file details in a SQL DB returns compile time errors.
Nov 13, 2020 12:31 PM|ddesarajubyc|LINK
I have to read files from a folder and save the file details like filepath, name etc in my SQL database. Below is my code for doing so which returns compile time errors:
path is saved in appsettings.json and retrieved in constructor of class as: path = config["FilePath:SharedFolderPath"];
newFile: The type arguments for method "ConfigurationExtensions.Add<TSource>(IConfigurationBuilder,Action<TSource>)" can not be inerred from the usage. Try specifying type arguments explicitly.
SaveChangesAsync(): IRSBRepository does not contain a definition for SaveChangesAsync and no accessible extension method 'SaveChangesAsync' accepting a first argument of type 'IRSBRepository' could be found.
I do not understand what I am missing. Please help me. My IRSBRepository code is:
RSBRepository which implements above interface is:
All-Star
53091 Points
23659 Posts
Re: pick files from a folder and save file details in a SQL DB returns compile time errors.
Nov 13, 2020 01:12 PM|mgebhard|LINK
The error messages are very clear but it is very difficult to understand what you are trying to do by reading the code. The code fills a collection named "details" and does nothing with the collection. After the loop there's, what looks like, a repository that saves an empty instance named newFile.
It looks like might have copied code from the Internet but do not understand how the code works.
Member
9 Points
33 Posts
Re: pick files from a folder and save file details in a SQL DB returns compile time errors.
Nov 13, 2020 01:46 PM|ddesarajubyc|LINK
Right, I changed my code now.
My RSBRepository Add method code is:
This I thought will save the detail of every row into my data table. But values are not being written into DB. Please help me know why?
All-Star
53091 Points
23659 Posts
Re: pick files from a folder and save file details in a SQL DB returns compile time errors.
Nov 13, 2020 04:09 PM|mgebhard|LINK
Answer the following questions?
What kind of application is this? ASP.NET Core? ASP.NET? MVC? Web API?
How many files are returned by the line of code below?
What is the value of "response"?
Never return void from an async mehtod. Return "Task". Returning void hides errors.
Are you using the Visual Studio debugger to step through your code?
https://docs.microsoft.com/en-us/visualstudio/debugger/navigating-through-code-with-the-debugger?view=vs-2019
Member
9 Points
33 Posts
Re: pick files from a folder and save file details in a SQL DB returns compile time errors.
Nov 15, 2020 10:30 AM|ddesarajubyc|LINK
It is a .NET core application(console application).
The foreach loop goes to FileInfos and then comes out of the loop after checking value in highlighted code: fileInfos shows "{System.IO.fileInfo[0]}"
when debugging
foreach (FileInfo fileInfo in fileInfos)
0 to 3 files every 5 minutes. I will put this code in a scheduler to check every 5 minutes.
I have tried with Visual studio debugging. fileInfos shows "{System.IO.fileInfo[0]}"
All-Star
53091 Points
23659 Posts
Re: pick files from a folder and save file details in a SQL DB returns compile time errors.
Nov 15, 2020 11:49 AM|mgebhard|LINK
The code is working as expected since there are not files to process.