2012-08-14 33 views
0

我需要根據以前的下拉列表值獲取值。但我很困惑,因爲有3個表是依賴的。基於3個表格的DropDown依賴關係

dbo.Client 
ClientID(PK) | ClientName 

dbo.Client_POC_Bridge 
ClientID(FK) | POCID (FK) 

dbo.PointOfContact 
POCID(PK) | FName | LName 

我有2個下拉列表:

DropDownList1:到目前爲止,我已經綁定客戶端的表信息

DropDownList2: I need FName and Lname from dbo.PointOfContact 

,這是代碼,但它不工作..

protected void DropDownList4_SelectedIndexChanged(object sender, EventArgs e) 
     { 

      DropDownList6.Items.Clear(); 
      DropDownList6.Items.Add(new ListItem("--Select Point Of Contact--", "")); 
      DropDownList6.AppendDataBoundItems = true; 

      String var = System.Configuration.ConfigurationManager.ConnectionStrings["KKSTechConnectionString"].ConnectionString; 

      String strQuery = "select FirstName, POCID from PointOfCContact " + 
           "where [email protected]"; 
      SqlConnection con = new SqlConnection(var); 
      SqlCommand cmd = new SqlCommand(); 
      cmd.Parameters.AddWithValue("@ClientID", 
       DropDownList3.SelectedItem.Value); 
      cmd.CommandType = CommandType.Text; 
      cmd.CommandText = strQuery; 
      cmd.Connection = con; 
      try 
      { 
       con.Open(); 
       DropDownList6.DataSource = cmd.ExecuteReader(); 
       DropDownList6.DataTextField = "FirstName"; 
       DropDownList6.DataValueField = "POCID"; 
       DropDownList6.DataBind(); 
       if (DropDownList6.Items.Count > 1) 
       { 
        DropDownList6.Enabled = true; 
       } 
       else 
       { 
        DropDownList6.Enabled = false; 

       } 
      } 
      catch (Exception ex) 
      { 
       throw ex; 
      } 
      finally 
      { 
       con.Close(); 
       con.Dispose(); 
      } 

     } 
+0

檢查http://msdn.microsoft.com/en-us/library/aa581792.aspx – Peru 2012-08-14 09:25:31

回答

0

更改以下行...使用Inner Join獲取值...

String strQuery ="select pof.FirstName, pof.POCID from PointOfCContact pof Inner Join Client_POC_Bridge cpb On pof.POCID =cpb.POCID where [email protected]"; 
+0

不錯..但它不從dbo.Client搜索..我們是否應該加入,然後搜索@ClientrID ?? – Girish 2012-08-14 09:36:09

+0

如果你想讓FirstName和POCID爲retreive,那麼就不需要使用dbo.Client,但是如果你想要「ClientName」,那麼你必須加入dbo.Client才能獲得... – 2012-08-14 09:40:34

+0

@Amol ..工作感謝一堆:) – Girish 2012-08-14 09:52:10