每次都从数据库中获取相同的数据

时间:2019-05-10 20:58:04

标签: c# html asp.net

我有一个组合框,其中装有数据库中的客户ID。单击选择按钮时,数据库中的其他信息应填充文本框。但是每次我按下选择按钮时,无论从组合框中选择的客户ID如何,文本框中都会填充数据库中第一个客户的信息。

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
            }
        }
    }
}

我能够成功填充组合框。

1 个答案:

答案 0 :(得分:3)

每次您发回服务器(例如单击按钮)时,第一件事就是Page_Load。 (嗯,在那之前还有其他事件,但是您没有使用。)然后在Page_Load中清除并重新填充customerlist。因此,在处理按钮单击事件时,将再次选择默认选项。

您只希望Page_Load事件中的逻辑在最初加载页面时发生,而不是在回发时发生。只需将所有逻辑包装在一个条件中即可进行检查:

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

重要的旁注:您的代码对 SQL注入完全开放的。您应该开始使用parameterized queries