简体   繁体   English

如何用来自不同相关表的多个下拉列表填充ASPX C#表单?

[英]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. 我基本上有一个带有3个表的数据库。 Structured below. 结构如下。

tblSiteDataEntryForm tblSiteDataEntryForm
ID ID
SiteName 网站名称
siteType siteType
siteSubType siteSubType

tblPrimaryTypes tblPrimaryTypes
ID ID
PrimaryType 主要类型

tblSubTypes tblSubTypes
ID ID
PrimaryTypeID 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" “ ddlSubType的SelectedValue无效,因为它在项目列表中不存在。参数名称: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. 主表中的siteSubType包含文本值。 Changed it to integer and all works 将其更改为整数,一切正常

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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