locked
Databinding RRS feed

  • Question

  • User-459334211 posted

    I have A form view which consist of two combo

    1.CboCountry

    2.CboCity

    In Sector table there are three fields 

    ID,FKCITYID,sector 

    since i had to link City dropdown to dropdown Country and bind City dropdown to FKCITYID to insert  in sector table''''''''''

    Databinding methods such as Eval(), XPath(), and Bind() can only be used in the context of a databound control.

    this error  occurs when i change a dropdown of contry on drpdown of City  if i bind it two way binding..

     note :if I bind it without two way binding then this error occurs what to do

    CboCity' has a SelectedValue which is invalid because it does not exist in the list of items.
    Parameter name: value

     plz help me out what to do

    Saturday, June 18, 2011 8:48 AM

Answers

  • User-1856974186 posted

    In your FormView template have this:

    <asp:DropDownList ID="CboCity" runat="server" DataSourceID="DSCity" 
    AppendDataBoundItems="true"
    DataTextField="City" DataValueField="ID" Height="20px" Width="155px" >
    <asp:ListItem Value="" Text="-- select city --" />
    </asp:DropDownList>
    <asp:Label id="CityIDLabel" runat="server" Text='<%# Bind("FKCityID") %>' />

    Then run the application and see what is shown in the CityIDLabel. Make sure that value is present in the list of cities provided by the data source DSCity.
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, June 23, 2011 4:56 AM
  • User-1856974186 posted

    So selecting the sector changes the city list; this causes an error because the city for the row isn't in the new city list. In this case I'd not bind the city using SelectedValue, but instead use one of the events on the FormView or formview's data source to set the values before saving. This way you won't get the binding error, but you can still ahve the data saved. For example, the FormView has an ItemInserting event and the SqlDataSource has an Inserting event, so you could do something like this (you only one of them):

        protected void fv1_ItemInserting(object sender, FormViewInsertEventArgs e)
        {
            DropDownList CityList = fv1.FindControl("CityList") as DropDownList;

            if (CityList != null)
            {
                e.Values["city"] = CityList.SelectedValue;
            }
        }
        protected void SqlDataSource1_Inserting(object sender, SqlDataSourceCommandEventArgs e)
        {
            DropDownList CityList = fv1.FindControl("CityList") as DropDownList;

            if (CityList != null)
            {
                e.Command.Parameters["city"].Value = CityList.SelectedValue;
            }
        }

    It's a while since I've used this so you'll need to perhaps read up on which is best to use.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, June 24, 2011 4:12 AM

All replies

  • User-693248168 posted

    Show us the code where you are receiving this error.

    Saturday, June 18, 2011 8:55 AM
  • User-459334211 posted
                                            <asp:DropDownList ID="CboCity" runat="server" DataSourceID="DSCity" 
    DataTextField="City" DataValueField="ID" Height="20px" Width="155px"
    SelectedValue='<%# Bind("FKCityID") %>'>
    </asp:DropDownList>
    Monday, June 20, 2011 5:38 AM
  • User-459334211 posted
    <asp:FormView ID="FormView1" runat="server" DataKeyNames="ID" 
    DataSourceID="DSSector" Height="19px" Width="158px">
    <EditItemTemplate>
    ID:
    <asp:Label ID="IDLabel1" runat="server" Text='<%# Eval("ID") %>' />
    <br />
    Country:<asp:DropDownList ID="DropDownList1" runat="server"
    DataSourceID="DSCountry" DataTextField="Country" DataValueField="CountryID"
    Height="20px" Width="128px">
    </asp:DropDownList>
    <br />
    City:<asp:DropDownList ID="DropDownList2" runat="server" Height="20px"
    Width="128px" SelectedValue='<%# Bind("FKCityID") %>'>
    </asp:DropDownList>
    <br />
    Sector:
    <asp:TextBox ID="SectorTextBox" runat="server"
    Text='<%# Bind("Sector") %>' />
    <br />
    FKCityID:
    <asp:TextBox ID="FKCityIDTextBox" runat="server"
    Text='<%# Bind("FKCityID") %>' />
    <br />
    Password:
    <asp:TextBox ID="PasswordTextBox" runat="server"
    Text='<%# Bind("Password") %>' />
    <br />
    <asp:LinkButton ID="UpdateButton" runat="server" CausesValidation="True"
    CommandName="Update" Text="Update" />
    &nbsp;<asp:LinkButton ID="UpdateCancelButton" runat="server"
    CausesValidation="False" CommandName="Cancel" Text="Cancel" />
    </EditItemTemplate>
    <InsertItemTemplate>
    <table class="style1">
    <tr>
    <td class="style5">
    Country:</td>
    <td>
    <asp:DropDownList ID="CboCountry" runat="server" AutoPostBack="True"
    DataSourceID="DSCountry" DataTextField="Country" DataValueField="ID"
    Height="20px" Width="155px">
    </asp:DropDownList>
    </td>
    </tr>
    <tr>
    <td class="style5">
    City:</td>
    <td>
    <asp:DropDownList ID="CboCity" runat="server" DataSourceID="DSCity"
    DataTextField="City" DataValueField="ID" Height="20px" Width="155px"
    SelectedValue='<%# Bind("FKCityID") %>'>
    </asp:DropDownList>
    </td>
    </tr>
    <tr>
    <td class="style5">
    Sector:
    </td>
    <td>
    <asp:TextBox ID="SectorTextBox" runat="server"
    Text='<%# Bind("Sector") %>' />
    </td>
    </tr>
    <tr>
    <td class="style5">
    Password:
    </td>
    <td>
    <asp:TextBox ID="PasswordTextBox" runat="server"
    Text='<%# Bind("Password") %>' />
    </td>
    </tr>
    </table>
    <asp:LinkButton ID="InsertButton" runat="server" CausesValidation="True"
    CommandName="Insert" Text="Insert" onclick="InsertButton_Click" />
    &nbsp;<asp:LinkButton ID="InsertCancelButton" runat="server"
    CausesValidation="False" CommandName="Cancel" Text="Cancel" />
    <asp:SqlDataSource ID="DSCity" runat="server"
    
    
    
    
    ' The code in bold is the th e place where it gives the error
    Monday, June 20, 2011 5:39 AM
  • User-1770227574 posted

    Hi AbbasAli,

    Try to replace SelectedValue with Text

                        <asp:DropDownList ID="CboCity" runat="server" DataSourceID="DSCity"  DataTextField="City" DataValueField="ID"
                         Height="20px" Width="155px"  Text='<%# Bind("FKCityID") %>'> </asp:DropDownList>

    Regards

    Tuesday, June 21, 2011 11:05 PM
  • User-1856974186 posted

    This error occurs becuase the value in the bound field (FKCityID) doesn't exist in the list of items from the data source. This is often the case when the data source has a strict set of values, but the bound field doesn't yet have a value set. In this case you can manually add an empty entry into the list. Eg:

    <asp:DropDownList ID="CboCity" runat="server" DataSourceID="DSCity" 
    AppendDataBoundItems="true"
    DataTextField="City" DataValueField="ID" Height="20px" Width="155px"
    SelectedValue='<%# Bind("FKCityID") %>'>
    <asp:ListItem Value="" Text="-- select city --" />
    </asp:DropDownList>

    Make sure you set AppendDataBoundItems to true, then the list of cities will be added after the empty value.

    Wednesday, June 22, 2011 4:34 AM
  • User-459334211 posted

    this answer not worked it gives the same error

    Thursday, June 23, 2011 3:21 AM
  • User-1856974186 posted

    You need to work out what the value is in the underlying data then.Remove the SelectedValue and just add anotherbound field, bound to FKCityID to see what the value is.

    Thursday, June 23, 2011 3:47 AM
  • User-459334211 posted

    dear Sir what are u saying i cant Understand! , could u plz explain throug example

    Thursday, June 23, 2011 4:47 AM
  • User-1856974186 posted

    In your FormView template have this:

    <asp:DropDownList ID="CboCity" runat="server" DataSourceID="DSCity" 
    AppendDataBoundItems="true"
    DataTextField="City" DataValueField="ID" Height="20px" Width="155px" >
    <asp:ListItem Value="" Text="-- select city --" />
    </asp:DropDownList>
    <asp:Label id="CityIDLabel" runat="server" Text='<%# Bind("FKCityID") %>' />

    Then run the application and see what is shown in the CityIDLabel. Make sure that value is present in the list of cities provided by the data source DSCity.
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, June 23, 2011 4:56 AM
  • User-459334211 posted

    Now i completely understood the problem 

    Since i am am using form view to insert  data into sector table in which i use  select statement using this query "Select Sector,FKCITYID From Sector" So it showing the new buttot and on Form View item templete Seeing lableFKCITYID have value 4 and when i click the NEW button i see the Formview view insert item templete where I use Dropdown of country and city and cut the FKCITYID text box and bindCity dropdown to FKCITYID on load of formview insert item templete it does not give error since country dropdown default selected value is 2 Since in city table FKCountryID = 2 and id =4 And bind value of dropdownCity =4 but when i change country from country dropdown it give error since if for suppose i choose CountryID 3 from Country dropdown there is no value it fetches the data and city id also changes but since it is bind value is 4 it gives the error

    now what to do plz help 

    Thursday, June 23, 2011 6:36 AM
  • User-1856974186 posted

    So selecting the sector changes the city list; this causes an error because the city for the row isn't in the new city list. In this case I'd not bind the city using SelectedValue, but instead use one of the events on the FormView or formview's data source to set the values before saving. This way you won't get the binding error, but you can still ahve the data saved. For example, the FormView has an ItemInserting event and the SqlDataSource has an Inserting event, so you could do something like this (you only one of them):

        protected void fv1_ItemInserting(object sender, FormViewInsertEventArgs e)
        {
            DropDownList CityList = fv1.FindControl("CityList") as DropDownList;

            if (CityList != null)
            {
                e.Values["city"] = CityList.SelectedValue;
            }
        }
        protected void SqlDataSource1_Inserting(object sender, SqlDataSourceCommandEventArgs e)
        {
            DropDownList CityList = fv1.FindControl("CityList") as DropDownList;

            if (CityList != null)
            {
                e.Command.Parameters["city"].Value = CityList.SelectedValue;
            }
        }

    It's a while since I've used this so you'll need to perhaps read up on which is best to use.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, June 24, 2011 4:12 AM