这是我为公司建立网站的第一次尝试。我基本上有一个包含3个表的数据库。结构如下。
tblSiteDataEntryForm
ID
SiteName
siteType
siteSubType
tblPrimaryTypes
ID
PrimaryType
tblSubTypes
ID
PrimaryTypeID
子类型
员工将使用简单表单在主表中创建新的DB条目。我有这个表格。第二种形式将用于更新主表。在更新表单上,我有一个下拉列表,使用主数据库上的选择查询获取其值。然后,将根据主表中的行数据填充主要类型和子类型的下拉列表。主类型下拉列表正在工作,但子类型不断抛出错误。 “ddlSubType有一个SelectedValue,它是无效的,因为它在项目列表中不存在。参数名称:值”
这是我的代码。任何帮助将不胜感激。
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();
}
}
}
}
}
答案 0 :(得分:0)
我能够解决问题。主表中的siteSubType持有文本值。将其更改为整数,所有工作