问题描述
以下是一个实现GridView表格嵌套的代码......我用AccessDataSource连接Access数据库时运行正常,换成SqlDataSource后运行报错,错误提示也莫名其妙:"第一行:'?'附近有错误"---TEST2.ASPX代码<%@PageLanguage="C#"AutoEventWireup="true"CodeFile="TEST2.aspx.cs"Inherits="TEST2"%><!DOCTYPEhtmlPUBLIC"-//W3C//DTDXHTML1.0Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><htmlxmlns="http://www.w3.org/1999/xhtml"><headrunat="server"><title>UntitledPage</title></head><body><formid="form1"runat="server"><div> <asp:SqlDataSourceID="SqlDataSource1"runat="server"ConnectionString="DataSource=ADSERVER;InitialCatalog=SZDK;PersistSecurityInfo=True;UserID=sa;Password=62055639"ProviderName="System.Data.SqlClient"SelectCommand="SELECT[ID],[U_NAME],[U_AGE]FROM[TEST]"></asp:SqlDataSource><asp:GridViewID="GridView1"runat="server"AutoGenerateColumns="False"DataSourceID="SqlDataSource1"DataKeyNames="U_NAME"OnRowDataBound="GridView1_RowDataBound"><Columns><asp:BoundFieldDataField="ID"HeaderText="ID"InsertVisible="False"ReadOnly="True"SortExpression="ID"/><asp:BoundFieldDataField="U_Name"HeaderText="U_Name"SortExpression="U_Name"/><asp:BoundFieldDataField="U_Age"HeaderText="U_Age"SortExpression="U_Age"/><asp:TemplateFieldHeaderText="Score"><ItemTemplate><asp:GridViewID="GridView2"runat="server"AutoGenerateColumns="False"DataKeyNames="U_NAMES"DataSourceID="SqlDataSource2"><Columns><asp:BoundFieldDataField="ID"HeaderText="ID"InsertVisible="False"ReadOnly="True"SortExpression="ID"/><asp:BoundFieldDataField="U_NAMES"HeaderText="Name"SortExpression="Name"/><asp:BoundFieldDataField="CHS"HeaderText="ENG"SortExpression="ENG"/><asp:BoundFieldDataField="ENG"HeaderText="CHS"SortExpression="CHS"/></Columns></asp:GridView><asp:SqlDataSourceID="SqlDataSource2"runat="server"ConnectionString="DataSource=ADSERVER;InitialCatalog=SZDK;PersistSecurityInfo=True;UserID=sa;Password=62055639"ProviderName="System.Data.SqlClient"SelectCommand="SELECT[ID],[U_NAMES],[CHN],[ENG]FROM[TEST2]WHERE([U_NAMES]=?)"><SelectParameters><asp:ParameterName="U_NAMES"DefaultValue=""Type="string"/></SelectParameters></asp:SqlDataSource></ItemTemplate></asp:TemplateField></Columns></asp:GridView></div></form></body></html>---TEST2.ASPX.CS代码usingSystem;usingSystem.Data;usingSystem.Data.SqlClient;usingSystem.Configuration;usingSystem.Collections;usingSystem.Web;usingSystem.Web.Security;usingSystem.Web.UI;usingSystem.Web.UI.WebControls;usingSystem.Web.UI.WebControls.WebParts;usingSystem.Web.UI.HtmlControls;publicpartialclassTEST2:System.Web.UI.Page{protectedvoidPage_Load(objectsender,EventArgse){}protectedvoidGridView1_RowDataBound(objectsender,GridViewRowEventArgse){if(e.Row.RowType==DataControlRowType.DataRow){stringu_name=GridView1.DataKeys[e.Row.DataItemIndex].Value.ToString();GridViewgv=(GridView)e.Row.FindControl("GridView2");SqlDataSourceds2=(SqlDataSource)e.Row.FindControl("SqlDataSource2");ds2.SelectParameters["U_NAMES"].DefaultValue=u_name;ds2.Select(DataSourceSelectArguments.Empty);--开始报错gv.DataBind();}}}
解决方案
解决方案二:
帮顶
解决方案三:
没人帮啊??自己顶起
解决方案四:
自己再顶
解决方案五:
帮帮吧啊
解决方案六:
再顶...我快疯了
解决方案七:
困了我几天了
解决方案八:
看错误提示,应该是sql语句问题吧
解决方案九:
你是读ACCESS你的数据源怎么是SQLDATASOURCE,我觉得应该是OLBD。。。,为什么非要用数据源控件呢,你可以写一个方法专门绑定GRIDVIEW,这样代码看起来也舒服写一个类,定义好SQLCONNECT对象,创建一个dataadpet对象,(单词记不得)staticvoidbindgridview(stringsql,gridviewview){dataadpetda=newdataadpet(sql,con);ds=newdataset();da.fill(ds);view.datasource=ds;view.databind();}protectedvoidPage_Load(objectsender,EventArgse){if(!ispostback){类名.bindgridview("select*from....",gridview1);}}}protectedvoidGridView1_RowDataBound(objectsender,GridViewRowEventArgse){if(e.Row.RowType==DataControlRowType.DataRow){stringu_name=GridView1.DataKey[e.Row.DataItemIndex].Value.ToString();GridViewgv=(GridView)e.Row.FindControl("GridView2");类名.bindgridview("select*from....",gv);}}这样肯定不会报错