dropdownlist绑定数据后,进行插入数据错误
现已将两个dropdownlist关联,选中第一个dropdownlist表示一个大类进行选择后,第二个dropdownlist会显示出第一个的相应子类。但是插入操作时,第二个dropdownlist中插入的数据永远都是子类中的第一个数据,我想了好久都没有明白,望高人指点。
以下是源码
关键问题是:(第一个dropdownlist是ddlclass,第二个dropdownlist是ddldevice),ddlclass是设备的大类别,ddldevice是设备的编号,选中大类别后再去选择编号,但是插入的设备编号却永远是排在一个个的编号,而非选中的编号
前台:
后台代码
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
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.Xml.Linq;
using System.Data.SqlClient;
namespace ServiceDesk.SD
{
public partial class sduseless : System.Web.UI.Page
{
// 定义连接字符串
private static string strCon = System.Configuration.ConfigurationManager.AppSettings["connStr"];
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
//绑定ddlclass
SqlConnection con = new SqlConnection(strCon);
con.Open();
SqlCommand cmd = new SqlCommand("select * from class ", con);
SqlDataReader reader = cmd.ExecuteReader();
ddlclass.DataSource = reader;
ddlclass.DataTextField = "classname";
ddlclass.DataValueField = "classno";
ddlclass.DataBind();
reader.Close();
//绑定ddldevice
SqlCommand cmd2 = new SqlCommand("select * from deviceinfo where classno=" + this.ddlclass.SelectedValue, con);
reader = cmd2.ExecuteReader();//上面定义过reader,这里就不用从新定义
ddldevice.DataSource = reader;
ddldevice.DataTextField = "deviceno";
ddldevice.DataValueField = "classno";
ddldevice.DataBind();
reader.Close();
}
}
// 验证用户名
private bool blnCheckUser()
{
// 数据库连接
SqlConnection objConnection = new SqlConnection(strCon);
// 数据库命令
SqlCommand objCommand = new SqlCommand("", objConnection);
// 设置Sql语句
objCommand.CommandText =
" SELECT userno FROM USERS WHERE userno = @user";
// 设置Sql语句参数
objCommand.Parameters.Add("user", SqlDbType.VarChar);
// Sql语句参数赋值
objCommand.Parameters["user"].Value = txtuserno.Text.Trim();
try
{
// 打开数据库连接
if (objConnection.State == ConnectionState.Closed) objConnection.Open();
// 获取运行结果
SqlDataReader result = objCommand.ExecuteReader();
if (result.Read() == false)
{
lblCheck.Visible = true;
return false;
}
else
{
lblCheck.Visible = false;
return true;
}
}
catch (SqlExcepti