I have created a connection class in asp.net to save values in list i have written following code but it is generating error that MessageBox
doesn't exist. I google this and have found few answers but these are also not working here my code is:
public static class Connection
{
public static ArrayList GetCoffeeByType(string coffeetype)
{
ArrayList list = new ArrayList();
try
{
mydbEntities db = new mydbEntities();
var query = from p in db.tableabc select p;
list = query.ToList();
}
catch (Exception ex)
{
MessageBox.Show(exceptionObj.Message.ToString());
}
return list;
}
}
I tried this System.Web.UI.ScriptManager.RegisterStartupScript(this, typeof(Page), "alert", "alert('" + Message + "')", true);
-- But it is also showing error at this
and Message
-- how can i show this MessageBox?
You won't be able to invoke MessageBox.Show
in an ASP.NET application. Try this instead:
catch (Exception ex)
{
var script = "alert(" + System.Web.HttpUtility.JavaScriptStringEncode(ex.Message, true) + ")";
System.Web.UI.ScriptManager.RegisterStartupScript(this, typeof(Page), "alert", script, true);
}
Note that you need to get the Message
property on the caught exception, ex
, and you should use JavaScriptStringEncode
to safely escape the alert message.
This is a static class called Alert with one public method called Show. The implementation is as simple as can be. Just put the .cs file in the App_Code folder on your website and you instantly have access to the method from all pages and user controls.
using System.Web;
using System.Text;
using System.Web.UI;
/// <summary>
/// A JavaScript alert
/// </summary>
publicstaticclass Alert
{
/// <summary>
/// Shows a client-side JavaScript alert in the browser.
/// </summary>
/// <param name="message">The message to appear in the alert.</param>
publicstaticvoid Show(string message)
{
// Cleans the message to allow single quotation marks
string cleanMessage = message.Replace("'", "\\'");
string script ="<script type=\"text/javascript\">alert('"+ cleanMessage +"');</script>";
// Gets the executing web page
Page page = HttpContext.Current.CurrentHandler as Page;
// Checks if the handler is a Page and that the script isn't allready on the Page
if (page !=null && !page.ClientScript.IsClientScriptBlockRegistered("alert"))
{
page.ClientScript.RegisterClientScriptBlock(typeof(Alert), "alert", script);
}
}
}
Demonstration
That class enables you to add a JavaScript alert to any page at any time. Here is an example of a Button.Click
event handler that uses the method for displaying status messages.
void btnSave_Click(object sender, EventArgs e)
{
try
{
Alert.Show("Your Message");
}
catch (Exeception ex )
{
Alert.Show(ex.Message);
}
}
In your Case :
public static class Connection
{
public static ArrayList GetCoffeeByType(string coffeetype)
{
ArrayList list = new ArrayList();
try
{
mydbEntities db = new mydbEntities();
var query = from p in db.tableabc select p;
list = query.ToList();
}
catch (Exception ex)
{
Alert.Show(ex.Message);
}
return list;
}
}
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.