Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Question
Tuesday, March 8, 2011 5:40 AM
Hi
All the examples and websites i have looked at redirect a user to the error.aspx page when an error occurs.
How do you just display a friendly error message on the page the user is viewing saying something like "Sorry unable to do whatever"
I've tried using a try catch block on my class that executes a stored procedure and put another try catch on the controller, but this does not work and i still get the default error message (System.InvalidOperationException was unhandled by user code)
My code is below:
public List<GetCurrencyDataFieldsFromDB> GetCurrencyDatabaseFields()
{
string spName = "dbo.******";
using (SqlConnection cn = new SqlConnection(dbConn))
{
using (SqlCommand cmd = new SqlCommand(spName, cn))
{
cmd.CommandType = CommandType.StoredProcedure;
List<G*****> CurrencyData = new List<G*****>();
try
{
cn.Open();
using (SqlDataReader rdr = cmd.ExecuteReader(CommandBehavior.SingleResult))
{
while (rdr.Read())
{
****** GetData = new *******(
(string)rdr["CurrencyID"],
(string)rdr["Country"]);
CurrencyData.Add(GetData);
}
rdr.Close();
}
return CurrencyData;
}
catch (SqlException ex)
{
throw new ApplicationException(ex.Message);
}
finally
{
if (cn.State == ConnectionState.Open)
{
cn.Close();
}
}
}
}
}
public ActionResult Index()
{
try
{
var ws = IGCD.******();
this.ViewData["*****"] = new SelectList(ws.ToList(), "dboCurrencyID", "dboCountry");
this.ViewData["*****"] = new SelectList(ws.ToList(), "dboCurrencyID", "dboCountry");
}
catch (Exception ex)
{
ViewData["SqlError"] = ex.Message;
return View();
}
finally
{
}
return View();
}
So how can i just return ViewData["SqlError"] message in page
Regards
George
All replies (7)
Tuesday, March 8, 2011 9:12 AM ✅Answered
what i need is to display an error on the page if the database is down when the page loads, before any user input.
Then make a try/catch in your action and in the catch add:
ModelState.AddModelError("", ex.Message)
Wednesday, March 9, 2011 5:58 AM ✅Answered
1. Because ViewData does not allow compile time checking. Please see
http://msprogrammer.serviciipeweb.ro/2010/03/29/asp-net-mvc-orm-and-viewmodels/
2. This sort of errors - if you continue with ViewData- just with testing
Tuesday, March 8, 2011 8:24 AM
ModelState.AddModelError("","my message")
Please follow MVC Music Store/NerdDinner tutorials
Tuesday, March 8, 2011 8:56 AM
Hi Ignatandrei
All the examples i find including the ones in MVC Music Store/NerdDinner tutorials are related to invalid user input, what i need is to display an error on the page if the database is down when the page loads, before any user input.
I will have 3 partialviews on the page(if i can get them to work), if the one returning the currency data from the database is unable to get the data, i just want to display a message informing the user that they cannot check the currency rates at the moment.
Regards
George
Tuesday, March 8, 2011 10:03 AM
Hi Ignatandrei
I have done that, but still get the same error, 'There is no ViewData item of type 'IEnumerable<SelectListItem>' that has the key 'ddlCurrencyConvertFrom'.' from my partialview
My code is below:
Going from Webforms to MVC is a huge learning curve, people say MVC is easy, but i find it to be anything but at the moment, and I have 5 books on MVC which is OK to read, but putting into my practice is another thing
<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<IEnumerable<Web.Domain.Models.Currency.Entities.GetCurrencyDataFieldsFromDB>>" %>
<p>Currency Convertor</p>
<%: Html.DropDownList("ddlCurrencyConvertFrom", (SelectList)(ViewData["ddlCurrency"]),"Convert From")%>
<%: Html.DropDownList("ddlCurrencyConvertTo", (SelectList)(ViewData["ddlCurrency"]),"Convert To")%>
<%: Html.ValidationMessage("Unable to connect to db") %>
public ActionResult Index()
{
try
{
if (ModelState.IsValid)
{
var ws = IGCD.GetCurrencyDatabaseFields();
this.ViewData["ddlCurrencyConvertFrom"] = new SelectList(ws.ToList(), "dboCurrencyID", "dboCountry");
this.ViewData["ddlCurrencyConvertTo"] = new SelectList(ws.ToList(), "dboCurrencyID", "dboCountry");
}
}
catch (Exception ex)
{
ModelState.AddModelError("", ex.Message);
}
return View();
}
Regards
George
Wednesday, March 9, 2011 3:20 AM
There is no ViewData item of type 'IEnumerable<SelectListItem>' that has the key 'ddlCurrencyConvertFrom'.' from my partialview
This is a view error, not an action error. You did not pass a ViewData with ddlCurrencyConvertFrom
( did I mention that I hate ViewData?)
Wednesday, March 9, 2011 5:31 AM
Hi Ignatandrei
Why do you hate viewdata?
Also how do i solve these sorts of errors?
Regards
George