Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8008009
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T18:00:33+00:00 2026-06-04T18:00:33+00:00

The code below simply gets data from database and populate the data into TextBoxes

  • 0

The code below simply gets data from database and populate the data into TextBoxes on asp.net page and when i click a button btnSave any changes on the TextBoxes reflect to the databse.
But when i click the btnSave button the values i’m getting from the Textboxes are the initial ones from Page_Load Event not the ones after i change values on Textboxes

public partial class Settings : System.Web.UI.Page
{
    string conString = System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;    

    protected void Page_Load(object sender, EventArgs e)
    {
        SqlConnection con = new SqlConnection(conString);
        con.Open();
        SqlCommand cmd = new SqlCommand("SELECT CompanyAUserName, CompanyAPassword, CompanyAEmail, CompanyBUserName, CompanyBPassword, AgentAName, AgentBName, ItemShortageNotification FROM COMPANY_SETTINGS", con);
        SqlDataReader dr = cmd.ExecuteReader();
        while (dr.Read())
        {
            txtHostName.Text =  dr["CompanyAUserName"].ToString();
            txtHostPassword.Text = dr["CompanyAPassword"].ToString();
            txtHostEmail.Text = dr["CompanyAEmail"].ToString();
            txtClientName.Text = dr["CompanyBUserName"].ToString();
            txtClientPassword.Text = dr["CompanyBPassword"].ToString();
            txtAgentAName.Text = dr["AgentAName"].ToString();
            txtAgentBName.Text = dr["AgentBName"].ToString();
            txtItemQty.Text = dr["ItemShortageNotification"].ToString();            
        }
        con.Close();
    }
    protected void btnSave_Click(object sender, EventArgs e)
    {        
        SqlConnection con = new SqlConnection(conString);
        con.Open();                
        SqlCommand cmd = new SqlCommand();
        cmd.Connection = con;        
        try
        {
            cmd.CommandText =
            "UPDATE COMPANY_SETTINGS SET CompanyAUserName = @CompanyAUserName, CompanyAPassword = @CompanyAPassword, CompanyAEmail = @CompanyAEmail, CompanyBUserName = @CompanyBUserName, CompanyBPassword = @CompanyBPassword, AgentAName = @AgentAName, AgentBName = @AgentBName, ItemShortageNotification = @ItemShortageNotification";
            cmd.Parameters.AddWithValue("@CompanyAUserName", txtHostName.Text);
            cmd.Parameters.AddWithValue("@CompanyAPassword", txtHostPassword.Text);
            cmd.Parameters.AddWithValue("@CompanyAEmail", txtHostEmail.Text);
            cmd.Parameters.AddWithValue("@CompanyBUserName", txtClientName.Text);
            cmd.Parameters.AddWithValue("@CompanyBPassword", txtClientPassword.Text);
            cmd.Parameters.AddWithValue("@AgentAName", txtAgentAName.Text);
            cmd.Parameters.AddWithValue("@AgentBName", txtAgentBName.Text);
            cmd.Parameters.AddWithValue("@ItemShortageNotification", txtItemQty.Text.Trim());
            cmd.ExecuteNonQuery();
            Response.Redirect("~/Settings.aspx");
        }
        catch (Exception ex)
        {
            Response.Write("<script>alert('Error: " + ex.Message + "')</script>");            
        }        
        con.Close();        
    }
}

Here is a part of code from .aspx page

<asp:Content ID="Content3" ContentPlaceHolderID="ContentPlaceHolderBody" Runat="Server">
    <div style="height:100%;" runat="server" id="divSettings">        
        <h3 style="padding:0; margin:0">Settings</h3>
        <h5 style="padding: 5px">(This page is only visible to the host)</h5>
        <table class="SignUpTable" runat="server">
            <tr runat="server">
                <td align="center" colspan="2" runat="server">
                    <h1>Company A Information</h1>
                    <p>This is host account infromation. </p>
                </td>
            </tr>
            <tr runat="server">
                <td class="label" runat="server" >
                    <asp:Label ID="UserNameLabel" runat="server" AssociatedControlID="UserNameLabel">User Name: </asp:Label><br />
                    <div class="small"> Minimun of 4 characters </div>
                </td>
                <td runat="server">
                    <asp:TextBox ID="txtHostName" runat="server" CssClass="textBox"></asp:TextBox><br />
                    <asp:RequiredFieldValidator ID="UserNameRequired" runat="server" 
                        ControlToValidate="txtHostName" Text="Host name is required." 
                        ToolTip="Enter Host Name" Display="Dynamic" 
                        ValidationGroup="ValidationSettings" SetFocusOnError="True"/>
                    <asp:RegularExpressionValidator ID="RegularExpressionValidator1" SetFocusOnError="True"
                            runat="server" ControlToValidate="txtHostName" Display="Dynamic" 
                        ValidationExpression="[0-9a-zA-Z]{4,}" ValidationGroup="ValidationSettings">Host name must be more than 4 characters.</asp:RegularExpressionValidator>
                </td>
            </tr> 

I tried to use FindControl() and still getting the same value.
Please tell me know what i am missing. Thank you.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T18:00:35+00:00Added an answer on June 4, 2026 at 6:00 pm

    Change your code to add a check to IsPostBack. What’s happening is you are loading the data before the btn_Save event fires.

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
           loadData();
    }
    
    private void loadData()
    {
    
        SqlConnection con = new SqlConnection(conString);
        con.Open();
        SqlCommand cmd = new SqlCommand("SELECT CompanyAUserName, CompanyAPassword, CompanyAEmail, CompanyBUserName, CompanyBPassword, AgentAName, AgentBName, ItemShortageNotification FROM COMPANY_SETTINGS", con);
        SqlDataReader dr = cmd.ExecuteReader();
        while (dr.Read())
        {
            txtHostName.Text =  dr["CompanyAUserName"].ToString();
            txtHostPassword.Text = dr["CompanyAPassword"].ToString();
            txtHostEmail.Text = dr["CompanyAEmail"].ToString();
            txtClientName.Text = dr["CompanyBUserName"].ToString();
            txtClientPassword.Text = dr["CompanyBPassword"].ToString();
            txtAgentAName.Text = dr["AgentAName"].ToString();
            txtAgentBName.Text = dr["AgentBName"].ToString();
            txtItemQty.Text = dr["ItemShortageNotification"].ToString();            
        }
        con.Close();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a data that looks like this . And my code below simply
I started switching some pre-existing nHibernate code in a Sharepoint-based ASP.NET project from eager
I am testing this simple code below and it works... $(document).ready( function() { $('.show-modal').click(
(Code below has been updated and worked properly) There is dynamic OrderBy sample from
I'm using the Data Annotation validation extensively in ASP.NET MVC 2. This new feature
update: the answer from virtualeyes (below) looks pretty nice: but now a bit code-sanititzing
I have written some code using jQuery to use Ajax to get data from
I have the following code to fetch the data from URL, store it in
When i run the code below, the iteration starts from 0. I would like
I have a simple code below: import java.util.ArrayList; public class BoidList extends ArrayList {

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.