简体   繁体   中英

How do I populate an ASPX C# Form with multiple dropdowns from different related tables?

This is my first attempt at building a website for my company. I basically have one database with 3 tables. Structured below.

tblSiteDataEntryForm
ID
SiteName
siteType
siteSubType

tblPrimaryTypes
ID
PrimaryType

tblSubTypes
ID
PrimaryTypeID
SubType

An employee would use a simple form to create a new DB entry in the Primary Table. I have this form working. A second form would be used to update the Primary Table. On the update form I have a dropdown that gets its values using a select query on the primary database. Then dropdowns for primary type and sub type would be filled based on the row data from the primary table. The primary type dropdown is working but the sub type keeps throwing an error. "ddlSubType has a SelectedValue which is invalid because it does not exist in the list of items. Parameter name: value"

Here is my code. Any help would be greatly appreciated.


    using System;
    using System.Collections.Generic;
    using System.Configuration;
    using System.Data;
    using System.Data.SqlClient;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;

    public partial class ddef_update : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

            if (!Page.IsPostBack)
            {
                LoadInfo();
            }

        }
        protected void LoadInfo()
        {

            SqlConnection connection;
            SqlCommand command;
            SqlDataAdapter adapter = new SqlDataAdapter();
            DataSet ds = new DataSet();
            int i = 0;
            string firstSql = null;
            string secondSql = null;
            string thirdSql = null;
            DataTable sites = new DataTable();
            DataTable types = new DataTable();
            DataTable subtypes = new DataTable();

            firstSql = "SELECT ID, siteName FROM tblSiteDataEntryForm";
            secondSql = "SELECT ID, PrimaryType FROM tblPrimaryTypes";
            thirdSql = "SELECT ID, SubType FROM tblSubTypes";

            connection = new SqlConnection(ConfigurationManager.ConnectionStrings["MyConsString"].ToString());


            {
                connection.Open();

                command = new SqlCommand(firstSql, connection);
                adapter.SelectCommand = command;

                adapter.Fill(sites);
                ddlSiteName.DataSource = sites;
                ddlSiteName.DataTextField = "siteName";
                ddlSiteName.DataValueField = "ID";
                ddlSiteName.DataBind();

                adapter.SelectCommand.CommandText = secondSql;
                adapter.Fill(types);
                ddlPrimaryType.DataSource = types;
                ddlPrimaryType.DataTextField = "PrimaryType";
                ddlPrimaryType.DataValueField = "ID";
                ddlPrimaryType.DataBind();

                adapter.SelectCommand.CommandText = thirdSql;
                adapter.Fill(subtypes);
                ddlSubType.DataSource = subtypes;
                ddlSubType.DataTextField = "SubType";
                ddlSubType.DataValueField = "ID";
                ddlSubType.DataBind();

                adapter.Dispose();
                command.Dispose();
                connection.Close();
            }
        }



        protected void ddlSiteName_SelectedIndexChanged(object sender, EventArgs e)
        {
            string selected = ddlSiteName.SelectedItem.Value;

            SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["MyConsString"].ToString());
            using (connection)
            {

                SqlCommand command = new SqlCommand("SELECT * FROM tblSiteDataEntryForm WHERE ID= @ID", connection);
                command.Parameters.AddWithValue("@ID", selected);
                command.CommandType = CommandType.Text;
                connection.Open();
                SqlDataReader reader = command.ExecuteReader();
                using (reader)
                {
                    if (reader.HasRows)
                    {
                        reader.Read();

                        ddlPrimaryType.Text = reader["siteType"].ToString();
                        ddlSubType.Text = reader["siteSubType"].ToString();


                    }
                }

            }

        }
    }

I was able to solve the problem. The siteSubType in the Primary table was holding a text value. Changed it to integer and all works

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM