Home > OS >  I am getting error in return count saying"Cannot implicitly convert type 'int' to �
I am getting error in return count saying"Cannot implicitly convert type 'int' to �

Time:10-29

public IActionResult DeluxRoomCount()
{
   string deluxRoom = "select COUNT(type) from Rooms where Type='Delxu' And Avilability='True'";
   int count = 0;
   using (SqlConnection thisConnection = new SqlConnection("Data Source=HootelReservationDb1"))
        {
           using (SqlCommand cmdCount = new SqlCommand(deluxRoom, thisConnection))
                {
                    thisConnection.Open();
                    count = (int)cmdCount.ExecuteScalar();
                }
                return count;
        }
 }

i am hoping that there will be some ideas to count sql data and i am tring to understand what is wrong with this code

CodePudding user response:

Your Method's return type is IActionResult, But you're returning an int and in order to make your method work, You'll need to return an IActionResult. Here is some reference on how to do that: https://learn.microsoft.com/en-us/aspnet/core/web-api/action-return-types?view=aspnetcore-6.0

CodePudding user response:

you need to cast into drive class which implement IActionResult. try this.

public IActionResult DeluxRoomCount()
{
   string deluxRoom = "select COUNT(type) from Rooms where Type='Delxu' And Avilability='True'";
   int count = 0;
   using (SqlConnection thisConnection = new SqlConnection("Data Source=HootelReservationDb1"))
        {
           using (SqlCommand cmdCount = new SqlCommand(deluxRoom, thisConnection))
                {
                    thisConnection.Open();
                    count = (int)cmdCount.ExecuteScalar();
                }
        }

    return Ok(count)
 }

  • Related