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);
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.