Hi
I have below code . I want if error is encountered in catch statement then some message should be returned
public List
{
List
try
{
using (SqlConnection con = new SqlConnection(Common.Con))
{
con.Open();
SqlCommand cmd = new SqlCommand("sp_Location", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@Action", "R");
SqlDataReader rdr = cmd.ExecuteReader();
while (rdr.Read())
{
objLocation.Add(new Location
{
Id = rdr["Id"].ToString(),
Description = rdr["Description"].ToString(),
GstStateCode = rdr["GstStateCode"].ToString(),
GstStateName = rdr["GstStateName"].ToString(),
GstNo = rdr["GstNo"].ToString(),
IsActive = Convert.ToBoolean(rdr["IsActive"]),
});
}
//return objLocation;
}
}
catch (Exception ex)
{
ExceptionLogging.SendExcepToDB(ex);
}
return objLocation;
}
Thanks

Kirtesh ShahRank #250 · 10K reputationPosted Jun 20, 2021, 4:28 AM EDT
You can throw the exception from this method. Your controller can handle that error using try/catch, Handler error attributes, global exception filter, and show an error message to View().
check this article to handle errors from MVC
https://www.c-sharpcorner.com/UploadFile/mscratnesh/exception-handling-in-mvc/
Ramco RamcoRank #462 · 5.4K reputationPosted Jun 20, 2021, 4:51 AM EDT
Hi
When i write ViewBag in catch statement i get error
The name ViewBag does not exist in the current context
Secondly if i write return -1 in the catch statement of below logic is it o.k
public List GetAllLocation() objLocation = new List();
{
List
try
{
using (SqlConnection con = new SqlConnection(Common.Con))
{
con.Open();
SqlCommand cmd = new SqlCommand("sp_Location", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@Action", "R");
SqlDataReader rdr = cmd.ExecuteReader();
while (rdr.Read())
{
objLocation.Add(new Location
{
Id = rdr["Id"].ToString(),
Description = rdr["Description"].ToString(),
GstStateCode = rdr["GstStateCode"].ToString(),
GstStateName = rdr["GstStateName"].ToString(),
GstNo = rdr["GstNo"].ToString(),
IsActive = Convert.ToBoolean(rdr["IsActive"]),
});
}
//return objLocation;
}
}
catch (Exception ex)
{
ExceptionLogging.SendExcepToDB(ex);
}
return objLocation;
}
Thanks
Ramco RamcoRank #462 · 5.4K reputationPosted Jun 20, 2021, 8:20 AM EDT
{
List
try
{
using (SqlConnection con = new SqlConnection(Common.Con))
{
con.Open();
SqlCommand cmd = new SqlCommand("sp_Location", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@Action", "R");
SqlDataReader rdr = cmd.ExecuteReader();
while (rdr.Read())
{
objLocation.Add(new Location
{
Id = rdr["Id"].ToString(),
Description = rdr["Description"].ToString(),
GstStateCode = rdr["GstStateCode"].ToString(),
GstStateName = rdr["GstStateName"].ToString(),
GstNo = rdr["GstNo"].ToString(),
IsActive = Convert.ToBoolean(rdr["IsActive"]),
});
}
//return objLocation;
}
}
catch (Exception ex)
{
ExceptionLogging.SendExcepToDB(ex);
}
return objLocation;
}
Sachin SinghRank #66 · 26.2K reputationPosted Jun 20, 2021, 10:10 AM EDT
Sachin SinghRank #66 · 26.2K reputationPosted Jun 20, 2021, 3:26 AM EDT