gridview InnerJoin查询,不使用数据源控件

时间:2016-02-18 15:06:36

标签: c# mysql gridview inner-join mysql-workbench

我正在尝试从MySQL工作台显示以下表格以在asp.net上显示

The inner join query work correctly in mysql

要在asp.net上显示表格,我使用网格视图,这里是以下代码:

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" OnRowCommand="GridView1_RowCommand" DataKeyNames ="child_id" OnSelectedIndexChanged="GridView1_SelectedIndexChanged" AllowPaging="True" OnPageIndexChanging="GridView1_PageIndexChanging">
        <Columns>
            <asp:TemplateField>
                <ItemTemplate>
                    <asp:LinkButton ID="LinkButton1" runat="server" CommandName="SelectFullDetails">Select</asp:LinkButton>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:BoundField DataField="child_id" HeaderText="Id" />
            <asp:BoundField DataField="child_firstname" HeaderText="FirstName" />
            <asp:BoundField DataField="parent_surname" HeaderText="Surname" />
            <asp:BoundField DataField="club_name" HeaderText="Club" />
        </Columns>
    </asp:GridView>



        protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            GridViewDataBind();
        }
    }

    private void GridViewDataBind()
    {
        GridView1.DataSource = DetailsAccessLayer.GetAllChildBasicDetails();
        GridView1.DataBind();
    }
 public class ChildBasic
{

    public int child_id { get; set; }
    public string child_firstname { get; set; }
    public string parent_surname { get; set; }
    public string club_name { get; set; }

}
 public class DetailsAccessLayer
{
    public static List<ChildBasic> GetAllChildBasicDetails()
    {
        List<ChildBasic> listChild = new List<ChildBasic>();
        string CS = ConfigurationManager.ConnectionStrings["dbyouthworkConnectionString"].ConnectionString;
        using (MySqlConnection con = new MySqlConnection(CS))
        {
            var InnerJoinClub = "SELECT child_details.child_id, child_details.child_firstname,child_details.parent_surname, enrolment_details.club_name FROM child_details INNER JOIN enrolment_details ON child_details.child_id = enrolment_details.child_id";
            MySqlCommand cmd = new MySqlCommand(innerJoinClub, con);
            con.Open();
            MySqlDataReader rdr = cmd.ExecuteReader();
            while (rdr.Read())
            {
                ChildBasic childbasic = new ChildBasic();
                childbasic.child_id = Convert.ToInt32(rdr["child_id"]);
                childbasic.child_firstname = rdr["child_firstname"].ToString();
                childbasic.parent_surname = rdr["parent_surname"].ToString();
                childbasic.club_name = rdr["club_name"].ToString();

                listChild.Add(childbasic);
            }
        }
        return listChild;
    }

然而,每次我运行程序时,都会收到错误:类型&#39; System.Web.HttpException&#39;发生在System.Web.dll中但未在用户代码中处理

其他信息:名称为&#39; club_name&#39;的字段或媒体资源未在所选数据源中找到。

有人能告诉我代码中的错误以及如何纠正错误吗?

非常感谢

This is the gridview

编辑:这是发生错误的地方

private void GridViewDataBind()
    {
        GridView1.DataSource = DetailsAccessLayer.GetAllChildBasicDetails();
        GridView1.DataBind();        }

GridView1.DataBind()

2 个答案:

答案 0 :(得分:0)

在SQL查询中,您需要为enrolment_details.club_name提供别名,例如enrolment_details.club_name AS club_name

答案 1 :(得分:0)

尝试这样:

public DataTable ConvertToDatatable(List<ChildBasic> list)
{
    DataTable dt = new DataTable();

    dt.Columns.Add("child_id");
    dt.Columns.Add("child_firstname");
    dt.Columns.Add("parent_surname");
    dt.Columns.Add("club_name");
    foreach (var item in list)
    {
        var row = dt.NewRow();

        row["child_id"] = item.child_id;
        row["child_firstname"] = Convert.ToString(item.child_firstname);
        row["parent_surname"] = Convert.ToString(item.parent_surname);
        row["club_name"] = Convert.ToString(item.parent_surname);

        dt.Rows.Add(row);
    }

    return dt;
}

private void GridViewDataBind()
    {
        GridView1.DataSource = ConvertToDatatable(DetailsAccessLayer.GetAllChildBasicDetails());
        GridView1.DataBind();        
    }