2013-12-09 215 views
0

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

在我的表現層,在下拉列表項中選擇:

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

而我的執行方法,讀者在相互分離的類:

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)還是?

在此先感謝。

+0

我相信你將需要讓它回來後,以填充它。或者將其綁定到數據庫。 – Jmoreland91

+0

我發現我的錯誤了。我忘了在下拉列表中啓用回帖。謝謝 –

+2

您有一個SQL注入漏洞。 – SLaks

回答

1

你發現自己的錯誤,但要避免SQL注入,修改您的代碼是這樣的:

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); 
+0

Sql注入的負面影響是什麼? –

+0

使用SQL注入攻擊者可以訪問存儲在SQL數據庫中的所有數據。如果SQL服務器配置不正確,可能會執行任意代碼。 –

相關問題