locked
Display success or failure message on page from a database transaction RRS feed

  • Question

  • User-253940806 posted

    I'm wanting to display a success or failure message on page from a database transaction. When the user clicks on the submit button, I want to display a confirmation message to the user on the page from the database.

    Example:

    The data has been saved.

    Thursday, May 31, 2018 12:09 PM

All replies

  • User632428103 posted

    Hello splitfire,

    here is it a little sample of sql transaction in code behind but your can also made it on your store procedure ...

    string message = string.Empty;
                string ConnStr = System.Configuration.ConfigurationManager.ConnectionStrings["db connection string"].ConnectionString;
                string SQLOne = "INSERT INTO tbl ...";
                string SQLTwo = "UPDATE tbl WHERE ...";
    
                using (SqlConnection conn = new SqlConnection(ConnStr))
                {
                    SqlTransaction transaction = null;
                    try
                    {
                        conn.Open();
                        transaction = conn.BeginTransaction();
    
                        //
                        using (SqlCommand cmd = new SqlCommand(SQLOne, conn, transaction)) { cmd.ExecuteNonQuery(); }
                        using (SqlCommand cmd = new SqlCommand(SQLTwo, conn, transaction)) { cmd.ExecuteNonQuery(); }
    
                        //sucess
                        transaction.Commit();
                        message = "Items has been saved.";
    
                        return message
                    }
                    catch (Exception ex)
                    {
                        transaction.Rollback();
                        message = "Error ...";
                    }
                }
    
                return message;

    Thursday, May 31, 2018 12:42 PM
  • User-330142929 posted

    Hi Splitfire,

    Based on your description, you want to display a success or failure message on page from a database transaction. I make a demo, hope it will be useful for you.

    .aspx File:

        <form id="form1" runat="server">
            <div>
                Enter the ID you want to delete: <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
                <br />
                <asp:Button ID="Button1" runat="server" Text="Try to Delete" OnClick="Button1_Click" />
            </div>
        </form>

    Code Behind.

    public string  ProcessTransaction(string id)
            {
                string message = "";
                SqlConnection connection = new SqlConnection(@"Data Source=(localdb)\MSSQLLocalDB;Initial Catalog=DataStore;Integrated Security=True;Connect Timeout=30;Encrypt=False;TrustServerCertificate=False;ApplicationIntent=ReadWrite;MultiSubnetFailover=False");
                connection.Open();
                SqlTransaction transaction= connection.BeginTransaction();
                SqlCommand command = new SqlCommand();
                command.CommandText = "delete from MyTime where Id=" + id;
                command.CommandType = System.Data.CommandType.Text;
                command.Connection = connection;
                command.Transaction = transaction;
                try
                {
                    command.ExecuteNonQuery();
                    transaction.Commit();
                    message = "Delete operation succeeded";
                }
                catch (Exception)
                {
                    transaction.Rollback();
                    message = "An error occurred";
                }
                finally
                {
                    connection.Close();
                }
                return message;
            }
     
            protected void Button1_Click(object sender, EventArgs e)
            {
                string message = string.Empty;
                string id = this.TextBox1.Text;
                if (!string.IsNullOrEmpty(id))
                {
                    message=ProcessTransaction(id);
                }
                ClientScript.RegisterStartupScript(this.GetType(), "", "alert('" + message + "')", true);
            }

    Gif how it works.

    https://www.screencast.com/t/1jDEmkjUYS

    If you still have any question, please feel free to let me know.

    Best Regards,

    Abraham

    Monday, June 4, 2018 3:34 AM