Rti
Rti

Reputation: 9

Same data is fetched from the database every time

I have a combobox which is populated with customer Id from the database. When a select button is clicked, the other information from the database should populate the textboxes. But every time I press the select button, the information of the first customer in the database is getting populated in the textboxes irrespective of the customer Id selected from the combobox.

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using MySql.Data.MySqlClient;

namespace BookStore
{
    public partial class Customers : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            // 1. Create the connection string and command string


    string connectionString =
                 "Data Source=localhost;Initial Catalog=arkhambooks;User ID=root;Password=";

            string commandString = "SELECT CustId FROM customer order by CustId";

            // Create the connection object
            MySqlConnection conn = new MySqlConnection(connectionString);

            // Create a command object
            MySqlCommand command = new MySqlCommand(commandString);

            // open the connection
            try
            {
                // open the connection
                conn.Open();

                // attach connection to command object
                command.Connection = conn;

                // get the data reader
                MySqlDataReader reader =
                     command.ExecuteReader(CommandBehavior.CloseConnection);

                // Populate the customer lastname
                customerlist.DataSource = reader;
                customerlist.DataTextField = "CustId";
                customerlist.DataBind();

            }
            finally
            {
                conn.Close();   // make sure the connection closes
            }
            // Disable the update button
            updateButton.Enabled = false;

            // Clear any values in the TextBox controls
            firstnametextbox.Text = "";
            lastnametextbox.Text = "";
            addresstextbox.Text = "";
            citytextbox.Text = "";
            statetextbox.Text = "";
            ziptextbox.Text = "";
            phonetextbox.Text = "";
            emailtextbox.Text = "";


        }

        protected void selectButton_Click(object sender, EventArgs e)
        {
            // 1. Create the connection string and command string
            string connectionString =
                 "Data Source=localhost;Initial Catalog=arkhambooks;User ID=root;Password=";

            string commandString = "SELECT LastName, firstname,address, city, state, zip, phone, email FROM customer " +
                "where CustId = '"+customerlist.Text+"'";

            // Create the connection object
            MySqlConnection conn = new MySqlConnection(connectionString);

            // Create a command object
            MySqlCommand command = new MySqlCommand(commandString);

            // open the connection
            try
            {
                // open the connection
                conn.Open();

                // attach connection to command object
                command.Connection = conn;

                // get the data reader
                MySqlDataReader reader =
                     command.ExecuteReader(CommandBehavior.CloseConnection);

                if (reader.Read())
                {
                    // Display the data
                    firstnametextbox.Text = reader["Firstname"].ToString();
                    lastnametextbox.Text = reader["Lastname"].ToString();
                    addresstextbox.Text = reader["Address"].ToString();
                    citytextbox.Text = reader["City"].ToString();
                    statetextbox.Text = reader["State"].ToString();
                    ziptextbox.Text = reader["Zip"].ToString();
                    phonetextbox.Text = reader["Phone"].ToString();
                    emailtextbox.Text = reader["Email"].ToString();
                }


                // Close the reader
                reader.Close();


                // Enable the Update button
                updateButton.Enabled = true;
            }
            catch
            {
                // Display error message
                dbErrorLabel.Text =
                "Error loading the customer details!<br />";
            }

            finally
            {
                conn.Close();   // make sure the connection closes
            }
        }
    }
}

I am just able to populate the combobox successfully.

Upvotes: 0

Views: 59

Answers (1)

David
David

Reputation: 218798

Every time you post back to the server (such as clicking a button), the first thing that happens is Page_Load. (Well, there are other events before that, but none that you're using.) And in your Page_Load you are clearing and re-populating the customerlist. So by the time the button click event is processed, the default option is again selected.

You only want the logic in your Page_Load event to happen when the page is initially loaded, not on a post-back. Just wrap all of that logic in a condition to check for that:

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        // all of your logic
    }
}

Important side note: Your code is wide open to SQL injection. You should start using parameterized queries.

Upvotes: 3

Related Questions