Click here to Skip to main content
15,883,771 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi, I am trying to read stream data and write it to a table in asp.net core 2.2 web api and running into errors.

C#
public class HomeController : Controller
{
    private readonly DbContext _context;
    private readonly IConfiguration Configuration;

    public HomeController(DbContext context, IConfiguration configuration)
    {
        _context = context;
        //_roleManager = roleManager;
        Configuration = configuration;
    }

    [HttpPost]
    [Route("api/v1/test", Name = "Test")]
    public Task Post()
    {
        try
        {
            using (var records = new StreamReader(Request.Body, Encoding.UTF8))
            {
                ImportData(records);
                return Ok("OK"); //Error: Cannot implicitly convert type 'Microsoft.AspNetCore.Mvc.OkObjectResult' to 'System.Threading.Tasks.Task'

            }
        }
        catch (Exception ex)
        {
            return BadRequest(ex.Message); //Error: Cannot implicitly convert type 'Microsoft.AspNetCore.Mvc.BadRequestObjectResult' to 'System.Threading.Tasks.Task'

        }
    }
    public void ImportData(StreamReader records)
    {
        string connectionstring = Configuration["ConnectionStrings:dbconnstr"];

        List test = new List();

        foreach (string record in records)
        {
            Test t = new Test();
            string[] textpart = record.Split('|');
            t.ID = textpart[0];
            t.Name = textpart[1];
            test.Add(t);
        }
        var copyParameters = new[]
            {
            nameof(Test.ID),
            nameof(Test.Name),
            };
        using (var sqlCopy = new SqlBulkCopy(connectionstring))
        {
            sqlCopy.DestinationTableName = "[Test]";
            sqlCopy.BatchSize = 500;
            using (var reader = ObjectReader.Create(test, copyParameters))
            {
                sqlCopy.WriteToServer(reader);
            }
        }
    }
}


What I have tried:

I tried the above code which is a bit different from .net 4.6 framework and trying to make it work.
Posted
Updated 11-Dec-19 4:20am
Comments
Mehul M Thakkar 10-Dec-19 3:37am    
try with "async Task"
Member 12586110 10-Dec-19 7:48am    
Hi Mehul M Thakkar, Thanks very much for your response. Tried async Task and still get the same error.
ZurdoDev 10-Dec-19 13:51pm    
How can we help you? You haven't told us what the errors are.
Member 12586110 10-Dec-19 14:43pm    
Hi ZurdoDev Thank you for your response. I am trying to read stream of data using StreamReader class and write the content to a database table as part of the post method. This approach used to work fine in .net 4.6 framework. When I write this I get exceptions when I use return and not able to import into a table. Could you please help or guide me write this in .net core 2.2.
ZurdoDev 10-Dec-19 14:48pm    
No, I can't help you because as I already said you haven't told us what the errors are.

1 solution

Edit- Just read your question again, even though I would use IFormFile for file transfers with a webapi but that is my personal prefernce

your error is in the signature of your method

C#
public Task Post()


should be

C#
public Task<IactionResult> Post()
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900