首页 新闻 会员 周边 捐助

关于页面Session传值到变量后,点击其他页面后,再使用变量查询数据时,页面总是报错。

0
悬赏园豆:50 [已关闭问题] 关闭于 2013-11-17 10:22

通过后台绑定gridview的都有问题,通过前台添加数据源的,查询都没有问题,不知是何故?

并提示:

关键字 'and' 附近有语法错误。

这个是我的查询方法:

public void gv_bind()
{

int idbiaozhun = abcd;

string selesql = "select id_stu,stu_name,xiaozu_name, defen_dxbenke,defen_dxleiji,defen_xiaozudxbenke from jifen where id_biaozhun= " + idbiaozhun + "and id_jiaoshi=" + idjiaoshi + "and id_banji=" + idbanji+ "order by xiaozu_name desc";
DataSet ds = cc.ExceDS(selesql);
GridView1.DataSource = ds;
GridView1.DataKeyNames = new string[] { "id_stu" };
GridView1.DataBind();//将控件及其所有子控件绑定到指定的数据源
if (ds.Tables[0].Rows.Count == 0)
{
string sqlcharu_stu = "insert into jifen(id_jiaoshi,id_banji,id_stu,stu_name,id_fenzu,xiaozu_name) select id_jiaoshi,id_banji,id_stu,stu_name,id_fenzu,xiaozu_name from stu_user where stu_user.id_jiaoshi="+idjiaoshi+" and stu_user.id_banji="+idbanji;
string sqlset_biaozhun = "update jifen set id_biaozhun ="+idbiaozhun+" where id_jiaoshi=" + idjiaoshi + "and id_banji=" + idbanji + " and id_biaozhun is null ";

cc.ExecSQL(sqlcharu_stu);
cc.ExecSQL(sqlset_biaozhun);
DataSet dd = cc.ExceDS(selesql);
GridView1.DataSource = dd;

GridView1.DataKeyNames = new string[] { "id_stu" };
GridView1.DataBind();//将控件及其所有子控件绑定到指定的数据源
}


}

bacd是全局变量,通过点击控件的按钮获得,查询里,idjiaoshi,idbanji,从页面  protected void Page_Load(object sender, EventArgs e)里获得:

string idbanji= Session["jiaoshi_name"].ToString();
string idjiaoshi = Session["keti_name"].ToString();

当第一次运行时,没有任何问题,只要你不离开这个页面。当离开页面去做别的事情时,就会报错。

这个是 变量,abcd的取值方法:

protected void BulletedList1_Click(object sender, System.Web.UI.WebControls.BulletedListEventArgs e)
{
GridView3.Visible = false;
GridView2.Visible = false;
GridView1.Visible = true;

System.Web.UI.WebControls.ListItem li = BulletedList1.Items[e.Index];

abcd = Convert.ToInt16( li.Value);
//Response.Write("<script>alert('"+abcd+"')</script>");
gv_bind();
Response.Write("'" + abcd + "'");

}

每次查询时,页面都是可以得到abcd的。

对了,还有,我已经在web.config中,把Session的默认时间变为60分钟了。

 

问题报错:

“/pjxt”应用程序中的服务器错误。


 

关键字 'and' 附近有语法错误。

说明: 执行当前 Web 请求期间,出现未经处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。 

异常详细信息: System.Data.SqlClient.SqlException: 关键字 'and' 附近有语法错误。

源错误: 

行 215: sda.SelectCommand = sqlCmd; 行 216: DataSet ds = new DataSet(); 行 217: sda.Fill(ds); 行 218: return ds; 行 219: }


源文件: f:\pjxt\pjxt\App_Code\DB.cs    行: 217 

堆栈跟踪: 

[SqlException (0x80131904): 关键字 'and' 附近有语法错误。] System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection) +2030802 System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection) +5009584 System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning() +234 System.Data.SqlClient.TdsParser.Run(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj) +2275 System.Data.SqlClient.SqlDataReader.ConsumeMetaData() +33 System.Data.SqlClient.SqlDataReader.get_MetaData() +86 System.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds, RunBehavior runBehavior, String resetOptionsString) +311 System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, Boolean async) +987 System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method, DbAsyncResult result) +162 System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method) +32 System.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior behavior, String method) +141 System.Data.SqlClient.SqlCommand.ExecuteDbDataReader(CommandBehavior behavior) +12 System.Data.Common.DbCommand.System.Data.IDbCommand.ExecuteReader(CommandBehavior behavior) +10 System.Data.Common.DbDataAdapter.FillInternal(DataSet dataset, DataTable[] datatables, Int32 startRecord, Int32 maxRecords, String srcTable, IDbCommand command, CommandBehavior behavior) +144 System.Data.Common.DbDataAdapter.Fill(DataSet dataSet, Int32 startRecord, Int32 maxRecords, String srcTable, IDbCommand command, CommandBehavior behavior) +319 System.Data.Common.DbDataAdapter.Fill(DataSet dataSet) +94 DB.ExceDS(String strCom) in f:\pjxt\pjxt\App_Code\DB.cs:217 teacherwork.gv_bind() in f:\pjxt\pjxt\teacherwork.aspx.cs:199 teacherwork.BulletedList1_Click(Object sender, BulletedListEventArgs e) in f:\pjxt\pjxt\teacherwork.aspx.cs:330 System.Web.UI.WebControls.BulletedList.OnClick(BulletedListEventArgs e) +115 System.Web.UI.WebControls.BulletedList.RaisePostBackEvent(String eventArgument) +164 System.Web.UI.WebControls.BulletedList.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +13 System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +13 System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +176 System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +5563

 


版本信息: Microsoft .NET Framework 版本:4.0.30319; ASP.NET 版本:4.0.30319.1

 前台绑定的数据查询就没有问题随时都能查到:

 

<asp:GridView ID="GridView3" runat="server" AllowSorting="True"
AutoGenerateColumns="False"
DataSourceID="SqlDataSource1" Visible="False" Width="837px">
<Columns>
<asp:BoundField DataField="id_stu" HeaderText="编号"
SortExpression="id_stu" />
<asp:BoundField DataField="stu_name" HeaderText="姓名"
SortExpression="stu_name" />
<asp:BoundField DataField="xiaozu_name" HeaderText="分组"
SortExpression="xiaozu_name" />
<asp:BoundField DataField="Column1" HeaderText="个人累计得分"
SortExpression="Column1" ReadOnly="True" />
<asp:BoundField DataField="defen_xiaozudxleiji" HeaderText="小组累计得分"
SortExpression="defen_xiaozudxleiji" />
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConflictDetection="CompareAllValues"
ConnectionString="<%$ ConnectionStrings:ktpjxtConnectionString %>"
DeleteCommand="DELETE FROM [jifen] WHERE [id_defen] = @original_id_defen AND (([id_jiaoshi] = @original_id_jiaoshi) OR ([id_jiaoshi] IS NULL AND @original_id_jiaoshi IS NULL)) AND (([id_keti] = @original_id_keti) OR ([id_keti] IS NULL AND @original_id_keti IS NULL)) AND (([id_fenzu] = @original_id_fenzu) OR ([id_fenzu] IS NULL AND @original_id_fenzu IS NULL)) AND (([id_banji] = @original_id_banji) OR ([id_banji] IS NULL AND @original_id_banji IS NULL)) AND (([xiaozu_name] = @original_xiaozu_name) OR ([xiaozu_name] IS NULL AND @original_xiaozu_name IS NULL)) AND (([id_biaozhun] = @original_id_biaozhun) OR ([id_biaozhun] IS NULL AND @original_id_biaozhun IS NULL)) AND (([id_stu] = @original_id_stu) OR ([id_stu] IS NULL AND @original_id_stu IS NULL)) AND (([stu_name] = @original_stu_name) OR ([stu_name] IS NULL AND @original_stu_name IS NULL)) AND (([defen_dxbenke] = @original_defen_dxbenke) OR ([defen_dxbenke] IS NULL AND @original_defen_dxbenke IS NULL)) AND (([defen_dxleiji] = @original_defen_dxleiji) OR ([defen_dxleiji] IS NULL AND @original_defen_dxleiji IS NULL)) AND (([defen_xiaozudxbenke] = @original_defen_xiaozudxbenke) OR ([defen_xiaozudxbenke] IS NULL AND @original_defen_xiaozudxbenke IS NULL)) AND (([defen_xiaozudxleiji] = @original_defen_xiaozudxleiji) OR ([defen_xiaozudxleiji] IS NULL AND @original_defen_xiaozudxleiji IS NULL)) AND (([defen_grheji] = @original_defen_grheji) OR ([defen_grheji] IS NULL AND @original_defen_grheji IS NULL))"
InsertCommand="INSERT INTO [jifen] ([id_jiaoshi], [id_keti], [id_fenzu], [id_banji], [xiaozu_name], [id_biaozhun], [id_stu], [stu_name], [defen_dxbenke], [defen_dxleiji], [defen_xiaozudxbenke], [defen_xiaozudxleiji], [defen_grheji]) VALUES (@id_jiaoshi, @id_keti, @id_fenzu, @id_banji, @xiaozu_name, @id_biaozhun, @id_stu, @stu_name, @defen_dxbenke, @defen_dxleiji, @defen_xiaozudxbenke, @defen_xiaozudxleiji, @defen_grheji)"
OldValuesParameterFormatString="original_{0}"
SelectCommand="select id_stu,stu_name,xiaozu_name ,sum(defen_dxleiji) ,defen_xiaozudxleiji from jifen where (([id_jiaoshi] = @id_jiaoshi) AND ([id_banji] = @id_banji)) group by xiaozu_name,stu_name,id_stu,defen_xiaozudxleiji ORDER BY xiaozu_name DESC

 

 

我把取到的值,放到  protected void Page_Load(object sender, EventArgs e)这里,有没有影响?

if (!IsPostBack)
{
string jiaoshiname = Session["jiaoshi_name"].ToString();
string ketiname = Session["keti_name"].ToString();
string banjiname = Session["banji_name"].ToString();
idjiaoshi = Session["id_jiaoshi"].ToString();
idbanji = Session["id_banji"].ToString();
idketi = Session["id_keti"].ToString();

}

 

问题的原因:是通过Session传到我的全局变量idjiaoshi,idbanji的值,丢失了,能有什么办法,留着这个值呢?

 又捣鼓了半天,还是这个问题????

除了Session,页面传值是不是就没有这个问题呢?

 

bj大海的主页 bj大海 | 初学一级 | 园豆:56
提问于:2013-11-13 10:42
< >
分享
所有回答(6)
0

从楼主贴出的代码看的话,应该是Session取值没取到,导致sql拼接错误,楼主Session是在哪里赋值地?

我把取到的值,放到  protected void Page_Load(object sender, EventArgs e)这里,有没有影响?

if (!IsPostBack)
{
string jiaoshiname = Session["jiaoshi_name"].ToString();
string ketiname = Session["keti_name"].ToString();
string banjiname = Session["banji_name"].ToString();
idjiaoshi = Session["id_jiaoshi"].ToString();
idbanji = Session["id_banji"].ToString();
idketi = Session["id_keti"].ToString();

}

 

把if (!IsPostBack)这个判断去掉应该就可以了

netqiang | 园豆:405 (菜鸟二级) | 2013-11-13 10:54
0

断点从Session赋值给变量时的代码,查看是否有值,另外类型是否一致,最好贴出异常的信息

 

看了你贴的异常,这是与你拼成的SQL语句错误有关,你把拼成的SQL到SQLSevre中去执行就知道是什么问题了,异常中说是 SQL语句的And 附近有错误string selesql = "select id_stu,stu_name,xiaozu_name, defen_dxbenke,defen_dxleiji,defen_xiaozudxbenke from jifen where id_biaozhun= " + idbiaozhun + "and id_jiaoshi=" + idjiaoshi + "and id_banji=" + idbanji+ "order by xiaozu_name desc";

where 后面是字符串到sql 中当然不行,要把条件加上单引号 如and id_jiaoshi="' + idjiaoshi + '" andid_banji="' + idbanji+ '"

Zery | 园豆:6151 (大侠五级) | 2013-11-13 11:18

问题的原因:是通过Session传到我的全局变量idjiaoshi,idbanji的值,丢失了,能有什么办法,留着这个值呢?

支持(0) 反对(0) bj大海 | 园豆:56 (初学一级) | 2013-11-13 17:05

@bj大海:那干嘛不直接从session里取呢?,定义变量请求一次就会刷新一次,值就会被清空 

支持(0) 反对(0) Zery | 园豆:6151 (大侠五级) | 2013-11-13 17:45
0

我想:你通过前台添加数据源的不管什么时候都会自动绑定数据源,而你后台添加的数据源应该是你忘记了处理绑定数据源的逻辑吧。当然这只是猜测。还希望你能把问题说的清楚些。。

jone_e | 园豆:1410 (小虾三级) | 2013-11-13 12:06

看看现在的问题,能帮下吗?

支持(0) 反对(0) bj大海 | 园豆:56 (初学一级) | 2013-11-13 13:06

@bj大海: 

支持(0) 反对(0) jone_e | 园豆:1410 (小虾三级) | 2013-11-13 14:44

@jone_e: 如果你的参数是数字类型的,那么你的写法没有问题。你第一次进入页面没有报错,大概就是因为你在前台绑定的数据源在起作用,实际上根本就没有走你后台绑定数据源的方法。做一个简单的测试:把你的前台的数据源去掉,验证一下。

支持(0) 反对(0) jone_e | 园豆:1410 (小虾三级) | 2013-11-13 14:48

@jone_e: 问题的原因:是通过Session传到我的全局变量idjiaoshi,idbanji的值,丢失了,能有什么办法,留着这个值呢?

支持(0) 反对(0) bj大海 | 园豆:56 (初学一级) | 2013-11-13 17:04

@bj大海: 把你Page_Load里面取Session值的if (!IsPostBack)去掉试试

支持(0) 反对(0) netqiang | 园豆:405 (菜鸟二级) | 2013-11-13 17:08

@bj大海: 既然你的是全局变量,那么就不应该放在if(!ispostback){}里面。当你第二次进入这个页面的时候,也就是你从其他页面再次进入这个页面,if条件里的语句是不会执行的。

支持(0) 反对(0) jone_e | 园豆:1410 (小虾三级) | 2013-11-13 18:14
0

至少得贴个错误吧~

幻天芒 | 园豆:37207 (高人七级) | 2013-11-13 12:53
0

先把SQL语句改为:

            string selesql = "SELECT [id_stu],[stu_name],[xiaozu_name], [defen_dxbenke],[defen_dxleiji],[defen_xiaozudxbenke] FROM [jifen] WHERE [id_biaozhun]= '" + idbiaozhun + "' AND [id_jiaoshi] ='" + idjiaoshi + "' AND [id_banji]=" + idbanji+ " ORDER BY [xiaozu_name] DESC";           
Insus.NET | 园豆:1132 (小虾三级) | 2013-11-13 13:14

问题的原因:是通过Session传到我的全局变量idjiaoshi,idbanji的值,丢失了,能有什么办法,留着这个值呢?

支持(0) 反对(0) bj大海 | 园豆:56 (初学一级) | 2013-11-13 17:04
0

=" + idjiaoshi + "and id_banji=" 你的and 前面至少要有个空格吧?? 没空格 sql语句 和前面的连在一起了 还有字符串的话 加上单引号

wolfy | 园豆:2636 (老鸟四级) | 2013-11-13 13:36
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册