GridView从数据库读数据显示问题
代码文件 ***********************
<%@ Page Language= "VB "%>
<html xmlns= "http://www.w3.org/1999/xhtml " >
<head runat= "server ">
<title> GridView & DetailsView Controls </title>
</head>
<body>
<form id= "form1 " runat= "server ">
<p>
<asp:GridView ID= "GridView1 " runat= "server " DataSourceID= "SqlDataSource1 " AllowPaging= "true "
BorderColor= "#DEBA84 " BorderStyle= "none " BorderWidth= "1px " BackColor= "#DEBA84 " CellSpacing= "2 "
cellpadding= "3 " DataKeyNames= "CustomerID " AutoGenerateSelectButton= "true "
autogenerateColumns= "false " PageSize= "5 ">
<FooterStyle ForeColor= "#8C4510 " BackColor= "#F7DFB5 " />
<PagerStyle ForeColor= "#8c4510 " HorizontalAlign= "Center " />
<HeaderStyle ForeColor= "White " BackColor= "#A55129 " Font-Bold= "true " />
<Columns>
<asp:BoundField ReadOnly= "true " HeaderText= "CustomerID " DataField= "CustomerID "
sortExpression= "CustomerID " />
<asp:BoundField HeaderText= "CompanyName " datafield= "CompanyName "
SortExpression= "companyName " />
<asp:BoundField HeaderText= "ContactName " DataField= "contactName "
sortExpression= "contactName " />
<asp:BoundField HeaderText= "ContactTitle " DataField= "ContactTitle "
sortExpression= "ContactTitle " />
<asp:BoundField HeaderText= "Address " DataField= "Address " SortExpression= "Address " />
<asp:BoundField HeaderText= "City " DataField= "City " SortExpression= "City " />
<asp:BoundField HeaderText= "Region " DataField= "Region " SortExpression= "Region " />
<asp:BoundField HeaderText= "PostalCode " DataField= "PostalCode " SortExpression= "PostalCode " />
<asp:BoundField HeaderText= "Country " DataField= "Country " SortExpression= "Country " />
<asp:BoundField HeaderText= "Phone " DataField= "Phone " SortExpression= "Phone " />
<asp:BoundField HeaderText= "Fax " DataField= "Fax " SortExpression= "Fax " />
</Columns>
<SelectedRowStyle ForeColor= "White " BackColor= "#738A9C " Font-Bold= "true " />
<RowStyle ForeColor= "#BC4510 " BackColor= "#FFE7E7 " />
</asp:GridView>
</p>
<p> <b> Customer Details: </b> </p>
<asp:DetailsView ID= "DetailsView1 " runat= "server " DataSourceID= "SqlDataSource2 " BorderColor= "#DEBA84 "
borderstyle= "None " BorderWidth= "1px " BackColor= "#DEBA84 " CellSpacing= "2 " CellPadding= "3 "
autoGenerateRows= "False " DataKeyNames= "CustomerID ">
<footerstyle ForeColor= "#8C4510 " BackColor= "#F7DFB5 " />
<RowStyle ForeColor= "#8C4510 " BackColor= "#FFF7E7 " />
<PagerStyle forecolor= "#8C4510 " horizontalAlign= "Center "> </PagerStyle>
<Fields>
<asp:BoundField ReadOnly= "true " HeaderText= "CustomerID " DataField= "CustomerID "
sortExpression= "CustomerID " />
<asp:BoundField HeaderText= "CompanyName " datafield= "CompanyName "
SortExpression= "companyName " />
<asp:BoundField HeaderText= "ContactName " DataField= "contactName "
sortExpression= "contactName " />
<asp:BoundField HeaderText= "ContactTitle " DataField= "ContactTitle "
sortExpression= "ContactTitle " />
<asp:BoundField HeaderText= "Address " DataField= "Address " SortExpression= "Address " />
<asp:BoundField HeaderText= "City " DataField= "City " SortExpression= "City " />
<asp:BoundField HeaderText= "Region " DataField= "Region " SortExpression= "Region " />
<asp:BoundField HeaderText= "PostalCode " DataField= "PostalCode " SortExpression= "PostalCode " />
<asp:BoundField HeaderText= "Country " DataField= "Country " SortExpression= "Country " />
<asp:BoundField HeaderText= "Phone " DataField= "Phone " SortExpression= "Phone " />
<asp:BoundField HeaderText= "Fax " DataField= "Fax " SortExpression= "Fax " />
</Fields>
<HeaderStyle ForeColor= "White " BackColor= "#A55129 " Font-Bold= "true " />
<EditRowStyle ForeColor= "White " BackColor= "#738A9C " Font-Bold= "true " />
</asp:DetailsView>
<asp:SqlDataSource ID= "SqlDataSource1 " runat= "server " SelectCommand= "select * from [customers] "
connectionstring= "DSN_Northwind " />
<asp:SqlDataSource ID= "SqlDataSource2 " runat= "server " SelectCommand= "select * from [Customers] "
filterExpression= "CustomerID= '{0} ' " ConnectionString= "DSN_Northwind ">
<FilterParameters>
<asp:ControlParameter Name= "CustomerID " ControlID= "GridView1 " PropertyName= "SelectedValue " />
</FilterParameters>
</asp:SqlDataSource>
</form>
</body>
</html>
*******************************************
错误提示为¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥
从索引 0 处开始,初始化字符串的格式不符合规范。
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。
异常详细信息: System.ArgumentException: 从索引 0 处开始,初始化字符串的格式不符合规范。
源错误:
执行当前 Web 请求期间生成了未处理的异常。可以使用下面的异常堆栈跟踪信息确定有关异常原因和发生位置的信息。
堆栈跟踪:
[ArgumentException: 从索引 0 处开始,初始化字符串的格式不符合规范。]
System.Data.Common.DbConnectionOptions.GetKeyValuePair(String connectionString, Int32 currentPosition, StringBuilder buffer, Boolean useOdbcRules, String& keyname, String& keyvalue) +1286
System.Data.Common.DbConnectionOptions.ParseInternal(Hashtable parsetable, String connectionString, Boolean buildChain, Hashtable synonyms, Boolean firstKey) +115
System.Data.Common.DbConnectionOptions..ctor(String connectionString, Hashtable synonyms, Boolean useOdbcRules) +99
System.Data.SqlClient.SqlConnectionString..ctor(String connectionString) +52
System.Data.SqlClient.SqlConnectionFactory.CreateConnectionOptions(String connectionString, DbConnectionOptions previous) +25
System.Data.ProviderBase.DbConnectionFactory.GetConnectionPoolGroup(String connectionString, DbConnectionPoolGroupOptions poolOptions, DbConnectionOptions& userConnectionOptions) +141
System.Data.SqlClient.SqlConnection.ConnectionString_Set(String value) +38
System.Data.SqlClient.SqlConnection.set_ConnectionString(String value) +4
System.Web.UI.WebControls.SqlDataSourceView.ExecuteSelect(DataSourceSelectArguments arguments) +128
System.Web.UI.DataSourceView.Select(DataSourceSelectArguments arguments, DataSourceViewSelectCallback callback) +13
System.Web.UI.WebControls.DataBoundControl.PerformSelect() +140
System.Web.UI.WebControls.BaseDataBoundControl.DataBind() +68
System.Web.UI.WebControls.GridView.DataBind() +4
System.Web.UI.WebControls.BaseDataBoundControl.EnsureDataBound() +61
System.Web.UI.WebControls.CompositeDataBoundControl.CreateChildControls() +67
System.Web.UI.Control.EnsureChildControls() +97
System.Web.UI.Control.PreRenderRecursiveInternal() +50
System.Web.UI.Control.PreRenderRecursiveInternal() +171
System.Web.UI.Control.PreRenderRecursiveInternal() +171
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +5731
¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥
请会的指点一下?
[解决办法]
<%@ Page Language= "VB "%>
<html xmlns= "http://www.w3.org/1999/xhtml " >
<head id= "Head1 " runat= "server ">
<title> GridView & DetailsView Controls </title>
</head>
<body>
<form id= "form1 " runat= "server ">
<p>
<asp:GridView ID= "GridView1 " runat= "server " DataSourceID= "SqlDataSource1 " AllowPaging= "true "
BorderColor= "#DEBA84 " BorderStyle= "none " BorderWidth= "1px " BackColor= "#DEBA84 " CellSpacing= "2 "
cellpadding= "3 " DataKeyNames= "CustomerID " AutoGenerateSelectButton= "true "
autogenerateColumns= "false " PageSize= "5 ">
<FooterStyle ForeColor= "#8C4510 " BackColor= "#F7DFB5 " />
<PagerStyle ForeColor= "#8c4510 " HorizontalAlign= "Center " />
<HeaderStyle ForeColor= "White " BackColor= "#A55129 " Font-Bold= "true " />
<Columns>
<asp:BoundField ReadOnly= "true " HeaderText= "CustomerID " DataField= "CustomerID "
sortExpression= "CustomerID " />
<asp:BoundField HeaderText= "CompanyName " datafield= "CompanyName "
SortExpression= "companyName " />
<asp:BoundField HeaderText= "ContactName " DataField= "contactName "
sortExpression= "contactName " />
<asp:BoundField HeaderText= "ContactTitle " DataField= "ContactTitle "
sortExpression= "ContactTitle " />
<asp:BoundField HeaderText= "Address " DataField= "Address " SortExpression= "Address " />
<asp:BoundField HeaderText= "City " DataField= "City " SortExpression= "City " />
<asp:BoundField HeaderText= "Region " DataField= "Region " SortExpression= "Region " />
<asp:BoundField HeaderText= "PostalCode " DataField= "PostalCode " SortExpression= "PostalCode " />
<asp:BoundField HeaderText= "Country " DataField= "Country " SortExpression= "Country " />
<asp:BoundField HeaderText= "Phone " DataField= "Phone " SortExpression= "Phone " />
<asp:BoundField HeaderText= "Fax " DataField= "Fax " SortExpression= "Fax " />
</Columns>
<SelectedRowStyle ForeColor= "White " BackColor= "#738A9C " Font-Bold= "true " />
<RowStyle ForeColor= "#BC4510 " BackColor= "#FFE7E7 " />
</asp:GridView>
</p>
<p> <b> Customer Details: </b> </p>
<asp:DetailsView ID= "DetailsView1 " runat= "server " DataSourceID= "SqlDataSource2 " BorderColor= "#DEBA84 "
borderstyle= "None " BorderWidth= "1px " BackColor= "#DEBA84 " CellSpacing= "2 " CellPadding= "3 "
autoGenerateRows= "False " DataKeyNames= "CustomerID ">
<footerstyle ForeColor= "#8C4510 " BackColor= "#F7DFB5 " />
<RowStyle ForeColor= "#8C4510 " BackColor= "#FFF7E7 " />
<PagerStyle forecolor= "#8C4510 " horizontalAlign= "Center "> </PagerStyle>
<Fields>
<asp:BoundField ReadOnly= "true " HeaderText= "CustomerID " DataField= "CustomerID "
sortExpression= "CustomerID " />
<asp:BoundField HeaderText= "CompanyName " datafield= "CompanyName "
SortExpression= "companyName " />
<asp:BoundField HeaderText= "ContactName " DataField= "contactName "
sortExpression= "contactName " />
<asp:BoundField HeaderText= "ContactTitle " DataField= "ContactTitle "
sortExpression= "ContactTitle " />
<asp:BoundField HeaderText= "Address " DataField= "Address " SortExpression= "Address " />
<asp:BoundField HeaderText= "City " DataField= "City " SortExpression= "City " />
<asp:BoundField HeaderText= "Region " DataField= "Region " SortExpression= "Region " />
<asp:BoundField HeaderText= "PostalCode " DataField= "PostalCode " SortExpression= "PostalCode " />
<asp:BoundField HeaderText= "Country " DataField= "Country " SortExpression= "Country " />
<asp:BoundField HeaderText= "Phone " DataField= "Phone " SortExpression= "Phone " />
<asp:BoundField HeaderText= "Fax " DataField= "Fax " SortExpression= "Fax " />
</Fields>
<HeaderStyle ForeColor= "White " BackColor= "#A55129 " Font-Bold= "true " />
<EditRowStyle ForeColor= "White " BackColor= "#738A9C " Font-Bold= "true " />
</asp:DetailsView>
<asp:SqlDataSource ID= "SqlDataSource1 " runat= "server " SelectCommand= "select * from [customers] "
connectionstring= " <%$ ConnectionStrings:NorthwindConnectionString %> " />
<asp:SqlDataSource ID= "SqlDataSource2 " runat= "server " SelectCommand= "select * from [Customers] "
filterExpression= "CustomerID= '{0} ' " ConnectionString= " <%$ ConnectionStrings:NorthwindConnectionString %> ">
<FilterParameters>
<asp:ControlParameter Name= "CustomerID " ControlID= "GridView1 " PropertyName= "SelectedValue " />
</FilterParameters>
</asp:SqlDataSource>
</form>
</body>
</html>
--------------------------------------
在web.config加入
<connectionStrings>
<add name= "NorthwindConnectionString " connectionString= "Data Source=127.0.0.1;Initial Catalog=Northwind;User ID=sa "
providerName= "System.Data.SqlClient " />
</connectionStrings>
不知道是不是你的数据源设置有问题才出问题的,改成上面这样设置可以运行,我这里测试通过