我创建了一个绑定到本地SQL数据库文件(mdf)的GridView。数据库连接成功。我收到了来自我的EmptyDataText =" No Data"的错误消息。在GridView所假设的网页上。
非常感谢任何帮助。
这是我的div持有GridView:
<div style="border: 10px hidden #0B4A80; height: 108px; width: 494px; overflow: auto; margin-bottom: 40px;" >
<asp:SqlDataSource runat="server" ID="SqlDataSource1"
ConnectionString='<%$ ConnectionStrings:DefaultConnection %>'
SelectCommand="SELECT AspNetUsers.EmailConfirmed, AspNetUser.UserName, AspNetUsers.FirstName, AspNetUsers.LastName, AspNetUsers.BusinessName, AspNetRoles.Name FROM AspNetUsers INNER JOIN AspNetRoles ON AspNetUsers.Id = AspNetRoles.Id INNER JOIN AspNetUserRoles ON AspNetUsers.Id = AspNetUserRoles.UserId AND AspNetRoles.Id = AspNetUserRoles.RoleId"></asp:SqlDataSource>
<asp:GridView ID="GridView2" runat="server" SelectMethod ="" UpdateMethod ="" AutoGenerateColumns="true" DataKeyNames="Id" EmptyDataText="No Data" EnableModelValidation="false" ForeColor="#333333" GridLines="None" BorderStyle="None" BorderWidth="10px" Height="75px" PageSize="3" Width="472px">
<AlternatingRowStyle BackColor="White" ForeColor="#284775" />
<Columns>
<asp:BoundField DataField="Text" HeaderText="User Information" />
<asp:DynamicField DataField="EmailConfirmed" HeaderText="EmailConfirmed" SortExpression="EmailConfirmed"></asp:DynamicField>
<asp:BoundField DataField="FirstName" HeaderText="First Name" SortExpression="FirstName"></asp:BoundField>
<asp:BoundField DataField="LastName" HeaderText="Last Name" SortExpression="LastName"></asp:BoundField>
<asp:DynamicField DataField="BusinessName" HeaderText="Business Name" SortExpression="BusinessName"></asp:DynamicField>
<asp:DynamicField DataField="UserName" HeaderText="Email" SortExpression="UserName"></asp:DynamicField>
<asp:DynamicField DataField="Name" HeaderText="Role" SortExpression="Role" ></asp:DynamicField>
</Columns>
<EditRowStyle BackColor="#999999" />
<FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#D7E4FF" Font-Bold="True" ForeColor="#1484AA" />
<PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
<RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
<SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
</asp:GridView>
</div>
这是我的代码背后:
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindGrid1();
BindGrid2();
}
private void BindGrid2()
{
String queryString = "SELECT [EmailConfirmed], [UserName], [FirstName], [LastName], [BusinessName], [Name] FROM AspNetUsers INNER JOIN AspNetRoles ON AspNetUsers.Id = AspNetRoles.Id INNER JOIN AspNetUserRoles ON AspNetUsers.Id = AspNetUserRoles.UserId AND AspNetRoles.Id = AspNetUserRoles.RoleId";
DataSet ds = GetData(queryString);
if (ds.Tables.Count > 0)
{
GridView2.DataSource = ds;
GridView2.DataBind();
}
else
{
Response.Write("Unable to conenct to the database");
}
}
DataSet GetData(String queryString)
{
//Retrive the connection string in the web.config file.
String connectionString = ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString;
DataSet ds = new DataSet();
try
{
// Connect to the database and run query.
SqlConnection connection = new SqlConnection(connectionString);
SqlDataAdapter adapter = new SqlDataAdapter(queryString, connection);
//Fill the dataSet.
adapter.Fill(ds);
}
catch(System.Exception ex)
{
// The connection failed. Display an error message.
Response.Write("Unable to connect to the database.");
Response.Write(ex.Message);
}
return ds;
}
}
}
答案 0 :(得分:0)
请删除您的代码隐藏数据库调用。您已经在ASPX页面的SQL数据源中填充了gridview。
<强> ASPX:强>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="Id" DataSourceID="SqlDataSource1">
<Columns>
<asp:BoundField DataField="Id" HeaderText="Id" ReadOnly="True" SortExpression="Id" />
<asp:BoundField DataField="Email" HeaderText="Email" SortExpression="Email" />
<asp:CheckBoxField DataField="EmailConfirmed" HeaderText="EmailConfirmed" SortExpression="EmailConfirmed" />
<asp:BoundField DataField="PasswordHash" HeaderText="PasswordHash" SortExpression="PasswordHash" />
<asp:BoundField DataField="SecurityStamp" HeaderText="SecurityStamp" SortExpression="SecurityStamp" />
<asp:BoundField DataField="PhoneNumber" HeaderText="PhoneNumber" SortExpression="PhoneNumber" />
<asp:CheckBoxField DataField="PhoneNumberConfirmed" HeaderText="PhoneNumberConfirmed" SortExpression="PhoneNumberConfirmed" />
<asp:CheckBoxField DataField="TwoFactorEnabled" HeaderText="TwoFactorEnabled" SortExpression="TwoFactorEnabled" />
<asp:BoundField DataField="LockoutEndDateUtc" HeaderText="LockoutEndDateUtc" SortExpression="LockoutEndDateUtc" />
<asp:CheckBoxField DataField="LockoutEnabled" HeaderText="LockoutEnabled" SortExpression="LockoutEnabled" />
<asp:BoundField DataField="AccessFailedCount" HeaderText="AccessFailedCount" SortExpression="AccessFailedCount" />
<asp:BoundField DataField="UserName" HeaderText="UserName" SortExpression="UserName" />
<asp:BoundField DataField="TrialExpiration" HeaderText="TrialExpiration" SortExpression="TrialExpiration" />
<asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" />
</Columns>
</asp:GridView>
<asp:SqlDataSource runat="server" ID="SqlDataSource1"
ConnectionString='......' SelectCommand="SELECT AspNetUsers.EmailConfirmed, AspNetUser.UserName, AspNetUsers.FirstName, AspNetUsers.LastName, AspNetUsers.BusinessName, AspNetRoles.Name FROM AspNetUsers LEFT OUTER JOIN AspNetUserRoles ON AspNetUsers.Id = AspNetUserRoles.UserId LEFT OUTER JOIN AspNetRoles ON AspNetRoles.Id = AspNetUserRoles.RoleId"></asp:SqlDataSource>
C#代码隐藏:
public partial class _Default : Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
}
}
}
答案 1 :(得分:0)
您是否忘记打开连接?
using(SqlConnection connection = new SqlConnection(connectionString))
{
connection.Open();
SqlDataAdapter adapter = new SqlDataAdapter(queryString, connection);
//Fill the dataSet.
adapter.Fill(ds);
}