using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
namespace voteGroup
{
/// <summary>
/// WebForm1 的摘要说明。
/// </summary>
public class WebForm1 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Label Label1;
protected System.Web.UI.WebControls.DropDownList ddlgroup;
protected System.Web.UI.WebControls.CheckBoxList cbl1;
protected System.Web.UI.WebControls.Button btn1;
protected System.Web.UI.WebControls.CheckBoxList cbl2;
protected System.Web.UI.WebControls.Button btn2;
private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
if(!this.IsPostBack)
{
SqlConnection con=DB.CreateConnection();
con.Open();
SqlCommand cmd=new SqlCommand("select * from groupNetcenter",con);
SqlDataReader sdr=cmd.ExecuteReader();
this.ddlgroup.DataSource=sdr;
this.ddlgroup.DataValueField="gncID";
this.ddlgroup.DataTextField="gncName";
this.ddlgroup.DataBind();
sdr.Close();
SqlCommand cmdwork=new SqlCommand("select * from nameNetCenter where gncID="+this.ddlgroup.SelectedValue,con);
sdr=cmdwork.ExecuteReader();
this.cbl1.DataSource=sdr;
this.cbl1.DataTextField="nncName";
this.cbl1.DataValueField="nncID";
this.cbl1.DataBind();
sdr.Close();
SqlCommand cmdaward= new SqlCommand("select * from awardNetCenter where nncID="+this.cbl1.SelectedValue.ToString(),con);
SqlDataReader sdr2=cmdaward.ExecuteReader();
this.cbl2.DataSource=sdr2;
this.cbl2.DataTextField="ancName";
this.cbl2.DataValueField="ancID";
this.cbl2.DataBind();
sdr2.Close();
con.Close();
}
}
#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.ddlgroup.SelectedIndexChanged += new System.EventHandler(this.ddlgroup_SelectedIndexChanged);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
private void ddlgroup_SelectedIndexChanged(object sender, System.EventArgs e)
{
string gncID=this.ddlgroup.SelectedValue;
SqlConnection con=DB.CreateConnection();
con.Open();
SqlCommand cmd=new SqlCommand("select * from nameNetCenter where gncID="+gncID,con);
SqlDataReader sdr=cmd.ExecuteReader();
// ///////////////////////////////sdr=cmd.ExecuteReader();
this.cbl1.DataSource=sdr;
this.cbl1.DataTextField="nncName";
this.cbl1.DataValueField="nncID";
this.cbl1.DataBind();
sdr.Close();
string nncID=this.cbl1.SelectedValue;
SqlCommand newcmd=new SqlCommand("select * from awardNetCenter where nncID="+nncID,con);
SqlDataReader sdr1=newcmd.ExecuteReader();
this.cbl2.DataSource=sdr1;
this.cbl2.DataTextField="ancName";
this.cbl2.DataValueField="ancID";
this.cbl2.DataBind();
sdr1.Close();
con.Close();
}
这是原代码,总是出现下面的问题:
“/voteGroup”应用程序中的服务器错误。
--------------------------------------------------------------------------------
第 1 行: '=' 附近有语法错误。
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。
异常详细信息: System.Data.SqlClient.SqlException: 第 1 行: '=' 附近有语法错误。
源错误:
行 49:
行 50: SqlCommand cmdaward= new SqlCommand("select * from awardNetCenter where nncID="+this.cbl1.SelectedValue.ToString(),con);
行 51: SqlDataReader sdr2=cmdaward.ExecuteReader();
行 52: this.cbl2.DataSource=sdr2;
行 53: this.cbl2.DataTextField="ancName";
源文件: c:\inetpub\wwwroot\votegroup\webform1.aspx.cs 行: 51
堆栈跟踪:
[SqlException: 第 1 行: '=' 附近有语法错误。]
System.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream)
System.Data.SqlClient.SqlCommand.ExecuteReader()
voteGroup.WebForm1.Page_Load(Object sender, EventArgs e) in c:\inetpub\wwwroot\votegroup\webform1.aspx.cs:51
System.Web.UI.Control.OnLoad(EventArgs e)
System.Web.UI.Control.LoadRecursive()
System.Web.UI.Page.ProcessRequestMain()
问什么原因?真的很捆扰。谢谢回答。