简体   繁体   English

收到错误消息:CS0029:无法隐式转换(ASP.NET Core MVC 和 C#)

[英]Getting error message: CS0029: Cannot implicitly convert (ASP.NET Core MVC & C#)

I'm getting that error message while trying to define my controller functions (I'm using ASP.NET Core 5.0).我在尝试定义 controller 函数时收到该错误消息(我使用的是 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: Controller 函数显示错误:

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);


I think the problem made by your "connectionstring".SQL connection failed to pick your "appsettings.json's"file's connetionstring.我认为您的“connectionstring”.SQL 连接导致的问题未能选择您的“appsettings.json”文件的连接字符串。 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现在你必须改变你的上下文 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.让我知道。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 CS0029 C# 无法将类型隐式转换为“int?” ASP.NET CORE 5 MVC 标识 - CS0029 C# Cannot implicitly convert type to 'int?' ASP.NET CORE 5 MVC Identity C# - 错误 CS0029 无法将类型“int”隐式转换为“Core.Models.Mandate” - C# - Error CS0029 Cannot implicitly convert type 'int' to 'Core.Models.Mandate' CS0029 C#无法将类型隐式转换为&#39;string []&#39; - CS0029 C# Cannot implicitly convert type to 'string[]' C#错误:无法将类型&#39;string&#39;隐式转换为&#39;System.Windows.Forms.Control&#39;(CS0029) - c# Error: Cannot implicitly convert type 'string' to 'System.Windows.Forms.Control' (CS0029) ASP.NET MVC 渲染部分错误代码 CS0029 - ASP.NET MVC RenderPartial Error Code CS0029 错误 CS0029:无法将类型“System.DateTime”隐式转换为“int”(CS0029)(DeclaringConstructor) - Error CS0029: Cannot implicitly convert type 'System.DateTime' to 'int' (CS0029) (DeclaringConstructor) 错误CS0029:无法将类型&#39;int&#39;隐式转换为&#39;Score&#39; - error CS0029: Cannot implicitly convert type 'int' to 'Score' 错误 CS0029 无法将类型“字符串”隐式转换为“双精度” - Error CS0029 Cannot implicitly convert type 'string' to 'double' 错误 CS0029:无法将类型“string”隐式转换为“int” - Error CS0029: Cannot implicitly convert type 'string' to 'int' 错误CS0029无法将类型“字符串”隐式转换为“十进制” - Error CS0029 Cannot implicitly convert type 'string' to 'decimal'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM