【发布时间】:2015-12-17 19:20:43
【问题描述】:
我创建了一个绑定到本地 SQL 数据库文件 (mdf) 的 GridView。数据库连接成功。我确实收到了来自我的 EmptyDataText="No Data" 的错误消息,该错误消息位于 GridView 所在的网页上。
任何帮助将不胜感激。
这是我持有 GridView 的 div:
<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;
}
}
}
【问题讨论】:
标签: c# sql gridview webforms asp.net-4.5