Asked by:
pick files from a folder and save file details in a SQL DB returns compile time errors.

Question
-
User-1660589204 posted
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); } }
Friday, November 13, 2020 12:31 PM
All replies
-
User475983607 posted
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.
Friday, November 13, 2020 1:12 PM -
User-1660589204 posted
Right, I changed my code now.
public async void PostValuesIntoDB() { DirectoryInfo info = new DirectoryInfo(path); 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.CreatedDate = fileInfo.CreationTime; await _rSBRepository.Add(detail); } } }
My RSBRepository Add method code is:
public async Task<string> Add(RsbfileDetail rsbfileDetail) { string dbresult = string.Empty; _dbContext.Add(rsbfileDetail); var response= await _dbContext.SaveChangesAsync(); dbresult = response.ToString(); return dbresult; }
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?
Friday, November 13, 2020 1:46 PM -
User475983607 posted
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?
FileInfo[] fileInfos = info.GetFiles("*.trm");
What is the value of "response"?
var response= await _dbContext.SaveChangesAsync();
Never return void from an async mehtod. Return "Task". Returning void hides errors.
public async Task PostValuesIntoDB()
Are you using the Visual Studio debugger to step through your code?
Friday, November 13, 2020 4:09 PM -
User-1660589204 posted
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]}"
Sunday, November 15, 2020 10:30 AM -
User475983607 posted
The code is working as expected since there are not files to process.
Sunday, November 15, 2020 11:49 AM