Show successful message after Insert, Update and Delete using C# in ASP.Net

Last Reply 10 months ago By dharmendr

Posted 10 months ago

Hello, 

I created a griedview in asp and I want when inserted, deleted and updated a record, it shows a message that a record has been inserted or deleted or updated successfully.

 

<%@ Page Title="" Language="C#" MasterPageFile="~/MasterPage.master" AutoEventWireup="true" CodeFile="EmployeesFemale.aspx.cs" Inherits="Employees" %>

<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
    <br />
    <br />
    <br />
    
    <asp:GridView ID="GridView1" runat="server" style="margin-left: 28px; margin-top: 76px; margin-bottom: 58px;" Width="776px" AutoGenerateColumns="False" CellPadding="1" DataSourceID="ObjectDataSource1" DataKeyNames="EmployeeF_ID" ForeColor="#333333" ShowFooter="True" ReadOnly="False" EnablePersistedSelection="True" AllowPaging="True">
        <AlternatingRowStyle BackColor="White" />
        <Columns>
              <asp:TemplateField>
                <FooterTemplate>
                  <asp:LinkButton ValidationGroup="INSERT" OnClick="lbInsert_Click" ID="lbInsert" runat="server">Insert</asp:LinkButton>
                </FooterTemplate>
            </asp:TemplateField>
            <asp:CommandField ShowDeleteButton="True" ShowEditButton="True" ShowHeader="True" />
            <asp:TemplateField HeaderText="First Name" SortExpression="EmployeeF_FName">
                <EditItemTemplate>
                    <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("EmployeeF_FName") %>'></asp:TextBox>
                </EditItemTemplate>
                <ItemTemplate>
                    <asp:Label ID="Label1" runat="server" Text='<%# Bind("EmployeeF_FName") %>'></asp:Label>
                </ItemTemplate>
                 <FooterTemplate>
                  <asp:TextBox ID="TxtFName" runat="server" BorderStyle="None"></asp:TextBox>
                </FooterTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Middle Name" SortExpression="EmployeeF_MName">
                <EditItemTemplate>
                    <asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("EmployeeF_MName") %>'></asp:TextBox>
                </EditItemTemplate>
                <ItemTemplate>
                    <asp:Label ID="Label2" runat="server" Text='<%# Bind("EmployeeF_MName") %>'></asp:Label>
                </ItemTemplate>
                 <FooterTemplate>
                  <asp:TextBox ID="TxtMName" runat="server" BorderStyle="None"></asp:TextBox>
                </FooterTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Last Name" SortExpression="EmployeeF_LName">
                <EditItemTemplate>
                    <asp:TextBox ID="TextBox3" runat="server" Text='<%# Bind("EmployeeF_LName") %>'></asp:TextBox>
                </EditItemTemplate>
                <ItemTemplate>
                    <asp:Label ID="Label3" runat="server" Text='<%# Bind("EmployeeF_LName") %>'></asp:Label>
                </ItemTemplate>
                 <FooterTemplate>
                  <asp:TextBox ID="TxtLName" runat="server" BorderStyle="None"></asp:TextBox>
                </FooterTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="National ID" SortExpression="EmployeeF_ID">
                <EditItemTemplate>
                    <asp:TextBox ID="TextBox4" runat="server" Text='<%# Bind("EmployeeF_ID") %>'></asp:TextBox>
                    <asp:RequiredFieldValidator ID="rfvEditId" runat="server" ErorrMessage="ID is a required field"
                          ControlToValidate="TextBox4" Text="* ID is a required field" ForeColor="Red" ValidationGroup="INSERT">
                        </asp:RequiredFieldValidator>
                </EditItemTemplate>
                <ItemTemplate>
                    <asp:Label ID="Label4" runat="server" Text='<%# Bind("EmployeeF_ID") %>'></asp:Label>
                </ItemTemplate>
                 <FooterTemplate>
                  <asp:TextBox ID="TxtID" runat="server" BorderStyle="None"></asp:TextBox>
                      <asp:RequiredFieldValidator ID="rfvInsertId" runat="server" ErorrMessage="ID is a required field"
                          ControlToValidate="TxtID" Text="* ID is a required field" Font-Size="9" ForeColor="Red" ValidationGroup="INSERT">
                        </asp:RequiredFieldValidator>
                </FooterTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Mobile" SortExpression="EmployeeF_Mobile">
                <EditItemTemplate>
                    <asp:TextBox ID="TextBox5" runat="server" Text='<%# Bind("EmployeeF_Mobile") %>'></asp:TextBox>
                </EditItemTemplate>
                <ItemTemplate>
                    <asp:Label ID="Label5" runat="server" Text='<%# Bind("EmployeeF_Mobile") %>'></asp:Label>
                </ItemTemplate>
                 <FooterTemplate>
                  <asp:TextBox ID="TxtMobile" runat="server" BorderStyle="None"></asp:TextBox>
                </FooterTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Email" SortExpression="EmployeeF_Email">
                <EditItemTemplate>
                    <asp:TextBox ID="TextBox6" runat="server" Text='<%# Bind("EmployeeF_Email") %>'></asp:TextBox>
                </EditItemTemplate>
                <ItemTemplate>
                    <asp:Label ID="Label6" runat="server" Text='<%# Bind("EmployeeF_Email") %>'></asp:Label>
                </ItemTemplate>
                 <FooterTemplate>
                  <asp:TextBox ID="TxtEmail" runat="server" BorderStyle="None"></asp:TextBox>
                </FooterTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Position" SortExpression="EmployeeF_Position">
                <EditItemTemplate>
                    <asp:TextBox ID="TextBox7" runat="server" Text='<%# Bind("EmployeeF_Position") %>'></asp:TextBox>
                </EditItemTemplate>
                <ItemTemplate>
                    <asp:Label ID="Label7" runat="server" Text='<%# Bind("EmployeeF_Position") %>'></asp:Label>
                </ItemTemplate>
                 <FooterTemplate>
                  <asp:TextBox ID="TxtPosition" runat="server" BorderStyle="None"></asp:TextBox>
                </FooterTemplate>
            </asp:TemplateField>
        </Columns>
        <EditRowStyle BackColor="#2461BF" />
        <FooterStyle BackColor="White" Font-Bold="True" ForeColor="White" />
        <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
        <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
        <RowStyle BackColor="#EFF3FB" />
        <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
        <SortedAscendingCellStyle BackColor="#F5F7FB" />
        <SortedAscendingHeaderStyle BackColor="#6D95E1" />
        <SortedDescendingCellStyle BackColor="#E9EBEF" />
        <SortedDescendingHeaderStyle BackColor="#4870BE" />
    </asp:GridView>
    <br />
    <asp:Label ID="lblmessage" runat="server" Text="Label" ForeColor="Green" Visible="False"></asp:Label>
    <p><asp:Button ID="Button1" ForeColor="White" BackColor="#507CD1" runat="server" Text="Back" PostBackUrl="~/HomeFemale.aspx" style="margin-left: 77px" Width="61px" Height="28px" />
    </p>
    <asp:ValidationSummary ValidationGroup="INSERT" ID="ValidationSummary1" ForeColor="Red" runat="server" />
    <asp:ObjectDataSource ID="ObjectDataSource1" runat="server" SelectMethod="GetAllEmployees" TypeName="Demo.EmployeeF.EmployeeDataAccessLayer" UpdateMethod="UpdateEmployee" DeleteMethod="DeleteEmployee"  InsertMethod="InsertEmployee">
        <DeleteParameters>
            <asp:Parameter Name="EmployeeF_ID" Type="Int32" />
        </DeleteParameters>
        <InsertParameters>
            <asp:Parameter Name="EmployeeF_ID" Type="Int32" />
            <asp:Parameter Name="EmployeeF_FName" Type="String" />
            <asp:Parameter Name="EmployeeF_MName" Type="String" />
            <asp:Parameter Name="EmployeeF_LName" Type="String" />
            <asp:Parameter Name="EmployeeF_Mobile" Type="String" />
            <asp:Parameter Name="EmployeeF_Email" Type="String" />
            <asp:Parameter Name="EmployeeF_Position" Type="String" />
        </InsertParameters>
        <UpdateParameters>
            <asp:Parameter Name="EmployeeF_ID" Type="Int32" />
            <asp:Parameter Name="EmployeeF_FName" Type="String" />
            <asp:Parameter Name="EmployeeF_MName" Type="String" />
            <asp:Parameter Name="EmployeeF_LName" Type="String" />
            <asp:Parameter Name="EmployeeF_Mobile" Type="String" />
            <asp:Parameter Name="EmployeeF_Email" Type="String" />
            <asp:Parameter Name="EmployeeF_Position" Type="String" />
        </UpdateParameters>
    </asp:ObjectDataSource>
</asp:Content>

code behind

 

public partial class Employees : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void lbInsert_Click(object sender, EventArgs e)
    {
        ObjectDataSource1.InsertParameters["EmployeeF_FName"].DefaultValue = ((TextBox)GridView1.FooterRow.FindControl("TxtFName")).Text;
        ObjectDataSource1.InsertParameters["EmployeeF_MName"].DefaultValue = ((TextBox)GridView1.FooterRow.FindControl("TxtMName")).Text;
        ObjectDataSource1.InsertParameters["EmployeeF_LName"].DefaultValue = ((TextBox)GridView1.FooterRow.FindControl("TxtLName")).Text;
        ObjectDataSource1.InsertParameters["EmployeeF_ID"].DefaultValue = ((TextBox)GridView1.FooterRow.FindControl("TxtID")).Text;
        ObjectDataSource1.InsertParameters["EmployeeF_Mobile"].DefaultValue = ((TextBox)GridView1.FooterRow.FindControl("TxtMobile")).Text;
        ObjectDataSource1.InsertParameters["EmployeeF_Email"].DefaultValue = ((TextBox)GridView1.FooterRow.FindControl("TxtEmail")).Text;
        ObjectDataSource1.InsertParameters["EmployeeF_Position"].DefaultValue = ((TextBox)GridView1.FooterRow.FindControl("TxtPosition")).Text;
        ObjectDataSource1.Insert();
    }
    
    
}

dataaccesslayer

 

public class EmployeeDataAccessLayer
    {
        public static void UpdateEmployee(int EmployeeF_ID, string EmployeeF_FName, string EmployeeF_MName, string EmployeeF_LName, string EmployeeF_Mobile, string EmployeeF_Email, string EmployeeF_Position)
        {
            string CS = "server=localhost; userid=; password=; database=; allowuservariables=True";
            using (MySqlConnection connection = new MySqlConnection(CS))
            {
                string UpdateQuery = " Update Employees_female SET EmployeeF_FName=@EmployeeF_FName," + " EmployeeF_MName=@EmployeeF_MName," + " EmployeeF_LName=@EmployeeF_LName," + " EmployeeF_Mobile=@EmployeeF_Mobile," + "EmployeeF_Email=@EmployeeF_Email," + "EmployeeF_Position=@EmployeeF_Position WHERE EmployeeF_ID=@EmployeeF_ID";
                MySqlCommand cmd = new MySqlCommand(UpdateQuery, connection);
                cmd.Connection.Open();
                MySqlParameter paramEmployeeF_FName = new MySqlParameter("@EmployeeF_FName", EmployeeF_FName);
                cmd.Parameters.Add(paramEmployeeF_FName);
                MySqlParameter paramEmployeeF_MName = new MySqlParameter("@EmployeeF_MName", EmployeeF_MName);
                cmd.Parameters.Add(paramEmployeeF_MName);
                MySqlParameter paramEmployeeF_LName = new MySqlParameter("@EmployeeF_LName", EmployeeF_LName);
                cmd.Parameters.Add(paramEmployeeF_LName);
                MySqlParameter paramEmployeeF_ID = new MySqlParameter("@EmployeeF_ID", EmployeeF_ID);
                cmd.Parameters.Add(paramEmployeeF_ID);
                MySqlParameter paramEmployeeF_Mobile = new MySqlParameter("@EmployeeF_Mobile", EmployeeF_Mobile);
                cmd.Parameters.Add(paramEmployeeF_Mobile);
                MySqlParameter paramEmployeeF_Email = new MySqlParameter("@EmployeeF_Email", EmployeeF_Email);
                cmd.Parameters.Add(paramEmployeeF_Email);
                MySqlParameter paramEmployeeF_Position = new MySqlParameter("@EmployeeF_Position", EmployeeF_Position);
                cmd.Parameters.Add(paramEmployeeF_Position);
                cmd.ExecuteNonQuery();
                cmd.Connection.Close();

            }
        }
        public static List<Employee> GetAllEmployees()
        {
            List<Employee> listEmployees = new List<Employee>();
            string CS = "server=localhost; userid=; password=; database=; allowuservariables=True";
            using (MySqlConnection connection = new MySqlConnection(CS))
            {
                MySqlCommand cmd = new MySqlCommand("Select * from employees_female", connection);
                connection.Open();
                MySqlDataReader rdr = cmd.ExecuteReader();
                while (rdr.Read())
                {
                    Employee employee = new Employee();
                    employee.EmployeeF_FName = rdr["EmployeeF_FName"].ToString();
                    employee.EmployeeF_MName = rdr["EmployeeF_MName"].ToString();
                    employee.EmployeeF_LName = rdr["EmployeeF_LName"].ToString();
                    employee.EmployeeF_ID = Convert.ToInt32(rdr["EmployeeF_ID"]);
                    employee.EmployeeF_Mobile = rdr["EmployeeF_Mobile"].ToString();
                    employee.EmployeeF_Email = rdr["EmployeeF_Email"].ToString();
                    employee.EmployeeF_Position = rdr["EmployeeF_Position"].ToString();

                    listEmployees.Add(employee);


                }
            }
            return listEmployees;
        }
        public static void DeleteEmployee(int EmployeeF_ID)
        {
            string CS = "server=localhost; userid=; password=; database=; allowuservariables=True";
            using (MySqlConnection connection = new MySqlConnection(CS))
            {
                string UpdateQuery = " Delete from employees_female WHERE EmployeeF_ID=@EmployeeF_ID";
                MySqlCommand cmd = new MySqlCommand(UpdateQuery, connection);
                MySqlParameter param = new MySqlParameter("@EmployeeF_ID", EmployeeF_ID);
                cmd.Parameters.Add(param);
                cmd.Connection.Open();
                cmd.ExecuteNonQuery();
                cmd.Connection.Close();

            }

        }

        public static void InsertEmployee(int EmployeeF_ID, string EmployeeF_FName, string EmployeeF_MName, string EmployeeF_LName, string EmployeeF_Mobile, string EmployeeF_Email, string EmployeeF_Position)
        {
            string CS = "server=localhost; userid=; password=; database=; allowuservariables=True";
            using (MySqlConnection connection = new MySqlConnection(CS))
            {
                string UpdateQuery = " INSERT INTO Employees_female (EmployeeF_FName, EmployeeF_Mname, EmployeeF_LName, EmployeeF_ID, EmployeeF_Mobile, EmployeeF_Email, EmployeeF_Position)" + " VALUES (@EmployeeF_FName,@EmployeeF_MName,@EmployeeF_LName,@EmployeeF_ID,@EmployeeF_Mobile,@EmployeeF_Email,@EmployeeF_Position)";
                MySqlCommand cmd = new MySqlCommand(UpdateQuery, connection);
                MySqlParameter paramEmployeeF_FName = new MySqlParameter("@EmployeeF_FName", EmployeeF_FName);
                cmd.Parameters.Add(paramEmployeeF_FName);
                MySqlParameter paramEmployeeF_MName = new MySqlParameter("@EmployeeF_MName", EmployeeF_MName);
                cmd.Parameters.Add(paramEmployeeF_MName);
                MySqlParameter paramEmployeeF_LName = new MySqlParameter("@EmployeeF_LName", EmployeeF_LName);
                cmd.Parameters.Add(paramEmployeeF_LName);
                MySqlParameter paramEmployeeF_ID = new MySqlParameter("@EmployeeF_ID", EmployeeF_ID);
                cmd.Parameters.Add(paramEmployeeF_ID);
                MySqlParameter paramEmployeeF_Mobile = new MySqlParameter("@EmployeeF_Mobile", EmployeeF_Mobile);
                cmd.Parameters.Add(paramEmployeeF_Mobile);
                MySqlParameter paramEmployeeF_Email = new MySqlParameter("@EmployeeF_Email", EmployeeF_Email);
                cmd.Parameters.Add(paramEmployeeF_Email);
                MySqlParameter paramEmployeeF_Position = new MySqlParameter("@EmployeeF_Position", EmployeeF_Position);
                cmd.Parameters.Add(paramEmployeeF_Position);
                cmd.Connection.Open();
                cmd.ExecuteNonQuery();
                cmd.Connection.Close();
                
            }      
        }
    }

What the code should i write and where?

Results 1 - 5 of 6 12
Posted 10 months ago

Hi yara,

I will get back to you soon.


Posted 10 months ago

hI yara,

After ExecuteNonQuery and connection Close write the below line of code to show the alert message. You just need to change the message as per the functionality(Insert,Update,Delete).

ClientScript.RegisterClientScriptBlock(this.GetType(), "Message", "alert('Record has been inserted successfully.')", true);

 


Posted 10 months ago

when I wrote it in the class of data access layer after connection close, it told me that "ClientScript" doesn't exist in the current context, and keyword "this" is not valid in a static property.

Is the code that you gave me as the below code, means does it give the same output?

 

 string message = "Record has been inserted successfully";
        string script = "window.onload = function(){ alert('";
        script += message;
        script += "')};";
        ClientScript.RegisterStartupScript(this.GetType(), "SuccessMessage", script, true);

 


Posted 10 months ago
yara says:
string message = "Record has been inserted successfully";
       string script = "window.onload = function(){ alert('";
       script += message;
       script += "')};";
       ClientScript.RegisterStartupScript(this.GetType(), "SuccessMessage", script, true);

 Replace above code with below.

string message = "Record has been inserted successfully";
string script = "window.onload = function(){ alert('";
script += message;
script += "')};";
Page page = (Page)HttpContext.Current.CurrentHandler;
ScriptManager.RegisterStartupScript(page, typeof(Page), "ApprovalHistory", script, true);

 


Posted 10 months ago

Thank you, but what the difference between them? since they give the same output.

I agree, here is the link: https://www.e-iceblue.com/Introduce/spire-office-for-net-free.html