troubleshooting Question

Asp Webform Textboxes not updating to new values

Avatar of MattStewart01
MattStewart01Flag for United States of America asked on
ASP.NET ProgrammingC#
6 Comments1 Solution606 ViewsLast Modified:
I am having an issue with an ASP C# webform. I am populating the form fields on the page load, then making changes to some of the fields. When I submit the form, it is supposed to update the variables to the new text in the form fields and update the database. The issue is that when I submit the form, the textboxes seem to save the old values instead of updating to the changed value.

The form fields that I am having issues with are tbNotes, ddReason, ddMethodOfClosure

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;

namespace CallTrackingApplication
{
    public partial class OpenCallEdit : System.Web.UI.Page
    {
        // Declare Variables
        int callID;
        int resolved;
        String now = DateTime.Now.ToString("MM/dd/yyyy hh:mm:ss");
        String companyName;
        String participantLastName;
        String participantFirstName;
        String representative;
        String reason;
        String closeMethod;
        String notes;
        String timeDateOfClose;
        DateTime timeDate;

        protected void Page_Load(object sender, EventArgs e)
        {

            // Hide Error Lables
            lblCompanyName.Visible = false;
            lblParticipantLastName.Visible = false;
            lblParticipantFirstName.Visible = false;
            lblTimeDate.Visible = false;
            lblRepresentative.Visible = false;
            lblReason.Visible = false;
            lblMethodOfClosure.Visible = false;
            lblNotes.Visible = false;
            lblError.Visible = false;

                // Set callID
                if (Request.QueryString["callID"] != null)
                {
                    callID = int.Parse(Request.QueryString["callID"]);

                }
                else
                {
                    callID = -1;
                    Response.Redirect("OpenCalls.aspx");
                }

                // Get call data
                SqlConnection conn;
                conn = new SqlConnection(@"Data Source=Server-SQL-011;Initial Catalog=MyDB;Persist Security Info=True;User ID=MyUser;Password=MyPassword");

                // Open Connection
                conn.Open();


                // SQL Query for companyName
                SqlCommand cmdCompanyName = new SqlCommand("SELECT CompanyName FROM CallTracking WHERE CallID = @callID", conn);

                // Add parameters to Query                
                cmdCompanyName.Parameters.AddWithValue("@callID", callID);

                // Execute Query and Get Value            
                companyName = (string)cmdCompanyName.ExecuteScalar();



                // SQL Query for participantLastName
                SqlCommand cmdParticipantLastName = new SqlCommand("SELECT ParticipantLastName FROM CallTracking WHERE CallID = @callID", conn);

                // Add parameters to Query                
                cmdParticipantLastName.Parameters.AddWithValue("@callID", callID);

                // Execute Query and Get Value            
                participantLastName = (string)cmdParticipantLastName.ExecuteScalar();



                // SQL Query for participantFirstName
                SqlCommand cmdParticipantFirstName = new SqlCommand("SELECT ParticipantFirstName FROM CallTracking WHERE CallID = @callID", conn);

                // Add parameters to Query                
                cmdParticipantFirstName.Parameters.AddWithValue("@callID", callID);

                // Execute Query and Get Value            
                participantFirstName = (string)cmdParticipantFirstName.ExecuteScalar();



                // SQL Query for timeDate
                SqlCommand cmdTimeDate = new SqlCommand("SELECT CallTime FROM CallTracking WHERE CallID = @callID", conn);

                // Add parameters to Query                
                cmdTimeDate.Parameters.AddWithValue("@callID", callID);

                // Execute Query and Get Value            
                timeDate = (DateTime)cmdTimeDate.ExecuteScalar();



                // SQL Query for representative
                SqlCommand cmdRepresentative = new SqlCommand("SELECT Representative FROM CallTracking WHERE CallID = @callID", conn);

                // Add parameters to Query                
                cmdRepresentative.Parameters.AddWithValue("@callID", callID);

                // Execute Query and Get Value            
                representative = (string)cmdRepresentative.ExecuteScalar();



                // SQL Query for reason
                SqlCommand cmdReason = new SqlCommand("SELECT Reason FROM CallTracking WHERE CallID = @callID", conn);

                // Add parameters to Query                
                cmdReason.Parameters.AddWithValue("@callID", callID);

                // Execute Query and Get Value            
                reason = (string)cmdReason.ExecuteScalar();



                // SQL Query for closeMethod
                SqlCommand cmdCloseMethod = new SqlCommand("SELECT CloseMethod FROM CallTracking WHERE CallID = @callID", conn);

                // Add parameters to Query                
                cmdCloseMethod.Parameters.AddWithValue("@callID", callID);

                // Execute Query and Get Value            
                closeMethod = (string)cmdCloseMethod.ExecuteScalar();



                // SQL Query for resolved
                SqlCommand cmdResolved = new SqlCommand("SELECT Resolved FROM CallTracking WHERE CallID = @callID", conn);

                // Add parameters to Query                
                cmdResolved.Parameters.AddWithValue("@callID", callID);

                // Execute Query and Get Value            
                resolved = (int)cmdResolved.ExecuteScalar();


                // SQL Query for notes
                SqlCommand cmdNotes = new SqlCommand("SELECT Notes FROM CallTracking WHERE CallID = @callID", conn);

                // Add parameters to Query                
                cmdNotes.Parameters.AddWithValue("@callID", callID);

                // Execute Query and Get Value            
                notes = (string)cmdNotes.ExecuteScalar();


                // Close Connection
                conn.Close();

                // Populate the textboxes
                tbCompanyName.Text = companyName;
                tbParticipantLastName.Text = participantLastName;
                tbParticipantFirstName.Text = participantFirstName;
                tbTimeDate.Text = timeDate.ToString();
                tbRepresentative.Text = representative;
                ddReason.Text = reason;
                ddMethodOfClosure.Text = closeMethod;
                tbNotes.Text = notes;


                if (resolved == 1)
                {
                    cbResolved.Checked = true;
                }
        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            if (tbCompanyName.Text == "")
            {
                lblCompanyName.Text = "Please Enter A Value for 'Company Name'.";
                lblCompanyName.Visible = true;
                lblError.Text = "Error.";
                lblError.Visible = true;
            }
            else if (tbParticipantLastName.Text == "")
            {
                lblParticipantLastName.Text = "Please Enter A Value for 'Participant Last Name'.";
                lblParticipantLastName.Visible = true;
                lblError.Text = "Error.";
                lblError.Visible = true;
            }
            else if (tbParticipantFirstName.Text == "")
            {
                lblParticipantFirstName.Text = "Please Enter A Value for 'Participant First Name'.";
                lblParticipantLastName.Visible = true;
                lblError.Text = "Error.";
                lblError.Visible = true;
            }
            else if (tbTimeDate.Text == "")
            {
                lblTimeDate.Text = "Please Enter A Value for 'Time/Date of Call'.";
                lblTimeDate.Visible = true;
                lblError.Text = "Error.";
                lblError.Visible = true;
            }
            else if (tbRepresentative.Text == "")
            {
                lblRepresentative.Text = "Please Enter A Value for 'Representitive'.";
                lblRepresentative.Visible = true;
                lblError.Text = "Error.";
                lblError.Visible = true;
            }
            else if (ddReason.Text == "")
            {
                lblReason.Text = "Please Enter A Value for 'Reason for Call'.";
                lblReason.Visible = true;
                lblError.Text = "Error.";
                lblError.Visible = true;
            }
            else if (tbNotes.Text == "")
            {
                lblNotes.Text = "Please Enter A Value for 'Notes'.";
                lblNotes.Visible = true;
                lblError.Text = "Error.";
                lblError.Visible = true;
            }
            else
            {
                // Set Variables for SQL Insert;
                reason = ddReason.Text;
                closeMethod = ddMethodOfClosure.Text;
                notes = tbNotes.Text;

                if (cbResolved.Checked == true)
                {
                    resolved = 1;
                    timeDateOfClose = now;
                }
                else
                {
                    resolved = 0;
                    timeDateOfClose = null;
                }


                // Set Connection String
                System.Data.SqlClient.SqlConnection dataConnection = new SqlConnection();
                dataConnection.ConnectionString =
                    // @"Server=SQL-011;Database=MyDB;Trusted_Connection= True";
                   @"Data Source=SQL-011;Initial Catalog=MyDB;Persist Security Info=True;User ID=MyUser;Password=MyPassword";

                System.Data.SqlClient.SqlCommand dataCommand = new SqlCommand();
                dataCommand.Connection = dataConnection;


                if (resolved == 1)
                {

                    // SQL Query
                    dataCommand.CommandText = ("UPDATE CallTracking SET [Reason] = @reason,[Resolved] = @resolved,[CloseTime] = @timeDateOfClose,[CloseMethod] = @methodOfClosure,[Notes] = @notescomments WHERE CallID = @callID");

                    // Add parameters to Query
                    dataCommand.Parameters.AddWithValue("@reason", reason);
                    dataCommand.Parameters.AddWithValue("@resolved", resolved);
                    dataCommand.Parameters.AddWithValue("@timeDateOfClose", timeDateOfClose);
                    dataCommand.Parameters.AddWithValue("@methodOfClosure", closeMethod);
                    dataCommand.Parameters.AddWithValue("@notesComments", notes);
                    dataCommand.Parameters.AddWithValue("@callID", callID);

                }
                else
                {

                    // SQL Query
                    dataCommand.CommandText = ("UPDATE CallTracking SET [Reason] = @reason,[Resolved] = @resolved,[CloseMethod] = @methodOfClosure,[Notes] = @notescomments WHERE CallID = @callID");
                    
                    // Add parameters to Query
                    dataCommand.Parameters.AddWithValue("@reason", reason);
                    dataCommand.Parameters.AddWithValue("@resolved", resolved);
                    dataCommand.Parameters.AddWithValue("@methodOfClosure", closeMethod);
                    dataCommand.Parameters.AddWithValue("@notesComments", notes);
                    dataCommand.Parameters.AddWithValue("@callID", callID);

                }


                    // Open DB Connection
                    dataConnection.Open();

                    // Execute Query
                    dataCommand.ExecuteNonQuery();

                    //Close DB Connection
                    dataConnection.Close();

                    // Show Success Message
                    lblError.Text = "Call Successfully Updated.";
                    lblError.Visible = true;

                
            }


        }
    }
}

Thanks for any help.
Join the community to see this answer!
Join our exclusive community to see this answer & millions of others.
Unlock 1 Answer and 6 Comments.
Join the Community
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 6 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros