簡體   English   中英

下拉列表選擇項

[英]Drop Down List Selected Item

我正在嘗試從下拉列表中選擇動態數據,並查看所選項目的詳細信息。 在用數據庫中的數據填充下拉列表時,我沒有問題。 但是,當我選擇某些項目時,它不會顯示細節。

在我的表示層中,當選擇下拉列表項時:

protected void ddlScheduleList_SelectedIndexChanged(object sender, EventArgs e)
    {
        string address = ddlScheduleList.SelectedItem.ToString();

        Distribution scheduledIndv = new Distribution();

        scheduledIndv = packBLL.getDistributionDetail(address);

        if (scheduledIndv == null)
        {
            Console.Out.WriteLine("Null");
        }
        else
        {
            tbScheduleDate.Text = scheduledIndv.packingDate.ToString();
            tbBeneficiary.Text = scheduledIndv.beneficiary;
        }

    }

在我的業務邏輯層中,我獲得了所選地址並將其傳遞給數據訪問層:

public Distribution getDistributionDetail(string address)
    {
        Distribution scheduledIndv = new Distribution();

        return scheduledIndv.getDistributionDetail(address);
    }

在我的數據訪問層中,我已經測試了SQL語句。 它給了我我想要的東西。 但是它只是不會顯示在網頁上。

public Distribution getDistributionDetail(string address)
    {
        Distribution distributionFound = null;

        using (var connection = new SqlConnection(FoodBankDB.GetConnectionString())) // get your connection string from the other class here
        {
            SqlCommand command = new SqlCommand("SELECT d.packingDate, b.name FROM dbo.Distributions d " +
            " INNER JOIN dbo.Beneficiaries b ON d.beneficiary = b.id " +
            " WHERE b.addressLineOne = '" + address + "'", connection);
            connection.Open();

            using (var dr = command.ExecuteReader())
            {
                if (dr.Read())
                {
                    DateTime packingDate = DateTime.Parse(dr["packingDate"].ToString());
                    string beneficiary = dr["beneficiary"].ToString();

                    distributionFound = new Distribution(packingDate, beneficiary);
                }
            }
        }
        return distributionFound;
    }

我的執行Reader方法在另一個單獨的類中:

 public static string connectionString = Properties.Settings.Default.connectionString;

    public static string GetConnectionString()
    {
        return connectionString;
    }

public static SqlDataReader executeReader(string query)
    {
        SqlDataReader result = null;

        System.Diagnostics.Debug.WriteLine("FoodBankDB executeReader: " + query);

        SqlConnection connection = new SqlConnection(connectionString);
        SqlCommand command = new SqlCommand(query, connection);
        connection.Open();
        result = command.ExecuteReader();
        connection.Close();

        return result;
    }

我想知道出了什么問題。 是關於(!IsPostBack)還是?

提前致謝。

您發現了錯誤,但是為了避免Sql Injection,請像這樣修改代碼:

    SqlCommand command = new SqlCommand("SELECT d.packingDate, b.name FROM dbo.Distributions d " +
                " INNER JOIN dbo.Beneficiaries b ON d.beneficiary = b.id " +
                " WHERE b.addressLineOne = @address", connection);

    command.Parameters.AddWithValue("@address", address);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM