Setting DataValueField and DataTextField shows error

I want to populate _PropSubtype which is a dropdown list by selecting a value from the another dropdown list which is _PropType, by looking at the query you will understad. The problem which is arising is that how do i set the DataValueField and DataTextField( text to display in the dropdown list ). so far the code which i have tried is:

protected void _PropType_SelectedIndexChanged(object sender, EventArgs e)
    {
      using (Property_dbDataContext context = new Property_dbDataContext())
        {
            var type = (from a in context.Property_subtypes
                        where a.main_type == _PropType.SelectedItem.ToString()
                        select new
                        {   
                            a.sr_no,
                            a.prop_subtype

                        }
        ).Distinct().ToList();

            _PropSubtype.DataSource = type;
            _PropSubtype.DataValueField = type[0].sr_no.ToString();
            _PropSubtype.DataTextField = type[1].prop_subtype.ToString();            
            _PropSubtype.DataBind();         
        }
}

Now i have a save button which saves the Selected value of both the dropdown lists, but again problem with _PropSubtype, it may store the text but not the value as the valuefield is sr_no. here is the code for it:

protected void save_Click1(object sender, EventArgs e)
    {
        using (Property_dbDataContext context = new Property_dbDataContext())
        {
            Property basic = new Property();
            basic.property_type = _PropType.SelectedValue;
            basic.property_subtype = _PropSubtype.SelectedIndex.ToString();
            basic.property_subtype = _PropSubtype.SelectedValue;

            context.Property.InsertOnSubmit(basic);
            context.SubmitChanges();
        }
    }

the error which ott return after selecting an option from _PropType:

DataBinding: '<>f__AnonymousType0`2[[System.Int32, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089],[System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]' does not contain a property with the name 'Industrial Land'.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.Web.HttpException: DataBinding: '<>f__AnonymousType0`2[[System.Int32, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089],[System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]' does not contain a property with the name 'Industrial Land'.

链接地址: http://www.djcxy.com/p/52394.html

上一篇: JSON序列化Dynamics CRM

下一篇: 设置DataValueField和DataTextField显示错误