C# Gridview数据源服务器错误
我正在开发一个C#VS 2008和SQL Server 2008网站。但是,当我第一次运行此程序时,出现以下错误:C# Gridview数据源服务器错误,c#,visual-studio-2008,sql-server-2008,gridview,C#,Visual Studio 2008,Sql Server 2008,Gridview,我正在开发一个C#VS 2008和SQL Server 2008网站。但是,当我第一次运行此程序时,出现以下错误: The DataSourceID of 'GridView1' must be the ID of a control of type IDataSource. A control with ID 'AdventureWorks3.mdf' could not be found 是什么导致了这个错误?这是我的default.aspx文件。我已将GridView1配置为使用我的
The DataSourceID of 'GridView1' must be the ID of a control of type
IDataSource. A control with ID 'AdventureWorks3.mdf' could not be found
是什么导致了这个错误?这是我的default.aspx文件。我已将GridView1配置为使用我的AdventureWorks3.mdf文件,该文件存储在我的App_数据文件夹中。是否需要将此文件夹名称添加到此ASPX文件
<%@ Page Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" Title="Untitled Page" %>
<asp:Content ID="Content1" ContentPlaceHolderID="MainContent" Runat="Server">
<asp:Panel runat="server" ID="AuthenticatedMessagePanel">
<asp:Label runat="server" ID="WelcomeBackMessage"></asp:Label>
<table>
<tr >
<td>
<asp:Label ID="tableLabel" runat="server" Font-Bold="True"
Text="Select target table:"></asp:Label>
</td>
<td>
<asp:Label ID="inputLabel" runat="server" Font-Bold="True"
Text="Select input file:"></asp:Label>
</td></tr>
<tr><td valign="top">
<asp:Label ID="feedbackLabel" runat="server"></asp:Label>
<asp:SqlDataSource ID="SelectTables" runat="server"
ConnectionString="<%$ ConnectionStrings:AdventureWorks3_SelectTables %>"
SelectCommand="SELECT TABLE_NAME
FROM information_schema.Tables
WHERE TABLE_TYPE = 'BASE TABLE' AND TABLE_SCHEMA = @SchemaName">
<SelectParameters>
<asp:FormParameter DefaultValue="Address" FormField="upload"
Name="SchemaName" />
</SelectParameters>
</asp:SqlDataSource>
<asp:GridView ID="GridView1" DatasourceID="SelectTables" runat="server" style="WIDTH: 400px;"
CellPadding="4" ForeColor="#333333"
GridLines="None" onselectedindexchanged="GridView1_SelectedIndexChanged"
AutoGenerateSelectButton="True" DataKeyNames="TABLE_NAME">
<RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
<Columns>
<asp:BoundField HeaderText="Table_Name" />
</Columns>
<FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
<SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
<HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<EditRowStyle BackColor="#999999" />
<AlternatingRowStyle BackColor="White" ForeColor="#284775" />
</asp:GridView>
</td>
<td valign="top">
<input id="uploadFile" type="file" size="26" runat="server" name="uploadFile"
title="UploadFile" class="greybar"
enableviewstate="True" />
</td></tr>
</table>
</asp:Panel>
<asp:Panel runat="Server" ID="AnonymousMessagePanel">
<asp:HyperLink runat="server" ID="lnkLogin" Text="Log In" NavigateUrl="~/Login.aspx">
</asp:HyperLink>
</asp:Panel> </asp:Content>
如果要在代码中设置数据源,则不需要DataSourceID=“AdventureWorks3.mdf”。此外,AdventureWorks3.mdf不能用作数据源,请尝试使用SqlDataSource或任何其他如果在代码中设置数据源,则不需要DataSourceID=“AdventureWorks3.mdf”。此外,AdventureWorks3.mdf也不能用作数据源,请尝试使用SqlDataSource或任何其他您不能将.mdf文件名粘贴到datagridview中,您必须将其加载到数据源中,然后将其绑定到网格。看起来这就是您正在做的,所以只需删除datasourceID属性 编辑虽然我现在无法测试,但您似乎没有将列绑定到数据字段 e、 g
您不能只将.mdf文件名粘贴到datagridview中,您必须将其加载到数据源中,然后将其绑定到网格。看起来这就是您正在做的,所以只需删除datasourceID属性 编辑虽然我现在无法测试,但您似乎没有将列绑定到数据字段 e、 g
谢谢大家。我现在编辑了我的ASPX文件的代码,并将其更新到上面的帖子中。这个错误消失了。但是现在桌子还是没有出现。您能告诉我需要修复什么吗?从上面的代码可以看出,您从未调用Load_GridData(),因此网格从来都不是boundedGood tip。我刚刚更新了上面的ASPX.CS文件。但这张桌子仍然没有显示出来。由于我在ASPX文件中设置了sqldatasource,所以我刚刚注释掉了Load_GridData代码。有什么想法吗?好的,现在您使用的是SqlDataSource,您应该将DataSourceID=“SelectTables”添加到网格中。很抱歉,我忘了早些时候更新我的ASPX代码。我已经将其设置为SelectTables,但仍然存在此问题。我刚刚更新了我的ASPX代码以反映当前的值。谢谢大家。我现在编辑了我的ASPX文件的代码,并将其更新到上面的帖子中。这个错误消失了。但是现在桌子还是没有出现。您能告诉我需要修复什么吗?从上面的代码可以看出,您从未调用Load_GridData(),因此网格从来都不是boundedGood tip。我刚刚更新了上面的ASPX.CS文件。但这张桌子仍然没有显示出来。由于我在ASPX文件中设置了sqldatasource,所以我刚刚注释掉了Load_GridData代码。有什么想法吗?好的,现在您使用的是SqlDataSource,您应该将DataSourceID=“SelectTables”添加到网格中。很抱歉,我忘了早些时候更新我的ASPX代码。我已经将其设置为SelectTables,但仍然存在此问题。我刚刚更新了我的ASPX代码以反映当前的值。
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections.Generic;
using System.IO;
using System.Drawing;
using System.ComponentModel;
using System.Data.SqlClient;
using ADONET_namespace;
using System.Security.Principal;
//using System.Windows;
public partial class _Default : System.Web.UI.Page
//namespace AddFileToSQL
{
//protected System.Web.UI.HtmlControls.HtmlInputFile uploadFile;
protected System.Web.UI.HtmlControls.HtmlInputButton btnOWrite;
protected System.Web.UI.HtmlControls.HtmlInputButton btnAppend;
protected System.Web.UI.WebControls.Label Label1;
protected static string inputfile = "";
public static string targettable;
public static string selection;
// Number of controls added to view state
protected int default_NumberOfControls
{
get
{
if (ViewState["default_NumberOfControls"] != null)
{
return (int)ViewState["default_NumberOfControls"];
}
else
{
return 0;
}
}
set
{
ViewState["default_NumberOfControls"] = value;
}
}
protected void uploadFile_onclick(object sender, EventArgs e)
{
}
protected void Load_GridData()
{
//GridView1.DataSource = ADONET_methods.DisplaySchemaTables();
//GridView1.DataBind();
}
protected void btnOWrite_Click(object sender, EventArgs e)
{
if (uploadFile.PostedFile.ContentLength > 0)
{
feedbackLabel.Text = "You do not have sufficient access to overwrite table records.";
}
else
{
feedbackLabel.Text = "This file does not contain any data.";
}
}
protected void btnAppend_Click(object sender, EventArgs e)
{
string fullpath = Page.Request.PhysicalApplicationPath;
string path = uploadFile.PostedFile.FileName;
if (File.Exists(path))
{
// Create a file to write to.
try
{
StreamReader sr = new StreamReader(path);
string s = "";
while (sr.Peek() > 0)
s = sr.ReadLine();
sr.Close();
}
catch (IOException exc)
{
Console.WriteLine(exc.Message + "Cannot open file.");
return;
}
}
if (uploadFile.PostedFile.ContentLength > 0)
{
inputfile = System.IO.File.ReadAllText(path);
Session["Message"] = inputfile;
Response.Redirect("DataMatch.aspx");
}
else
{
feedbackLabel.Text = "This file does not contain any data.";
}
}
protected void Page_Load(object sender, EventArgs e)
{
if (Request.IsAuthenticated)
{
WelcomeBackMessage.Text = "Welcome back, " + User.Identity.Name + "!";
// Reference the CustomPrincipal / CustomIdentity
CustomIdentity ident = User.Identity as CustomIdentity;
if (ident != null)
WelcomeBackMessage.Text += string.Format(" You are the {0} of {1}.", ident.Title, ident.CompanyName);
AuthenticatedMessagePanel.Visible = true;
AnonymousMessagePanel.Visible = false;
if (!Page.IsPostBack)
{
Load_GridData();
}
}
else
{
AuthenticatedMessagePanel.Visible = false;
AnonymousMessagePanel.Visible = true;
}
}
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
GridViewRow row = GridView1.SelectedRow;
targettable = row.Cells[2].Text;
}
}
<asp:boundfield datafield="CustomerID" headertext="Customer ID"/>