Note: Forums will be making significant UX changes to address key usability improvements surrounding search, discoverability and navigation. To learn more about these changes please visit the announcement which can be found HERE.

Answered Get user account from custom list

  • 2012년 8월 16일 목요일 오전 9:21
     
     

    Hi,

    I add user account using 'user field' and save into custom list.

    I can save account name and can see inside the list.

    But when I pull out this user account value in other from, user account field is empty.

    Below is my sample code:

    SPListItemCollection collListItems1 = oList1.GetItems(spQuery1);            
                DataTable dt1 = oList1.GetItems(spQuery1).GetDataTable();           
                dt1 = collListItems1.GetDataTable();           

                        if (dt1.Columns["UserAccount"] != null)
                        {

                             ddlAcount.DataSource = dt1;

                            ddlAcount.DataTextField = dt1.Columns["UserAccount"].ToString();
                            ddlAcount.DataValueField = dt1.Columns["UserAccount"].ToString();
                            ddlAcount.DataBind();
                        }


    Best Regards, wendy

모든 응답

  • 2012년 8월 16일 목요일 오전 9:47
     
     

    Hello,

    can you please share the caml query for your code


    Hiren Patel | Please click "Propose As Answer" if this post solves your problem or "Vote As Helpful" if this post has been useful to you.

  • 2012년 8월 16일 목요일 오전 10:01
     
     제안된 답변 코드 있음

    Hey,

    I think the problem is that in: ddlAcount.DataTextField = dt1.Columns["UserAccount"].ToString();

    Let me explain, lets say the user account is User1, what the code is doing in essence is saying that the DataTextField must bind to the column "User1", it's showing no values because obviously there is no column like that. Instead your code should be:

    ddlAcount.DataTextField = "UserAccount";
    ddlAcount.DataValueField  = "UserAccount";

    This will then point to your column, and thus show the correct values.

    Thanks and have a great day!


    I find it distasteful to beg for 'Mark as Answer' and 'Mark as helpful'. It's supposed to be about helping people, not about getting the high score.

    • 답변으로 제안됨 Borislav Grgić 2012년 8월 16일 목요일 오전 10:32
    •  
  • 2012년 8월 16일 목요일 오전 10:14
     
      코드 있음

    hi

    i do not know what is ddlAcount, but if u want to get UserAccount value, it most be like bellow:

    foreach (DataRow row in dt1.Rows)
                {
                    if (row["UserAccount"] != null)
                    {
                        string UserAccountValue = row["UserAccount"].ToString();
                    }
                }
    if ddlaccount is a dropdown list i agree with Werners answer


    yaşamak bir eylemdir


    • 편집됨 Ilker Alizade 2012년 8월 16일 목요일 오전 10:27
    •  
  • 2012년 8월 16일 목요일 오전 10:30
     
      코드 있음

    hi

    i do not know what is ddlAcount, but if u want to get UserAccount value, it most be like bellow:

    foreach (DataRow row in dt1.Rows)
                {
                    if (row["UserAccount"] != null)
                    {
                        string UserAccountValue = row["UserAccount"].ToString();
                    }
                }
    if ddlaccount is a dropdown list i agree with Werners answer


    yaşamak bir eylemdir


    From the naming convention, I'm assuming it's a dropdown list


    I find it distasteful to beg for 'Mark as Answer' and 'Mark as helpful'. It's supposed to be about helping people, not about getting the high score.

  • 2012년 8월 16일 목요일 오전 11:22
     
      코드 있음

    Hi,

    I just pass value to 'ddlAccount' field.

    I also write using this one

       if (row["UserAccount"] != null)

    But the value is only null.


    Best Regards, wendy

  • 2012년 8월 16일 목요일 오전 11:27
     
     답변됨 코드 있음

    Hi,

    I just pass value to 'ddlAccount' field.

    I also write using this one

       if (row["UserAccount"] != null)

    But the value is only null.


    Best Regards, wendy

    You don't have to pass the value to the drop down, that will happen in the binding, all you need to do is tell the drop down list to which column in the data table it has to go find the data, therefor you don't need to do null checks. All you need is

    SPListItemCollection collListItems1 = oList1.GetItems(spQuery1);            
    DataTable dt1 = oList1.GetItems(spQuery1).GetDataTable();           
    dt1 = collListItems1.GetDataTable(); 
      
    ddlAcount.DataSource = dt1;
    ddlAcount.DataTextField = "UserAccount";
    ddlAcount.DataValueField = "UserAccount";
    ddlAcount.DataBind();
    If you don't want NULL values in the data, you should filter them out when you query for the data in the first line of code.


    I find it distasteful to beg for 'Mark as Answer' and 'Mark as helpful'. It's supposed to be about helping people, not about getting the high score.

    • 답변으로 표시됨 Lhan HanModerator 2012년 8월 23일 목요일 오전 9:33
    •  
  • 2012년 8월 20일 월요일 오후 12:28
     
     

    Hi,

    Has this been resolved? If so please mark as answer so thread can be closed.

    Regards


    I find it distasteful to beg for 'Mark as Answer' and 'Mark as helpful'. It's supposed to be about helping people, not about getting the high score.