繁体   English   中英

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

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

这是我为公司建立网站的第一次尝试。 我基本上有一个带有3个表的数据库。 结构如下。

tblSiteDataEntryForm
ID
网站名称
siteType
siteSubType

tblPrimaryTypes
ID
主要类型

tblSubTypes
ID
PrimaryTypeID
子类型

员工将使用简单的表单在主表中创建新的数据库条目。 我有这个表格在工作。 第二种形式将用于更新主表。 在更新表单上,我有一个下拉列表,可使用主数据库上的选择查询来获取其值。 然后将基于主表中的行数据填充主类型和子类型的下拉列表。 主要类型下拉列表正在运行,但是子类型不断抛出错误。 “ ddlSubType的SelectedValue无效,因为它在项目列表中不存在。参数名称:value”

这是我的代码。 任何帮助将不胜感激。


    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();


                    }
                }

            }

        }
    }

我能够解决问题。 主表中的siteSubType包含文本值。 将其更改为整数,一切正常

暂无
暂无

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

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