Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9302641
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T23:18:10+00:00 2026-06-18T23:18:10+00:00

Why am I getting a Runtime error when trying to pre-select a value from

  • 0

Why am I getting a Runtime error when trying to pre-select a value from a DropDownList in ASP.net 2.0 vb?

I’ve tried to do the same thing with a GridView and I get the same message.

Initially I’m trying to show the user the details they submitted to apply for access. When clicking the edit button, I want to allow the user to alter the details they submitted to us and update the application. I’m not looking to do any additional inserting into the database.

I wanted to restrict the user to selecting the department from a DropDownList. When the user clicks edit, I want the department they originally applied with to be pre-selected. When I have the line:

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

I get a Runtime error. If I remove the line, it doesn’t error but doesn’t pre-select anything.

Is anyone able to help me understand why its not working?

Something else that might help is that I’m using Visual Studio 2005 and as I type class properties are being shown as I type. I can see lots of properties for the DropDownList when I press my spacebar, but SelectedValue isn’t one of them which makes me think I’m trying to do something which isn’t supposed to work.

My Code:

<asp:SqlDataSource ID="UserDetails_Conn" runat="server" 
    ConnectionString="<%$ ConnectionStrings:NormCon %>" 
    SelectCommand="SELECT u.first_name, u.last_name, u.email, d.department FROM mis_userlist AS u INNER JOIN mis_depts AS d ON u.dept_id = d.dept_id WHERE (u.windows_login = @windows_ID)"
    UpdateCommand="UPDATE mis_userlist SET first_name = @first_name, last_name = @last_name, email = @email, dept_id = @dept_id WHERE (windows_login = @windows_ID)"
>
    <SelectParameters>
        <asp:SessionParameter Name="windows_ID" SessionField="username" />
    </SelectParameters>
    <UpdateParameters>
        <asp:Parameter Name="first_name" />
        <asp:Parameter Name="last_name" />
        <asp:Parameter Name="email" />
        <asp:Parameter Name="dept_id" />
        <asp:Parameter Name="windows_ID" />
    </UpdateParameters>

</asp:SqlDataSource>
<asp:DetailsView ID="UserDetailsView" runat="server" Height="50px" Width="125px" AutoGenerateRows="False" DataSourceID="UserDetails_Conn">
    <Fields>
        <asp:BoundField DataField="first_name" HeaderText="first_name" SortExpression="first_name" />
        <asp:BoundField DataField="last_name" HeaderText="last_name" SortExpression="last_name" />
        <asp:BoundField DataField="email" HeaderText="email" SortExpression="email" />
        <asp:TemplateField HeaderText="department" SortExpression="department">
            <EditItemTemplate>
                <asp:SqlDataSource ID="DepartmentList" runat="server" 
                    ConnectionString="<%$ ConnectionStrings:NormCon %>" 
                    SelectCommand="SELECT dept_id, department FROM mis_depts ORDER BY department"
                >
                </asp:SqlDataSource>
                <asp:DropDownList ID="ddlDepartments" runat="server"
                    DataSourceId="DepartmentList"
                    DataTextField="department"
                    DataValueField="dept_id"
                    SelectedValue='<%# Bind("department") %>' > <!-- Errorsome line -->
                </asp:DropDownList>
            </EditItemTemplate>
            <InsertItemTemplate>
                <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("department") %>'></asp:TextBox>
            </InsertItemTemplate>
            <ItemTemplate>
                <asp:Label ID="Label1" runat="server" Text='<%# Bind("department") %>'></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:CommandField ShowEditButton="True" />
    </Fields>
</asp:DetailsView>

Solution

Along with Win’s Solution, I needed to add the dept_id field in the SQL string.

SelectCommand="SELECT u.first_name, u.last_name, u.email, d.department, d.dept_id FROM mis_userlist AS u INNER JOIN mis_depts AS d ON u.dept_id = d.dept_id WHERE (u.windows_login = @windows_ID)"

This allowed department when represented here:
ddlDepartments.SelectedValue = department
to be a value that could link with my DataValueField=”dept_id” line from the DropDownList.

Thanks to Win.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T23:18:11+00:00Added an answer on June 18, 2026 at 11:18 pm

    SelectedValue will throw an exception if you assign a value which doesn’t exists in the list.

    In order to avoid that, you can validate in DataBound –

    Remove SelectedValue='<%# Bind("department") %>' from aspx page, and attach data bound event to UserDetailsView <asp:DetailsView ... OnDataBound="DetailsView_DataBound" ...

    Protected Sub DetailsView_DataBound(sender As Object, e As EventArgs)
       Dim detailsView = DirectCast(sender, DetailsView)
    
       Dim dr = DirectCast(detailsView.DataItem, DataRowView)
       Dim department = dr(4).ToString() ' Column index of department
       *** OR *** Dim department = dr("department").ToString() ' Column by name
       Dim ddlDepartments = TryCast(detailsView.
          FindControl("ddlDepartments"), DropDownList)
    
       If ddlDepartments IsNot Nothing AndAlso 
          ddlDepartments.Items.FindByValue(department) IsNot Nothing Then
          ddlDepartments.SelectedValue = department
       End If
    End Sub
    

    Basically, you are intercepting data binding of controls.

    Updated: checking the index of department column.
    enter image description here

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am getting an error when trying to move my ASP.NET application from VS2010
I'm trying to launch VB application from Java, but I'm getting runtime error: Exception
I'm getting the dreaded Microsoft JScript runtime error: 'Sys' is undefined error while trying
Getting an Access Violation runtime error when trying to get a WebBrowser's .Url property
I am trying to use jGrowl in ASP.NET, but am getting a Microsoft JScript
I'm trying to use std:getline() but getting a strange runtime error: malloc: * error
Why am I getting runtime error while trying to do: std::vector<int> vi; std::generate_n(std::back_inserter(vi),10,rand); std::vector<int>
I am getting a runtime error in my Java code, and I'm trying to
I'm getting the following runtime error and I cannot determine for the life of
I am getting this weird RunTime error. The classes compile easy on eclipse but

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.