各位前辈,界面做了二级联表,无法z在第二个下拉菜单中赋值“DropDownList2”有一个无效 SelectedValue,因为它不在项目列表中。 ”
求助各位前辈,界面做了二级联表,无法z在第二个下拉菜单中赋值“DropDownList2”有一个无效 SelectedValue,因为它不在项目列表中。 ”
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.Data.Common;
public partial class 二级联表实验 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
//绑定大品类
SqlConnection myCon = null;
SqlCommand myCom = null;
string myConStr = System.Configuration.ConfigurationSettings.AppSettings["db_link"]; ;
myCon = new SqlConnection(myConStr);
myCon.Open();
myCom = new SqlCommand("select * from B_TYPE", myCon);
SqlDataReader sdr = myCom.ExecuteReader();
this.DropDownList1.DataSource = sdr;
this.DropDownList1.DataTextField = "b_type";
this.DropDownList1.DataValueField = "b_id";
this.DropDownList1.DataBind();
sdr.Close();
//绑定小品类
SqlCommand cmd = new SqlCommand("select * from S_TYPE where b_id=" + this.DropDownList1.SelectedValue, myCon);
sdr = cmd.ExecuteReader();
this.DropDownList2.DataSource = sdr;
this.DropDownList2.DataTextField = "s_type";
this.DropDownList2.DataValueField = "s_id";
this.DropDownList2.DataBind();
sdr.Close();
myCon.Close();
}
}
protected void Button1_Click(object sender, EventArgs e)
{
string sku = TextBox1.Text.Trim();
SqlConnection myCon = null;
SqlCommand myCom = null;
SqlDataAdapter myDa = null;
DataSet myDs = null;
try
{
//建立SqlConnection连接对象
string myConStr = System.Configuration.ConfigurationSettings.AppSettings["db_link"]; ;
myCon = new SqlConnection(myConStr);
myCon.Open();
//在连接的基础上建立SqlCommand命令对象,
myCom = new SqlCommand();
myCom.Connection = myCon;
string strSelect = "select * from PRO where sku='{0}'";
string sql = string.Format(strSelect, sku);
myCom.CommandType = CommandType.Text;
myCom.CommandText = sql;
myDa = new SqlDataAdapter();
myDa.SelectCommand = myCom;
myDs = new DataSet();
myDa.Fill(myDs, "DS_pro");
DbDataReader reader = myCom.ExecuteReader();
if (reader.Read())
{
TextBox6.Text = reader["b_type"].ToString();
TextBox7.Text = reader["s_type"].ToString();
}
myCon.Close();
}
catch (Exception oe)
{ }
finally
{
//结束语句
//关闭不使用的内容。
if (myCon != null)
{
myCon.Close();
}
}//try 结束
string id1 = TextBox6.Text.Trim();
try
{
//建立SqlConnection连接对象
string myConStr = System.Configuration.ConfigurationSettings.AppSettings["db_link"]; ;
myCon = new SqlConnection(myConStr);
myCon.Open();
//在连接的基础上建立SqlCommand命令对象,
myCom = new SqlCommand();
myCom.Connection = myCon;
string strSelect = "select * from B_TYPE where b_type='{0}'";
string sql = string.Format(strSelect, id1);
myCom.CommandType = CommandType.Text;
myCom.CommandText = sql;
myDa = new SqlDataAdapter();
myDa.SelectCommand = myCom;
myDs = new DataSet();
myDa.Fill(myDs, "DS_B_TYPE");
DbDataReader reader = myCom.ExecuteReader();
if (reader.Read())
{
DropDownList1.Text = reader["b_id"].ToString();
}
myCon.Close();
}
catch (Exception oe)
{ }
finally
{
//结束语句
//关闭不使用的内容。
if (myCon != null)
{
myCon.Close();
}
}//try 结束
string id2 = TextBox7.Text.Trim();
try
{
string myConStr = System.Configuration.ConfigurationSettings.AppSettings["db_link"]; ;
myCon = new SqlConnection(myConStr);
myCon.Open();
myCom = new SqlCommand();
myCom.Connection = myCon;
string strSelect = "select * from S_TYPE where s_type='{0}'";
string sql = string.Format(strSelect, id2);
myCom.CommandType = CommandType.Text;
myCom.CommandText = sql;
myDa = new SqlDataAdapter();
myDa.SelectCommand = myCom;
myDs = new DataSet();
myDa.Fill(myDs, "DS_S_TYPE");
DbDataReader reader = myCom.ExecuteReader();
if (reader.Read())
{
string sid = reader["s_id"].ToString();
DropDownList2.Text = sid;
}
myCon.Close();
}
catch (Exception oe)
{
Label1.Text = "错误";
}
finally
{
if (myCon != null)
{
myCon.Close();
}
}//try 结束
}
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
string b_id = this.DropDownList1.SelectedValue;
SqlConnection myCon = null;
SqlCommand myCom = null;
string myConStr = System.Configuration.ConfigurationSettings.AppSettings["db_link"]; ;
myCon = new SqlConnection(myConStr);
myCon.Open();
SqlCommand cmd = new SqlCommand("select*from S_TYPE where b_id=" + b_id, myCon);
SqlDataReader sdr = cmd.ExecuteReader();
this.DropDownList2.DataSource = sdr;
this.DropDownList2.DataTextField = "s_type";
this.DropDownList2.DataValueField = "s_id";
this.DropDownList2.DataBind();
sdr.Close();
myCon.Close();
}
}
------解决思路----------------------
http://blog.****.net/xianfajushi/article/details/5928136
------解决思路----------------------
试试:
http://www.cnblogs.com/insus/archive/2013/05/10/3070348.html
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.Data.Common;
public partial class 二级联表实验 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
//绑定大品类
SqlConnection myCon = null;
SqlCommand myCom = null;
string myConStr = System.Configuration.ConfigurationSettings.AppSettings["db_link"]; ;
myCon = new SqlConnection(myConStr);
myCon.Open();
myCom = new SqlCommand("select * from B_TYPE", myCon);
SqlDataReader sdr = myCom.ExecuteReader();
this.DropDownList1.DataSource = sdr;
this.DropDownList1.DataTextField = "b_type";
this.DropDownList1.DataValueField = "b_id";
this.DropDownList1.DataBind();
sdr.Close();
//绑定小品类
SqlCommand cmd = new SqlCommand("select * from S_TYPE where b_id=" + this.DropDownList1.SelectedValue, myCon);
sdr = cmd.ExecuteReader();
this.DropDownList2.DataSource = sdr;
this.DropDownList2.DataTextField = "s_type";
this.DropDownList2.DataValueField = "s_id";
this.DropDownList2.DataBind();
sdr.Close();
myCon.Close();
}
}
protected void Button1_Click(object sender, EventArgs e)
{
string sku = TextBox1.Text.Trim();
SqlConnection myCon = null;
SqlCommand myCom = null;
SqlDataAdapter myDa = null;
DataSet myDs = null;
try
{
//建立SqlConnection连接对象
string myConStr = System.Configuration.ConfigurationSettings.AppSettings["db_link"]; ;
myCon = new SqlConnection(myConStr);
myCon.Open();
//在连接的基础上建立SqlCommand命令对象,
myCom = new SqlCommand();
myCom.Connection = myCon;
string strSelect = "select * from PRO where sku='{0}'";
string sql = string.Format(strSelect, sku);
myCom.CommandType = CommandType.Text;
myCom.CommandText = sql;
myDa = new SqlDataAdapter();
myDa.SelectCommand = myCom;
myDs = new DataSet();
myDa.Fill(myDs, "DS_pro");
DbDataReader reader = myCom.ExecuteReader();
if (reader.Read())
{
TextBox6.Text = reader["b_type"].ToString();
TextBox7.Text = reader["s_type"].ToString();
}
myCon.Close();
}
catch (Exception oe)
{ }
finally
{
//结束语句
//关闭不使用的内容。
if (myCon != null)
{
myCon.Close();
}
}//try 结束
string id1 = TextBox6.Text.Trim();
try
{
//建立SqlConnection连接对象
string myConStr = System.Configuration.ConfigurationSettings.AppSettings["db_link"]; ;
myCon = new SqlConnection(myConStr);
myCon.Open();
//在连接的基础上建立SqlCommand命令对象,
myCom = new SqlCommand();
myCom.Connection = myCon;
string strSelect = "select * from B_TYPE where b_type='{0}'";
string sql = string.Format(strSelect, id1);
myCom.CommandType = CommandType.Text;
myCom.CommandText = sql;
myDa = new SqlDataAdapter();
myDa.SelectCommand = myCom;
myDs = new DataSet();
myDa.Fill(myDs, "DS_B_TYPE");
DbDataReader reader = myCom.ExecuteReader();
if (reader.Read())
{
DropDownList1.Text = reader["b_id"].ToString();
}
myCon.Close();
}
catch (Exception oe)
{ }
finally
{
//结束语句
//关闭不使用的内容。
if (myCon != null)
{
myCon.Close();
}
}//try 结束
string id2 = TextBox7.Text.Trim();
try
{
string myConStr = System.Configuration.ConfigurationSettings.AppSettings["db_link"]; ;
myCon = new SqlConnection(myConStr);
myCon.Open();
myCom = new SqlCommand();
myCom.Connection = myCon;
string strSelect = "select * from S_TYPE where s_type='{0}'";
string sql = string.Format(strSelect, id2);
myCom.CommandType = CommandType.Text;
myCom.CommandText = sql;
myDa = new SqlDataAdapter();
myDa.SelectCommand = myCom;
myDs = new DataSet();
myDa.Fill(myDs, "DS_S_TYPE");
DbDataReader reader = myCom.ExecuteReader();
if (reader.Read())
{
string sid = reader["s_id"].ToString();
DropDownList2.Text = sid;
}
myCon.Close();
}
catch (Exception oe)
{
Label1.Text = "错误";
}
finally
{
if (myCon != null)
{
myCon.Close();
}
}//try 结束
}
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
string b_id = this.DropDownList1.SelectedValue;
SqlConnection myCon = null;
SqlCommand myCom = null;
string myConStr = System.Configuration.ConfigurationSettings.AppSettings["db_link"]; ;
myCon = new SqlConnection(myConStr);
myCon.Open();
SqlCommand cmd = new SqlCommand("select*from S_TYPE where b_id=" + b_id, myCon);
SqlDataReader sdr = cmd.ExecuteReader();
this.DropDownList2.DataSource = sdr;
this.DropDownList2.DataTextField = "s_type";
this.DropDownList2.DataValueField = "s_id";
this.DropDownList2.DataBind();
sdr.Close();
myCon.Close();
}
}
------解决思路----------------------
http://blog.****.net/xianfajushi/article/details/5928136
------解决思路----------------------
试试:
http://www.cnblogs.com/insus/archive/2013/05/10/3070348.html