运行有错误,请指教

qiujunw 2006-07-06 01:01:09
WebForm1.aspx和WebForm1.aspx.cs代码都已贴 上

WebForm1.aspx如下:
<%@ Page language="c#" Codebehind="WebForm1.aspx.cs" AutoEventWireup="false" Inherits="t.WebForm1" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>WebForm1</title>
<meta content="Microsoft Visual Studio .NET 7.1" name="GENERATOR">
<meta content="C#" name="CODE_LANGUAGE">
<meta content="JavaScript" name="vs_defaultClientScript">
<meta content="http://schemas.microsoft.com/intellisense/ie5" name="vs_targetSchema">
</HEAD>
<body MS_POSITIONING="GridLayout">
<form id="Form1" method="post" runat="server">
<FONT face="宋体">
<asp:CheckBoxList id="ckbEmployees" style="Z-INDEX: 101; LEFT: 24px; POSITION: absolute; TOP: 32px"
RepeatLayout="Table" RepeatDirection="Vertical" RepeatColumns="3" CellPadding="9" CellSpacing="18"
TextAlign="Right" AutoPostBack="True" runat="server"></asp:CheckBoxList>
<asp:DataGrid id="dgEmployee" style="Z-INDEX: 102; LEFT: 16px; POSITION: absolute; TOP: 168px"
runat="server"></asp:DataGrid></FONT></form>
</body>
</HTML>


WebForm1.aspx.cs如下:
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 t
{
/// <summary>
/// WebForm1 的摘要说明。
/// </summary>
public class WebForm1 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.DataGrid dgEmployee;
protected System.Web.UI.WebControls.CheckBoxList ckbEmployees;

private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
if(!IsPostBack)
{
string strConnection="server=127.0.0.1;user id=sa;password=qiujun;database=Northwind;";
string strCommand="SELECT * FROM Employees ORDER BY LastName;";
SqlConnection Connection=new SqlConnection(strConnection);
SqlCommand Command=new SqlCommand(strCommand,Connection);
Connection.Open();
ckbEmployees.DataSource=Command.ExecuteReader();
ckbEmployees.DataTextField="LastName";
ckbEmployees.DataValueField="EmployeeID";
ckbEmployees.DataBind();
Connection.Close();
}
}

#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}

/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.ckbEmployees.SelectedIndexChanged += new System.EventHandler(this.ListChange);
this.Load += new System.EventHandler(this.Page_Load);

}
#endregion

private void ListChange(object sender, System.EventArgs e)
{
string strConnection="server=127.0.0.1;user id=sa;password=qiujun;database=Northwind;";
string strWhereClause="";
//ListItem liThisOne;
foreach(ListItem liThisOne in ckbEmployees.Items)
{
if(liThisOne.Selected)
strWhereClause+="EmployeeID="+liThisOne.Value+" OR ";
}
Response.Write("strWhereClause="+strWhereClause+"<hr/>");
if (strWhereClause.Length>0)
{
strWhereClause=strWhereClause.Substring(0,strWhereClause.Length-4);
strWhereClause="WHERE "+strWhereClause;
string strCommand="SELECT TitleOfCourtesy,FirstName,LastName,Country,Region,City,Notes FROM Employees"+strWhereClause;
SqlConnection Connection=new SqlConnection(strConnection);
SqlCommand Command=new SqlCommand(strCommand,Connection);
Connection.Open();
Response.Write("strCommand="+strCommand+"<hr/>");
dgEmployee.DataSource=Command.ExecuteReader();
dgEmployee.DataBind();
Connection.Close();

}

}
}
}
...全文
81 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
cscer 2006-07-06
  • 打赏
  • 举报
回复
string strCommand="SELECT TitleOfCourtesy,FirstName,LastName,Country,Region,City,Notes FROM Employees"+strWhereClause;

这句有问题

where子句前要加空格的
qiujunw 2006-07-06
  • 打赏
  • 举报
回复
怎么还没人帮忙?错误的地方大概是这里吧:
private void ListChange(object sender, System.EventArgs e)
{
string strConnection="server=127.0.0.1;user id=sa;password=qiujun;database=Northwind;";
string strWhereClause="";
//ListItem liThisOne;
foreach(ListItem liThisOne in ckbEmployees.Items)
{
if(liThisOne.Selected)
strWhereClause+="EmployeeID="+liThisOne.Value+" OR ";
}
Response.Write("strWhereClause="+strWhereClause+"<hr/>");
if (strWhereClause.Length>0)
{
strWhereClause=strWhereClause.Substring(0,strWhereClause.Length-4);
strWhereClause="WHERE "+strWhereClause;
string strCommand="SELECT TitleOfCourtesy,FirstName,LastName,Country,Region,City,Notes FROM Employees"+strWhereClause;
SqlConnection Connection=new SqlConnection(strConnection);
SqlCommand Command=new SqlCommand(strCommand,Connection);
Connection.Open();
Response.Write("strCommand="+strCommand+"<hr/>");
dgEmployee.DataSource=Command.ExecuteReader();
dgEmployee.DataBind();
Connection.Close();

}

}
qiujunw 2006-07-06
  • 打赏
  • 举报
回复
具体错在哪里我也不知道,大概是在数据库多项查询部分有错.
第 1 行: '=' 附近有语法错误。
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。

异常详细信息: System.Data.SqlClient.SqlException: 第 1 行: '=' 附近有语法错误。

源错误:


行 83: Connection.Open();
行 84: Response.Write("strCommand="+strCommand+"<hr/>");
行 85: dgEmployee.DataSource=Command.ExecuteReader();
行 86: dgEmployee.DataBind();
行 87: Connection.Close();

yanggood007 2006-07-06
  • 打赏
  • 举报
回复
偶头晕,你贴出错误的代码行就行了,并说明白抱什么错

110,567

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧