© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•13mo ago•
44 replies
/Mr.Ares77

How to update mysql server data?

Hi, I'm building todo list project server.

I wanna update database but have got 500 error.

public async Task<bool> UpdateTodo(Guid userId, Guid todoId, TodoEntity todo)
{
    var existingTodo = await _dbContext.todos
        .Include(t => t.TodoItems)
        .Where(t => t.DeletedAt == null && t.Id == todoId && t.UserId == userId)
        .FirstOrDefaultAsync();

    if (existingTodo != null)
    {
        existingTodo.UpdatedAt = DateTime.UtcNow;
        existingTodo.DueDate = todo.DueDate;
        existingTodo.Title = todo.Title;
        existingTodo.TodoItems.Clear();
        existingTodo.TodoItems = todo.TodoItems.Select(item => new TodoItemEntity
        {
            ItemTitle = item.ItemTitle,
            Status = item.Status,
            Deleted = item.Deleted
        }).ToList();

        _dbContext.todos.Update(existingTodo);

        await _dbContext.SaveChangesAsync();

        return true;
    }

    return false;

}
public async Task<bool> UpdateTodo(Guid userId, Guid todoId, TodoEntity todo)
{
    var existingTodo = await _dbContext.todos
        .Include(t => t.TodoItems)
        .Where(t => t.DeletedAt == null && t.Id == todoId && t.UserId == userId)
        .FirstOrDefaultAsync();

    if (existingTodo != null)
    {
        existingTodo.UpdatedAt = DateTime.UtcNow;
        existingTodo.DueDate = todo.DueDate;
        existingTodo.Title = todo.Title;
        existingTodo.TodoItems.Clear();
        existingTodo.TodoItems = todo.TodoItems.Select(item => new TodoItemEntity
        {
            ItemTitle = item.ItemTitle,
            Status = item.Status,
            Deleted = item.Deleted
        }).ToList();

        _dbContext.todos.Update(existingTodo);

        await _dbContext.SaveChangesAsync();

        return true;
    }

    return false;

}

[HttpPut("{id}")]
public async Task<IActionResult> UpdateDate(Guid id, [FromBody] TodoRequestDto request)
{
    var userIdClaim = User.FindFirst(ClaimTypes.NameIdentifier)?.Value;

    if (string.IsNullOrEmpty(userIdClaim))
    {
        return Unauthorized("User not authenticated.");
    }

    var userId = Guid.Parse(userIdClaim);

    var todo = new TodoEntity
    {
        DueDate = request.DueDate,
        Title = request.Title,
        TodoItems = request.TodoItems.Select(item => new TodoItemEntity
        {
            ItemTitle = item.ItemTitle,
            Status = item.Status,
            Deleted = item.Deleted,
        }).ToList(),
    };

    var result = await _todoService.UpdateTodo(userId, id, todo);

    return Ok(new { Success = result });
}
[HttpPut("{id}")]
public async Task<IActionResult> UpdateDate(Guid id, [FromBody] TodoRequestDto request)
{
    var userIdClaim = User.FindFirst(ClaimTypes.NameIdentifier)?.Value;

    if (string.IsNullOrEmpty(userIdClaim))
    {
        return Unauthorized("User not authenticated.");
    }

    var userId = Guid.Parse(userIdClaim);

    var todo = new TodoEntity
    {
        DueDate = request.DueDate,
        Title = request.Title,
        TodoItems = request.TodoItems.Select(item => new TodoItemEntity
        {
            ItemTitle = item.ItemTitle,
            Status = item.Status,
            Deleted = item.Deleted,
        }).ToList(),
    };

    var result = await _todoService.UpdateTodo(userId, id, todo);

    return Ok(new { Success = result });
}
C# banner
C#Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,871Members
Resources

Similar Threads

Was this page helpful?
Recent Announcements

Similar Threads

❔ inserting data from visual studio to mysql databases
C#CC# / help
4y ago
❔ Error while trying to connect to MySql using MySql .Data
C#CC# / help
3y ago
ERROR while trying to connect to mysql using mysql.data
C#CC# / help
3y ago