2013-03-25 51 views
0

我不知道標題中究竟寫什麼。如果你認爲它是一個不正確的標題,我會改變它。 繼承人的問題。我將一個下拉列表綁定到一個數據集(一個表),我需要的字段如Name,AddressLine1,AddressLine2,City,Email,Country等等。我想在標籤上顯示這些字段(值)。下面是完整的代碼:將列指定給數據集

 public String GetSessionObject() 
    { 
     string strSession = ""; 
     if (HttpContext.Current.Session["SessionEmail"] != null) 
     { 
      strSession = HttpContext.Current.Session["SessionEmail"].ToString(); 

     } 
     return strSession; 
    } 

    public DataSet BindDropDownListToAUserAddress() 
    { 

     DataSet ds = new DataSet(); 
     SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.AppSettings["ConnectionString"]); 
     con.Open(); 
     string strQuery = "SELECT *, FirstName +' '+ LastName as FullName from AUserAddress where AUser_ID = (Select ID from AUser where Email='" + GetSessionObject() + "')"; 
     SqlCommand cmd = new SqlCommand(strQuery, con); 
     using (SqlDataAdapter da = new SqlDataAdapter(cmd)) 
     da.Fill(ds, "AUserAddress"); 
     con.Close(); 
     return ds; 
    } 


    ddlName.DataSource = objBindDDL.BindDropDownListToAUserAddress().Tables["AUserAddress"]; 
      ddlName.DataTextField = "FullName"; 
      ddlName.DataBind(); 
      lblDisplayAddressLine1.Text = objBindDDL.BindDropDownListToAUserAddress().Tables["AUserAddress"].Columns.("AddressLine1").ToString();-----------???? 

這就是我卡住的地方。我需要來自特定列的值才能顯示特定的標籤。我有什麼選擇?請指導....

+0

很抱歉,但我沒有得到你的問題。您是否要在標籤中顯示所選用戶的地址?那麼你應該使用'selectedIndexChanged'事件。什麼是你的DDL的DataValue字段? – Sachin 2013-03-25 10:19:48

+0

看到我會告訴你...我想使用創建的數據集(中間代碼)在標籤上顯示文本(地址,城市,州等)......它不在選定的indexchanged上,它應該在頁面上。 – adityawho 2013-03-25 10:23:26

+0

我已經發布了我的答案,看看那個。 – Sachin 2013-03-25 10:36:26

回答

0

我有什麼理解你的問題,你可以做到這一點

// Get User's Details 
    DataSet ds=BindDropDownListToAUserAddress(); 

    // Now from this dataset you can get the specific column like this 
    if(ds!=null && ds.Tables.Count>0) 
    { 
    // I am assuming that your table contains single row of specific user 
    string AddressLine1= ds.Tables[0].Rows[0]["AddressLine1"].ToString(); 
    string AddressLine2= ds.Tables[0].Rows[0]["AddressLine2"].ToString(); 
    } 
+0

是的,我的表格包含一個用戶的單行。 所以在這之後,我這樣做:lblAddressLine1.Text = AddressLine1 rt? – adityawho 2013-03-25 10:39:11

+0

另外,爲什麼表[0] .row [0] ..我的意思是爲什麼零? – adityawho 2013-03-25 10:40:24

+0

是的,即使你可以直接寫'lblAddressLine1.Text = ds.Tables [0] .Row [0] [「AddressLine1」]。ToString();'不需要帶入單獨的變量,然後爲其分配文本框文本。 – Sachin 2013-03-25 10:40:41