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

  • Home
  • SEARCH
  • 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 5988073
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T22:54:27+00:00 2026-05-22T22:54:27+00:00

I was trying to make a custom application form. I’ve added some control like

  • 0

I was trying to make a custom application form. I’ve added some control like TextBox and Button so far everything is fine until I modified some of the TextBox with a DropDownList control. When I click the add button it returns an error:

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

I’ve attached the sample code of my code behind:

public partial class Test2 : System.Web.UI.Page
    {
        public string GetConnectionString()
        {
            return System.Configuration.ConfigurationManager.ConnectionStrings["LibrarySystemConnectionString"].ConnectionString;
        }

        private void execution(string bookid, string booktitle, string lastname, string firstname, string description, string categoryid, string dateadded, string quantity, string statusid)
        {
            SqlConnection conn = new SqlConnection(GetConnectionString());

            string sql = "INSERT INTO TblBooks(bookid, booktitle, lastname, firstname, description, categoryid, dateadded, quantity, statusid) VALUES "+" (@bookid, @booktitle,@lastname, @firstname, @description, @categoryid, @dateadded, @quantity, @statusid)";

            try
            {
                conn.Open();
                SqlCommand cmd = new SqlCommand(sql, conn);
                SqlParameter[] pram = new SqlParameter[9];
                pram[0] = new SqlParameter("@bookid", SqlDbType.BigInt, 64);
                pram[1] = new SqlParameter("@booktitle", SqlDbType.NVarChar, 50);
                pram[2] = new SqlParameter("@lastname", SqlDbType.NVarChar, 50);
                pram[3] = new SqlParameter("@firstname", SqlDbType.NVarChar, 50);
                pram[4] = new SqlParameter("@description", SqlDbType.NVarChar, 200);
                pram[5] = new SqlParameter("@categoryid", SqlDbType.Int, 32);
                pram[6] = new SqlParameter("@quantity", SqlDbType.Int, 32);
                pram[7] = new SqlParameter("@statusid", SqlDbType.Int, 32);
                pram[8] = new SqlParameter("@dateadded", SqlDbType.DateTime, 32);

                pram[0].Value = bookid;
                pram[1].Value = booktitle;
                pram[2].Value = lastname;
                pram[3].Value = firstname;
                pram[4].Value = description;
                pram[5].Value = categoryid;
                pram[6].Value = quantity;
                pram[7].Value = statusid;
                pram[8].Value = dateadded;

                for (int i = 0; i < pram.Length; i++)
                {
                    cmd.Parameters.Add(pram[i]);
                }
                cmd.CommandType = CommandType.Text;
                cmd.ExecuteNonQuery();
            }
            catch (System.Data.SqlClient.SqlException ex_msg)
            {

                string msg = "Error occured while inserting";
                msg += ex_msg.Message;
                throw new Exception(msg);
            }

            finally
            {
                conn.Close();
            }
            }

        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            if (bookidTextBox.Text == "")
            {
                Response.Write("Please complete the form");
            }

            else
            {
             execution(bookidTextBox.Text, booktitleTextBox.Text, lastnameTextBox.Text, firstnameTextBox.Text, descriptionTextBox.Text, categoryDropDownList.SelectedValue, dateaddedTextBox.Text, quantityTextBox.Text, statusDropDownList.SelectedValue);
                //conform.Visible = true;
                bookidTextBox.Text = "";
                booktitleTextBox.Text = "";
                lastnameTextBox.Text = "";
                firstnameTextBox.Text = "";
                descriptionTextBox.Text = "";
                categoryDropDownList.SelectedValue = "";
                dateaddedTextBox.Text = "";
                statusDropDownList.SelectedValue = "";
                quantityTextBox.Text = "";

            }
        }
    }
}

Here’s the sample code of the form:

<table class="style1">
    <tr>
        <td class="style2">
            ISBN:
            </td>
            <td class="style3">
            <asp:TextBox ID="bookidTextBox" runat="server" Text='<%# Bind("bookid") %>'>
            </asp:TextBox>
            </td>
            <td>
            <asp:RequiredFieldValidator ID="RequiredFieldValidator" runat="server" ControlToValidate="bookidTextBox" ErrorMessage="* " ValidationGroup="addbooks">
            </asp:RequiredFieldValidator>
        </td>
    </tr>

<tr>
<td class="style2">
Title:
</td>
<td class="style3">
<asp:TextBox ID="booktitleTextBox" runat="server" Text='<%# Bind("booktitle") %>'>
</asp:TextBox>
</td>
<td>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="booktitleTextBox" ErrorMessage="* " ValidationGroup="addbooks">
</asp:RequiredFieldValidator>
</td>
</tr>

... (cut some code here)

<tr>
<td class="style2">
Category:
</td>
<td class="style3">
    <asp:DropDownList ID="categoryDropDownList" runat="server" 
        DataSourceID="categoryDataSource" DataTextField="name"
        DataValueField="categoryid" >
    </asp:DropDownList>
    <asp:SqlDataSource ID="categoryDataSource" runat="server"
        ConnectionString="<%$ ConnectionStrings:LibrarySystemConnectionString %>" 
        SelectCommand="SELECT [categoryid], [name] FROM [TblCategory]">
    </asp:SqlDataSource>
</td>
</tr>

...

<tr>
<td class="style2">
Status:</td>
<td class="style3">
    <asp:DropDownList ID="statusDropDownList" runat="server" 
        DataSourceID="statusDataSource" DataTextField="statusname" 
        DataValueField="statusid" >
    </asp:DropDownList>
    <asp:SqlDataSource ID="statusDataSource" runat="server" 
        ConnectionString="<%$ ConnectionStrings:LibrarySystemConnectionString %>" 
        SelectCommand="SELECT [statusid], [statusname] FROM [BookStatus]">
    </asp:SqlDataSource>
</td>
</tr>

</table>
</div>

<asp:Button ID="AddBtn" runat="server" Text="Add" OnClick="Button1_Click" Width="100px" />
<br />
<br />

<b>Book List</b>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
    DataKeyNames="bookid" DataSourceID="bookDataSource">
    <Columns>
        <asp:BoundField DataField="bookid" HeaderText="bookid" ReadOnly="True" 
            SortExpression="bookid" />
        <asp:BoundField DataField="booktitle" HeaderText="booktitle" 
            SortExpression="booktitle" />
        <asp:BoundField DataField="lastname" HeaderText="lastname" 
            SortExpression="lastname" />
        <asp:BoundField DataField="firstname" HeaderText="firstname" 
            SortExpression="firstname" />
        <asp:BoundField DataField="categoryid" HeaderText="categoryid" 
            SortExpression="categoryid" />
        <asp:BoundField DataField="description" HeaderText="description" 
            SortExpression="description" />
        <asp:BoundField DataField="dateadded" HeaderText="dateadded" 
            SortExpression="dateadded" />
        <asp:BoundField DataField="statusid" HeaderText="statusid" 
            SortExpression="statusid" />
        <asp:BoundField DataField="quantity" HeaderText="quantity" 
            SortExpression="quantity" />
        <asp:CheckBoxField DataField="isdeleted" HeaderText="isdeleted" 
            SortExpression="isdeleted" />
    </Columns>
</asp:GridView>

It worked when I try to convert the DropDownList to textbox but how can I bind it with a DropDownList control??

Help would be much appreciated! Thanks in advance.

  • 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-05-22T22:54:28+00:00Added an answer on May 22, 2026 at 10:54 pm

    Your DropDown is bound to SqlDataSource. When you execute categoryDropDownList.SelectedValue = "", the DropDown is looking for empty value in the data source, but can’t find such, so it fails.

    If might want to run dropDownList.ClearSelection() if this is what you are trying to achieve.

    Here is simplified code example that would demonstrate the issue

    protected void Page_Load(object sender, EventArgs e)
    {
        var items = new[] { new { Id = 1, Name = "Test1" }, new { Id = 2, Name = "Test2" } };
    
        dropDownList.DataSource = items;
        dropDownList.DataValueField = "Id";
        dropDownList.DataTextField = "Name";
        dropDownList.DataBind();
    }
    
    protected void Button1_Click(object sender, EventArgs e)
    {
        dropDownList.SelectedValue = ""; // trhows exception
        dropDownList.ClearSelection(); // works
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using Doctrine ODM and trying to make a custom mapping type but I'm
I am trying to add a custom button in the new Xcode 4 Interface
I'm trying to alter the existing number formatting in my company's application to make
I'm trying to control the keystroke from kinect. For example, when my right hand
I am trying to develop an application where in my C# application should print
Writing an application for a custom gallery, and all the script files are put
I am trying to write an ASP.NET MVC application which is a frontend to
I'm building a WPF application in which I need to display document previews such
I am developing an iOS application that comes pre-loaded with CoreData content. I am
In my ASP.NET MVC application I need to implement persistence of data. I've choose

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.