Display Success Popup Box after record inserted using C# and VB.Net in ASP.Net

Last Reply 3 months ago By dharmendr

Posted 3 months ago

hello,

i have many forms where on button click it saves data. 

i want when user click on save button if data add successfully it shows a popup message on top bar

please advice how to achieve it globally. 

i am using try catch block

Posted 3 months ago

Hi nauna,

Check this example. Now please take its reference and correct your code.

HTML

<table>
    <tr>
        <td>
            Name
        </td>
        <td>
            <asp:TextBox runat="server" ID="txtName" />
        </td>
    </tr>
    <tr>
        <td>
            Country
        </td>
        <td>
            <asp:TextBox runat="server" ID="txtCountry" />
        </td>
    </tr>
    <tr>
        <td colspan="2" align="center">
            <asp:Button Text="Save" runat="server" OnClick="Save" />
        </td>
    </tr>
</table>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script type="text/javascript" src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
<link type="text/css" rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css" />
<style type="text/css">
    .message
    {
        width: 100%;
        position: absolute;
        top: 10px;
        left: 50%;
        z-index: 100000;
        padding: 0;
        font-size: 15px;
        margin: 0 auto;
    }
</style>
<script type="text/javascript">
    function ShowMessage(message, messagetype) {
        var cssclass;
        switch (messagetype) {
            case 'Success':
                cssclass = 'alert-success'
                break;
            case 'Error':
                cssclass = 'alert-danger'
                break;
            case 'Warning':
                cssclass = 'alert-warning'
                break;
            default:
                cssclass = 'alert-info'
        }
        $('body').append('<div id="alert_div" style="margin: 0 10%; -webkit-box-shadow: 3px 4px 6px #999;width:50%" class="alert fade in '
    + cssclass + '"><a href="#" class="close" data-dismiss="alert" aria-label="close">&times;</a><strong>'
    + '<span>' + message + '</span></div>');
    }
</script>

Namespaces

C#

using System.Configuration;
using System.Data.SqlClient;

VB.Net

Imports System.Configuration
Imports System.Data.SqlClient

Code

C#

protected void Save(object sender, EventArgs e)
{
    int i = 0;
    string conString = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
    string query = "INSERT INTO Customers VALUES(@Name,@Country)";
    SqlCommand cmd = new SqlCommand(query);
    SqlConnection con = new SqlConnection(conString);

    cmd.Connection = con;
    cmd.Parameters.AddWithValue("@Name", txtName.Text.Trim());
    cmd.Parameters.AddWithValue("@Country", txtCountry.Text.Trim());
    try
    {
        con.Open();
        i = cmd.ExecuteNonQuery();
        con.Close();
        if (i > 0)
        {
            ShowMessage("Record submitted successfully", MessageType.Success);
        }
    }
    catch (Exception ex)
    {

    }
}

public enum MessageType { Success, Error, Info, Warning };
protected void ShowMessage(string Message, MessageType type)
{
    ScriptManager.RegisterStartupScript(this, this.GetType(), System.Guid.NewGuid().ToString(), "ShowMessage('" + Message + "','" + type + "');", true);
}

VB.Net

Protected Sub Save(ByVal sender As Object, ByVal e As EventArgs)
    Dim i As Integer = 0
    Dim conString As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
    Dim query As String = "INSERT INTO Customers VALUES(@Name,@Country)"
    Dim cmd As SqlCommand = New SqlCommand(query)
    Dim con As SqlConnection = New SqlConnection(conString)
    cmd.Connection = con
    cmd.Parameters.AddWithValue("@Name", txtName.Text.Trim())
    cmd.Parameters.AddWithValue("@Country", txtCountry.Text.Trim())
    Try
        con.Open()
        i = cmd.ExecuteNonQuery()
        con.Close()

        If i > 0 Then
            ShowMessage("Record submitted successfully", MessageType.Success)
        End If
    Catch ex As Exception
    End Try
End Sub

Public Enum MessageType
    Success = 0
    [Error] = 1
    Info = 2
    Warning = 3
End Enum

Protected Sub ShowMessage(ByVal Message As String, ByVal type As MessageType)
    ScriptManager.RegisterStartupScript(Me, Me.GetType(), System.Guid.NewGuid().ToString(), "ShowMessage('" & Message & "','" & type & "');", True)
End Sub

Screenshot


Posted 3 months ago

Hi nauna,

Yes you can.

Follow the below steps.

1. Create a class file and write the below code.

public class MessagePopup
{
    private System.Web.UI.Page page;
    public MessagePopup(System.Web.UI.Page pages)
    {
        page = pages;
    }

    public void ShowMessage(string Message, string messageType)
    {
        if (messageType.ToLower() == "success")
        {
            messageType = "Success";
        }
        else if (messageType.ToLower() == "error")
        {
            messageType = "Error";
        }
        else if (messageType.ToLower() == "info")
        {
            messageType = "Info";
        }
        else if (messageType.ToLower() == "warning")
        {
            messageType = "Warning";
        }
        System.Web.UI.ScriptManager.RegisterStartupScript(page, this.GetType(), System.Guid.NewGuid().ToString(), "ShowMessage('" + Message + "','" + messageType + "');", true);
    }
}

2. Call the method using the below code in the page where you want to call.

MessagePopup messagePopup = new MessagePopup(this.Page);
messagePopup.ShowMessage("Record submitted successfully", "Success");