Setting DataKeyValues in Code Behind

2 posts, 0 answers
  1. Jessica
    Jessica avatar
    2 posts
    Member since:
    Oct 2013

    Posted 06 Dec 2013 Link to this post

    Hello,

    I'm trying to set the RadListBox DataKeyField within the code behind.  When I attempt to assign the value I get the error: 

    [column_name] is neither a DataColumn nor a DataRelation for table DefaultView

    The value returned from the string category variable exactly matches with the column name in the [Product] table.

    Here is my applicable code:

    aspx
    <telerik:RadListBox ID="RadListBox2" runat="server"
           Height="200"
           Width="300"
           AllowReorder="true"
           SelectionMode="Multiple"
           AllowDelete="true"
           DataSourceID="SelectedAttributes">
    </telerik:RadListBox>
     
    <asp:SqlDataSource ID="SelectedAttributes" runat="server"
            ConnectionString="<%$ ConnectionStrings:MyConnectionString %>"
            SelectCommand="SELECT @catName FROM [Product] WHERE [Number] = @Number">
                <SelectParameters>
                    <asp:Parameter Name="catName"  />
                </SelectParameters>
                <SelectParameters>
                    <asp:ControlParameter Name="Number"
                      ControlID="tbSearch"
                      PropertyName="Text"/>
                  </SelectParameters>
        </asp:SqlDataSource>



    code-behind:
    //Get CategoryName
    SqlConnection conn1 = new SqlConnection(ConfigurationManager.ConnectionStrings["MyConnectionString"].ToString());
    SqlCommand commDetails = new SqlCommand("SELECT CategoryFieldName FROM [AttributeCategory] WHERE CategoryID = @CatID", conn1);
     
     conn1.Open();
     commDetails.Parameters.Add("@CatID", SqlDbType.Int);
    commDetails.Parameters["@CatID"].Value = (RadListView1.SelectedItems[0] as RadListViewDataItem).GetDataKeyValue("CategoryID").ToString();
    object catName = (object)commDetails.ExecuteScalar();
    conn1.Close();
     
    string category = catName.ToString();
     
    SelectedAttributes.SelectParameters["catName"].DefaultValue = category;
     
    //Pass Data Keys For RadListBox2
    RadListBox2.DataKeyField = category;
    RadListBox2.DataSortField = category;
    RadListBox2.DataTextField = category;
  2. Jessica
    Jessica avatar
    2 posts
    Member since:
    Oct 2013

    Posted 06 Dec 2013 Link to this post

    My code works if I update my Select Statement to:

    <asp:SqlDataSource ID="SelectedAttributes" runat="server"
            ConnectionString="<%$ ConnectionStrings:MyConnectionString %>"
            SelectCommand="SELECT * FROM [Product] WHERE [Number] = @Number">
                <SelectParameters>
                    <asp:Parameter Name="catName"  />
                </SelectParameters>
                <SelectParameters>
                    <asp:ControlParameter Name="Number"
                      ControlID="tbSearch"
                      PropertyName="Text"/>
                  </SelectParameters>
        </asp:SqlDataSource>
  3. UI for ASP.NET Ajax is Ready for VS 2017
Back to Top