【发布时间】:2013-12-09 14:56:11
【问题描述】:
我正在尝试从下拉列表中选择动态数据并查看所选项目的详细信息。我用数据库中的数据填充下拉列表没有问题。但是,当我选择一些项目时,它不会显示详细信息。
在我的表示层中,当下拉列表项被选中时:
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 注入漏洞。
标签: c# sql html-select