2011-01-27 53 views
0

我有一个客户表 - >CustomerNumberCustomerName从另一个表获得一个数据DropownList用的AutoPostBack

我有一个Sales表 - >CustomerName

我有一个Label(代表CUSTOMERNUMBER )和DropDownList(代表客户名称)

我到DropDownList销售表 - >客户名称与SqlDataSource

我想全自动(带的AutoPostBack)填充CUSTOMERNUMBER标签哪个客户名称中的DropDownList

例SQL选自:

选择A.CustomerNumber 从客户A,销售乙 其中B.CustomerName = DropDownList1。 SelectedItems.Value

我在这样想。

我该怎么做?

protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e) 
     { 

     } 
+0

你是正确的方向。我看到你到达那里。 – Pradeep 2011-01-27 07:17:58

回答

2

保护无效DropDownList1_SelectedIndexChanged(对象发件人,EventArgs的)

{

string selectedName = DropDownList1.SelectedValue; 

string sqlQuery = "select A.CustomerNumber from Customer A, Sales B where B.CustomerName = '" + DropDownList1.SelectedValue + "'"; 
// pass you sql query to command object and call execute scalar method 
label1.Text = dbCommand.ExecuteScalar().ToString(); 

}

什么ExecuteScalar呢?

+0

我可以在哪里使用dbCommand? – 2011-01-27 07:30:55

3

试试这个

protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e) 
{ 
if(-1 != DropDownList1.SelectedIndex) 
{ 
using(SqlConnection connection = new SqlConnection("connectionString")) 
{ 
connection.Open(); 
using(SqlCommand command = new SqlCommand("SELECT A.CUSTOMERNUMBER FROM CUSTOMER A, SALES B WHERE B.CUSTOMERNAME = @CustomerName")) 
{ 
command.Connection = connection; 
command.Parameters.AddWithValue("@CustomerName", DropDownlist1.SelectedValue.ToString()); 
this.Label1.Text = command.ExecuteScalar().ToString(); 
} 
} 
} 
} 

希望工程。 声明:我没有测试代码

相关问题