email and phone textbox update log table

 To create a .NET C# Web Forms application that updates an email and phone number textbox, sends a "Yes" message notification upon update, and inserts data into an MSSQL table with logging based on if-else conditions, follow these steps:


<html xmlns="http://www.w3.org/1999/xhtml">

<head runat="server">

    <title></title>

</head>

<body>

    <form id="form1" runat="server">

         <div>

            <asp:Label ID="lblEmail" runat="server" Text="Email: "></asp:Label>

            <asp:TextBox ID="txtEmail" runat="server"></asp:TextBox>

            <br />

            <asp:Label ID="lblPhone" runat="server" Text="Phone: "></asp:Label>

            <asp:TextBox ID="txtPhone" runat="server"></asp:TextBox>

            <br />

            <asp:Button ID="btnUpdate" runat="server" Text="Update" OnClick="btnUpdate_Click" />

            <br />

            <asp:Label ID="lblNotification" runat="server" Text=""></asp:Label>

        </div>

    </form>

</body>

</html>





===========

protected void Page_Load(object sender, EventArgs e)

{


}

protected void btnUpdate_Click(object sender, EventArgs e)

{

    string email = txtEmail.Text.Trim();

    string phone = txtPhone.Text.Trim();


    if (!string.IsNullOrEmpty(email) && !string.IsNullOrEmpty(phone))

    {

        string connectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;


        using (SqlConnection conn = new SqlConnection(connectionString))

        {

            conn.Open();

            SqlTransaction transaction = conn.BeginTransaction();


            try

            {

                // Insert into the main table

                string updateQuery = "INSERT INTO C_ContactInfo (Email, Phone) VALUES (@Email, @Phone)";

                using (SqlCommand cmd = new SqlCommand(updateQuery, conn, transaction))

                {

                    cmd.Parameters.AddWithValue("@Email", email);

                    cmd.Parameters.AddWithValue("@Phone", phone);

                    cmd.ExecuteNonQuery();

                }


                // Insert into the log table

                string logQuery = "INSERT INTO C_ContactInfoLog (Email, Phone, UpdateDate) VALUES (@Email, @Phone, @UpdateDate)";

                using (SqlCommand cmd = new SqlCommand(logQuery, conn, transaction))

                {

                    cmd.Parameters.AddWithValue("@Email", email);

                    cmd.Parameters.AddWithValue("@Phone", phone);

                    cmd.Parameters.AddWithValue("@UpdateDate", DateTime.Now);

                    cmd.ExecuteNonQuery();

                }


                transaction.Commit();

                lblNotification.Text = "Yes";

            }

            catch (Exception ex)

            {

                transaction.Rollback();

                lblNotification.Text = "Error updating contact information: " + ex.Message;

            }

        }

    }

    else

    {

        lblNotification.Text = "Email and Phone are required.";

    }

}




===========




CREATE TABLE C_ContactInfo (

    Id INT PRIMARY KEY IDENTITY,

    Email NVARCHAR(255) NOT NULL,

    Phone NVARCHAR(50) NOT NULL

);


CREATE TABLE C_ContactInfoLog (

    Id INT PRIMARY KEY IDENTITY,

    Email NVARCHAR(255) NOT NULL,

    Phone NVARCHAR(50) NOT NULL,

    UpdateDate DATETIME NOT NULL

);

Post a Comment

0 Comments