This is a migrated thread and some comments may be shown as answers.

Setting DataKeyValues in Code Behind

1 Answer 83 Views
ListBox
This is a migrated thread and some comments may be shown as answers.
Jessica
Top achievements
Rank 1
Jessica asked on 06 Dec 2013, 08:48 PM
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;

1 Answer, 1 is accepted

Sort by
0
Jessica
Top achievements
Rank 1
answered on 06 Dec 2013, 11:07 PM
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>
Tags
ListBox
Asked by
Jessica
Top achievements
Rank 1
Answers by
Jessica
Top achievements
Rank 1
Share this question
or