-1

I'm getting that error message while trying to define my controller functions (I'm using ASP.NET Core 5.0). I've been trying to solve it for a while but without success, I'm pretty much stuck.

Take a look:

Controller functions showing error:

public ActionResult Edit(int id)
{
    using (Approver approver = dbContext.GetApproverbyId(id))
    {
        return View(approver);
    }
}

public ActionResult Index()
{
    List<approver> approverList = dbContext.GetAllapprovers().ToList();
    return View(approverList);
}

public ActionResult Details(int id)
{
    approver approver = dbContext.GetApproverbyId(id);
    return View(approver);
}

My CONTEXT functions:

public IEnumerable<approver> GetAllapprovers()
{
    var approverList = new List<approver>();

    using (SqlConnection con = new SqlConnection(connectionString))
    {
        SqlCommand cmd = new SqlCommand("SP_GetAllApprovers", con)
                {
                    CommandType = System.Data.CommandType.StoredProcedure
                };

        con.Open();

        SqlDataReader dr = cmd.ExecuteReader();

        while (dr.Read())
        {
            var approver = new approver
                    {
                        CODE = Convert.ToInt32(dr["CODE"].ToString()),
                        MAIL = dr["MAIL"].ToString()
                    };

            approversList.Add(approver);
        }

        con.Close();
    }

    return approversList;
}

public approver GetApproverbyId(int? id)
{
    var approver = new approver();

    using (SqlConnection con = new SqlConnection(connectionString))
    {
        SqlCommand cmd = new SqlCommand("SP_GetapproverById", con);
        cmd.CommandType = System.Data.CommandType.StoredProcedure;
        cmd.Parameters.AddWithValue("@CODE", id);

        con.Open();

        SqlDataReader dr = cmd.ExecuteReader();

        while (dr.Read())
        {
            approver.CODE = Convert.ToInt32(dr["CODE"].ToString());
            approver.MAIL = dr["MAIL"].ToString();
        }

        con.Close();
    }

    return approver;
}

What am I doing wrong? Can help me to solve this?. I'm pretty lost

Lines showing error:

dbContext.GetApproverbyId(id))

dbContext.GetAllapprovers().ToList();

dbContext.GetApproverbyId(id);


JustToKnow
  • 785
  • 6
  • 23
  • Hey @00110001 , thanks for replying. I just edited my thread, please, take a look – JustToKnow Feb 23 '21 at 05:26
  • 4
    I would **strongly** suggest you take a look at **Dapper** which can make life a lot easier and make you not have to write all those boring "glue" code just to load the data from the database ..... – marc_s Feb 23 '21 at 05:34
  • 1
    you talk about ***that error*** in the title but what I see is just this *Cannot implicitly convert*, so what's the error? where is the exact line you have that error? those info would make the others jump into the issue more quickly and actually for some, that would be required to intrigue them to care about the issue first. – King King Feb 23 '21 at 06:12
  • 1
    Do you maybe have two definitions of `approver`? Your dbContext returns one version, but your controller uses the other one for its local variables – Hans Kesting Feb 23 '21 at 08:51
  • OT please [don't use "sp_" prefix](https://stackoverflow.com/a/42640926/121309) – Hans Kesting Feb 23 '21 at 08:53
  • It is great that you translate the code in your question into English, but please check that translation: is the classname "Approver" or "approver", is it "approveresLista" or "approverList" - is it just a typo in your post or the source of your problems? – Hans Kesting Feb 23 '21 at 08:55
  • Could you please full error text. *Cannot implicitly convert* what to what? – Alexander Feb 23 '21 at 09:03
  • Hey @HansKesting i've installed a program and it corrects my grammar and sometimes it goes crazy. Take a look, i've edited my question – JustToKnow Feb 23 '21 at 19:04
  • Hey @Alexander i've edited my question, can check again? – JustToKnow Feb 23 '21 at 19:04
  • Hey @KingKing can check again? i've edited my question – JustToKnow Feb 23 '21 at 19:04

1 Answers1

1

I think the problem made by your "connectionstring".SQL connection failed to pick your "appsettings.json's"file's connetionstring. for that you found error here

dbContext.GetApproverbyId(id))

dbContext.GetAllapprovers().ToList();

dbContext.GetApproverbyId(id); So you have to pick your connectionstring correctly.

private IConfiguration Configuration;
public HomeController(IConfiguration _configuration)
    {
        Configuration = _configuration;
    }

Now you have to some changes your context function


public IEnumerable<approver> GetAllapprovers()
{
    var approveresLista = new List<approver>();
    string connectionString=  Configuration.GetConnectionString("YOUR CONNECTION STRING NAME");

    using (SqlConnection con = new SqlConnection(connectionString))
    {
        SqlCommand cmd = new SqlCommand("SP_GetAllApprovers", con)
                {
                    CommandType = System.Data.CommandType.StoredProcedure
                };

        con.Open();

        SqlDataReader dr = cmd.ExecuteReader();

        while (dr.Read())
        {
            var approver = new approver
                    {
                        CODE = Convert.ToInt32(dr["CODE"].ToString()),
                        MAIL = dr["MAIL"].ToString()
                    };

            approversList.Add(approver);
        }

        con.Close();
    }

    return approversList;
}

Use your other context methods same as above. I think it will work.if you have any issues with the above code. let me know.

Pritom Sarkar
  • 2,154
  • 3
  • 11
  • 26