SitePoint Sponsor

User Tag List

Results 1 to 19 of 19

Thread: Issue with a radiobuttonlist when updating a database

  1. #1
    SitePoint Enthusiast
    Join Date
    May 2006
    Location
    Edinburgh, Scotland
    Posts
    33
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Issue with a radiobuttonlist when updating a database

    I there

    I have a database (sqlserver) holding user information, I just added a field (bit datatype) to enable an administrator to enable or disable a user account let's refer to it as the user status.

    I use a formview to pullout the information so far so good, the radiobuttonlist dynamically displays the status of the user, ie the correct button is selected.

    The problem I have is that when I press the edit url from the formview I can edit any information about the user but it won't update the user status if when using the radiobuttonlist. No error just that this bit is not updated

    Any idea ??

    Here is a bit of my code:

    Code ASP:
    <asp:SqlDataSource ID="SqlDS_formview" runat="server" ConnectionString="<%$ ConnectionStrings:IMS_DBConnectionString %>"
        SelectCommand="SELECT * FROM [tbl_user] 
           INNER JOIN [tbl_department]
           ON tbl_user.dpt_id = tbl_department.dpt_id 
           WHERE ([user_id] = @user_id)"
     
        ConflictDetection="CompareAllValues"
        OldValuesParameterFormatString="original_{0}"
     
        UpdateCommand="UPDATE tbl_user SET user_name = @user_name, user_surname = @user_surname, user_email = @user_email, user_pw = @user_pw WHERE (user_id = @original_user_id) AND (user_name = @original_user_name) AND (user_surname = @original_user_surname) AND (user_email = @original_user_email) AND (user_pw = @original_user_pw)">
     
            <SelectParameters>
                <asp:QueryStringParameter Name="user_id" QueryStringField="d" Type="Int32" />
            </SelectParameters>
     
            <UpdateParameters>            
                <asp:Parameter Name="user_name" Type="String" />
                <asp:Parameter Name="user_surname" Type="String" />
                <asp:Parameter Name="user_email" Type="String" />
                <asp:Parameter Name="user_pw" Type="String" />
                <asp:Parameter Name="user_active" Type="Boolean" />
                <asp:Parameter Name="original_user_id" Type="Int32" />
                <asp:Parameter Name="original_user_name" Type="String" />
                <asp:Parameter Name="original_user_surname" Type="String" />
                <asp:Parameter Name="original_user_email" Type="String" />
                <asp:Parameter Name="original_user_pw" Type="String" />
            </UpdateParameters>
     
        </asp:SqlDataSource>
     
     
    <asp:FormView ID="FormView1" runat="server" DataKeyNames="user_id" DataSourceID="SqlDS_formview" OnItemUpdated="OnRowUpdated"> 
     
            <EditItemTemplate>
     
    <asp:TextBox ID="user_nameTextBox" runat="server" Width="200px" Text='<%# Bind("user_name") %>'></asp:TextBox><strong> User Name</strong><br />
     
                <asp:TextBox ID="user_surnameTextBox" runat="server" Width="200px" Text='<%# Bind("user_surname") %>'></asp:TextBox><strong> User Surname</strong><br />
     
                <asp:TextBox ID="user_emailTextBox" runat="server" Width="200px" Text='<%# Bind("user_email") %>'></asp:TextBox><strong> User Email</strong><br />
     
                <asp:TextBox ID="user_pwTextBox" runat="server" Width="200px" Text='<%# Bind("user_pw") %>'></asp:TextBox><strong>User Password</strong><br />
     
                 <br />
                 <asp:RadioButtonList AutoPostBack="true" ID="user_active" runat="server" Enabled="true" SelectedIndex='<%#Convert.ToInt32(Eval("user_active"))%>'>
                        <asp:ListItem Value="0">Deactivated</asp:ListItem>
                        <asp:ListItem Value="1">Activated</asp:ListItem>                       
                    </asp:RadioButtonList>
                <br /> 
     
     
     
    etc....


    Thanks for looking I am pretty stuck !

    Chris

  2. #2
    SitePoint Author silver trophybronze trophy
    wwb_99's Avatar
    Join Date
    May 2003
    Location
    Washington, DC
    Posts
    10,424
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    You need to <&#37;# Bind %> the list methinks.

  3. #3
    SitePoint Enthusiast
    Join Date
    May 2006
    Location
    Edinburgh, Scotland
    Posts
    33
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Unhappy

    Yeah it's what I thought too at first, but if I replace

    Code ASP:
    <asp:RadioButtonList AutoPostBack="true" ID="user_active" runat="server" Enabled="true" SelectedIndex='<%#Convert.ToInt32(Eval("user_active"))%>'>
                        <asp:ListItem Value="0">Deactivated</asp:ListItem>
                        <asp:ListItem Value="1">Activated</asp:ListItem>                       
                    </asp:RadioButtonList>

    with this

    Code ASP:
     <asp:RadioButtonList AutoPostBack="true" ID="user_active" runat="server" Enabled="true" SelectedIndex='<%#Convert.ToInt32(Bind("user_active"))%>'>
                        <asp:ListItem Value="0">Deactivated</asp:ListItem>
                        <asp:ListItem Value="1">Activated</asp:ListItem>                       
                    </asp:RadioButtonList>

    I get the following error

    Compiler Error Message: CS0103: The name 'Bind' does not exist in the current context

    So no joy

  4. #4
    SitePoint Author silver trophybronze trophy
    wwb_99's Avatar
    Join Date
    May 2003
    Location
    Washington, DC
    Posts
    10,424
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Ok. One question--why are you using SelectedIndex? You should be able to Bind() directly to the SelectedValue property.

  5. #5
    SitePoint Enthusiast
    Join Date
    May 2006
    Location
    Edinburgh, Scotland
    Posts
    33
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You are right, it makes more sens but it doesn't work

    Code ASP:
    SelectedValue='<%#Convert.ToInt32(Bind("user_active"))%>'>

    using the above code throws me the same error

    Compiler Error Message: CS0103: The name 'Bind' does not exist in the current context

    If I change Bind to Eval it displays but still doens't update..

  6. #6
    SitePoint Author silver trophybronze trophy
    wwb_99's Avatar
    Join Date
    May 2003
    Location
    Washington, DC
    Posts
    10,424
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Skip the Convert.ToInt32 bit, you don't need it for selected value and it messes with Bind's 'magics'.

  7. #7
    SitePoint Enthusiast
    Join Date
    May 2006
    Location
    Edinburgh, Scotland
    Posts
    33
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    No Joy I am afraid

    SelectedValue='<&#37;#Convert.ToInt32(Bind("user_active"))%>'>

    or even

    SelectedValue='<%#Convert.ToInt32(Eval("user_active"))%>'>

    now throws the error

    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.

  8. #8
    SitePoint Author silver trophybronze trophy
    wwb_99's Avatar
    Join Date
    May 2003
    Location
    Washington, DC
    Posts
    10,424
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    ^^^You still are using the Convert.ToInt32. You shouldn't need it.

  9. #9
    SitePoint Enthusiast
    Join Date
    May 2006
    Location
    Edinburgh, Scotland
    Posts
    33
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Sorry I pasted the wrong code

    I meant

    SelectedValue='<%#Bind("user_active")%>'>

    or even

    SelectedValue='<%#Eval("user_active")%>'>

    now throws the error

    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.

  10. #10
    SitePoint Mentor NightStalker-DNS's Avatar
    Join Date
    Jul 2004
    Location
    Cape Town, South Africa
    Posts
    2,748
    Mentioned
    17 Post(s)
    Tagged
    0 Thread(s)
    Which line is it giving that error on? As just that alone is not very descriptive. In ur web.config enable debugging for now to get proper detailed descriptions to see what its moaning about. Just remember to turn debug off b4 going live tho.

  11. #11
    SitePoint Member
    Join Date
    Jan 2009
    Posts
    9
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    can you post complete error...

  12. #12
    SitePoint Enthusiast
    Join Date
    May 2006
    Location
    Edinburgh, Scotland
    Posts
    33
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ok to summarise.. the section triggering the error is this one.

    Code ASP:
    <asp:RadioButtonList AutoPostBack="true" ID="user_active" runat="server" AppendDataBoundItems="true" SelectedValue='<%#Bind("user_active")%>'>
                        <asp:ListItem Value="0">Deactivated</asp:ListItem>
                        <asp:ListItem Value="1">Activated</asp:ListItem>                       
                    </asp:RadioButtonList>
                <br />

    I have the same error if I put Eval instead of the Bind

    Code ASP:
    <asp:RadioButtonList AutoPostBack="true" ID="user_active" runat="server" AppendDataBoundItems="true" SelectedValue='<%#Eval("user_active")%>'>
                        <asp:ListItem Value="0">Deactivated</asp:ListItem>
                        <asp:ListItem Value="1">Activated</asp:ListItem>                       
                    </asp:RadioButtonList>
                <br />

    The following kind of works (it displays )but doesn't let me update the database when I select an option on the radio button and click update (no error thrown jut that id doesn't update this particular field in the database)

    Code ASP:
    <asp:RadioButtonList AutoPostBack="true" ID="user_active" runat="server" AppendDataBoundItems="true" SelectedValue='<%#Convert.ToInt32(Eval("user_active"))%>'>
                        <asp:ListItem Value="0">Deactivated</asp:ListItem>
                        <asp:ListItem Value="1">Activated</asp:ListItem>                       
                    </asp:RadioButtonList>

    Debug is enabled and here is the error I get:

    Server Error in '/' Application.
    --------------------------------------------------------------------------------

    'user_active' has a SelectedValue which is invalid because it does not exist in the list of items.
    Parameter name: value
    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.ArgumentOutOfRangeException: 'user_active' has a SelectedValue which is invalid because it does not exist in the list of items.
    Parameter name: value

    Source Error:

    An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.

    Stack Trace:


    [ArgumentOutOfRangeException: 'user_active' has a SelectedValue which is invalid because it does not exist in the list of items.
    Parameter name: value]
    System.Web.UI.WebControls.ListControl.PerformDataBinding(IEnumerable dataSource) +1604142
    System.Web.UI.WebControls.ListControl.OnDataBinding(EventArgs e) +107
    System.Web.UI.WebControls.ListControl.PerformSelect() +34
    System.Web.UI.WebControls.BaseDataBoundControl.DataBind() +73
    System.Web.UI.Control.DataBindChildren() +211
    System.Web.UI.Control.DataBind(Boolean raiseOnDataBinding) +102
    System.Web.UI.Control.DataBind() +15
    System.Web.UI.Control.DataBindChildren() +211
    System.Web.UI.Control.DataBind(Boolean raiseOnDataBinding) +102
    System.Web.UI.Control.DataBind() +15
    System.Web.UI.Control.DataBindChildren() +211
    System.Web.UI.Control.DataBind(Boolean raiseOnDataBinding) +102
    System.Web.UI.Control.DataBind() +15
    System.Web.UI.Control.DataBindChildren() +211
    System.Web.UI.Control.DataBind(Boolean raiseOnDataBinding) +102
    System.Web.UI.WebControls.FormView.CreateChildControls(IEnumerable dataSource, Boolean dataBinding) +1461
    System.Web.UI.WebControls.CompositeDataBoundControl.PerformDataBinding(IEnumerable data) +57
    System.Web.UI.WebControls.FormView.PerformDataBinding(IEnumerable data) +12
    System.Web.UI.WebControls.DataBoundControl.OnDataSourceViewSelectCallback(IEnumerable data) +114
    System.Web.UI.DataSourceView.Select(DataSourceSelectArguments arguments, DataSourceViewSelectCallback callback) +31
    System.Web.UI.WebControls.DataBoundControl.PerformSelect() +142
    System.Web.UI.WebControls.BaseDataBoundControl.DataBind() +73
    System.Web.UI.WebControls.FormView.DataBind() +4
    System.Web.UI.WebControls.BaseDataBoundControl.EnsureDataBound() +82
    System.Web.UI.WebControls.FormView.EnsureDataBound() +166
    System.Web.UI.WebControls.BaseDataBoundControl.OnPreRender(EventArgs e) +22
    System.Web.UI.Control.PreRenderRecursiveInternal() +80
    System.Web.UI.Control.PreRenderRecursiveInternal() +171
    System.Web.UI.Control.PreRenderRecursiveInternal() +171
    System.Web.UI.Control.PreRenderRecursiveInternal() +171
    System.Web.UI.Control.PreRenderRecursiveInternal() +171
    System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +842




    --------------------------------------------------------------------------------
    Version Information: Microsoft .NET Framework Version:2.0.50727.3053; ASP.NET Version:2.0.50727.3053

  13. #13
    SitePoint Author silver trophybronze trophy
    wwb_99's Avatar
    Join Date
    May 2003
    Location
    Washington, DC
    Posts
    10,424
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Everything is working as designed--what is the value of your user_active field?

  14. #14
    SitePoint Member
    Join Date
    Jan 2009
    Posts
    9
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    yes everything is correct... you are trying to bind a value which is not mention in Listitems
    check your query or source if it has different value other than mentioned in itemlist

  15. #15
    SitePoint Enthusiast
    Join Date
    May 2006
    Location
    Edinburgh, Scotland
    Posts
    33
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Mmm I think you are right it has something to do with the database datatype

    When I look at my user datatabase table from the Server Explorer in VS2005,
    The data type for user_active is "bit" since it's the closest to boolean in sql server , so when I look at the data in the field is populated with "False" or "True".

    In my Radiobuttonlist, if I change the list item Value from 0 or 1 to False or True as follow I can use the Bind instead of the Eval, I displays fine, but It still doesn't update the user_active data field..

    Code ASP:
    <asp:RadioButtonList AutoPostBack="true" ID="user_active" runat="server" AppendDataBoundItems="true" SelectedValue='<%#Bind("user_active")%>'>
                        <asp:ListItem Value="False">Deactivated</asp:ListItem>
                        <asp:ListItem Value="True">Activated</asp:ListItem>                       
                    </asp:RadioButtonList>

    but if I put 0 or 1 as you know I can't use Bind.

    the strange thing is that on another page, when I add a new user, I use a hidden field as an insert parameter to insert the value "1" by default in my "user_active" field and it insert fine. Only the update gives me grief.

  16. #16
    SitePoint Evangelist
    Join Date
    Apr 2008
    Location
    Dublin, Ireland
    Posts
    461
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Use a checkbox for true/false (bit) fields.

  17. #17
    SitePoint Enthusiast
    Join Date
    May 2004
    Location
    Canada
    Posts
    52
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Instead of using regular parameter (just an example)
    Code ASP:
    <asp:Parameter Name="user_name" Type="String" />

    for radio button if you want to catch the parameters you should use ControlParameter

    Code ASP:
    <asp:ControlParameter Name="Id" ControlID="ddlMovies"
    PropertyName="SelectedValue" />

    Otherwise if you use regular parametters then you need to catch them from Page_Load

  18. #18
    SitePoint Enthusiast
    Join Date
    May 2006
    Location
    Edinburgh, Scotland
    Posts
    33
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks but is it actually possible to use a mix of parameter and controlParameter in the same update parameter ? ie:
    <UpdateParameters>
    <asp:Parameter Name="user_name" Type="String" />
    <asp:Parameter Name="user_surname" Type="String" />
    <asp:ControlParameter Name="Id" ControlID="ddlMovies"
    PropertyName="SelectedValue" />
    </UpdateParameters>

  19. #19
    SitePoint Member
    Join Date
    Apr 2009
    Posts
    2
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Please Help!

    This thread is the closest to my problem ... I have created an templatefield and the insert works fine but when editing, it corrupts, but not all the time?? My code is:

    <asp:TemplateField HeaderText="DelegateActive" SortExpression="DelegateActive" ItemStyle-Width="400px" ControlStyle-Width="466px" >
    <EditItemTemplate>
    <asp:RadioButtonList ID="RadioButtonList6" RepeatDirection="Horizontal" runat="server" DataSourceID="SqlDataSource6" DataTextField="DelegateActive" DataValueField="DelegateActive" SelectedValue='<%# Bind("DelegateActive") %>'>
    <asp:ListItem Text="True" Value="True" ></asp:ListItem>
    <asp:ListItem Text="False" Value="False" ></asp:ListItem>
    </asp:RadioButtonList>
    <asp:SqlDataSource ID="SqlDataSource6" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>" UpdateCommand="UPDATE Delegate SET [DelegateActive] = @DelegateActive"></asp:SqlDataSource>
    </EditItemTemplate>
    <InsertItemTemplate>
    <asp:RadioButtonList ID="RadioButtonList6" RepeatDirection="Horizontal" runat="server" AppendDataBoundItems="true" DataSourceID="SqlDataSource6" DataTextField="DelegateActive" DataValueField="DelegateActive" SelectedValue='<%# Bind("DelegateActive") %>'>
    <asp:ListItem Text="True" Value="True" Selected="True" ></asp:ListItem>
    <asp:ListItem Text="False" Value="False" ></asp:ListItem>
    </asp:RadioButtonList>
    <asp:SqlDataSource ID="SqlDataSource6" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>" InsertCommand="INSERT INTO Delegate(DelegateActive) VALUES (DelegateActive)" ></asp:SqlDataSource>
    </InsertItemTemplate>
    <ItemTemplate>
    <asp:Label ID="Label7" runat="server" Text='<%# Bind("DelegateActive") %>'></asp:Label>
    </ItemTemplate>

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

    But it has ??? Thanks David

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •