Я создал GridView, привязанный к локальному файлу базы данных SQL (mdf). Соединение с базой данных выполнено успешно. Я получаю сообщение об ошибке из моего EmptyDataText = «Нет данных» на веб-странице, где должен быть GridView.My GridView не будет заполняться данными из моего SQL-источника данных
Любая помощь была бы принята с благодарностью.
Вот мой 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;
}
}
}
Да, я использую Identity. Я все еще получаю сообщение об ошибке «Нет данных». Первоначальный оператор Select был создан, когда я использовал «Create Data Source wizard». Я просто запустил это снова с помощью Select Statement. Когда я выбираю «Text Query», возвращаются только заголовки столбцов. У меня есть один зарегистрированный пользователь в таблице AspNetUser. –
@ joey.coyle Я не понимаю, что вы имеете в виду. Возвращает ли SQL любые записи при выполнении в базе данных? – Seany84
@ joey.coyle У вас есть записи в таблицах «AspNetRoles» или «AspNetUserRoles»? – Seany84