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
Sachin SinghPosted Jun 20, 2021, 2:10 PM
Ramco RamcoPosted Jun 20, 2021, 12:20 PM
{
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;
}
Ramco RamcoPosted Jun 20, 2021, 8:51 AM
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
Kirtesh ShahPosted Jun 20, 2021, 8:28 AM
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/
Sachin SinghPosted Jun 20, 2021, 7:26 AM