locked
GridView update RRS feed

  • Question

  • User-1164293554 posted

    Hello friends,

    i have 2 sql tables, table 1 and table 2, and i need to select data from table 1 witch has id, meetID, and so on, with a unique value for meetID, single row, o my first .aspx page, and also when i click select o this row, to redirect on a second page fiiling data from both tables with that unique meetID, but table 2 has multiple rows, and so, i need both tables joined with that single row from table1 and multiple rows from table2 selected by meetID (same value for both), in a gridview or something, on the second page .aspx.

    Sorry for bad details, please help me with some short example, code!

    Thursday, March 18, 2021 2:00 PM

Answers

  • User-939850651 posted

    Hi Cristian0910,

    It seems that you want to achieve a function similar to viewing information. If this is the case, you could try to use query string in Url.

    This is my tes:

    Page1: 
    
    <form id="form1" runat="server">
            <div>
                <asp:GridView runat="server" ID="GV1" AutoGenerateColumns="false">
                    <Columns>
                        <asp:TemplateField HeaderText="ID">
                            <ItemTemplate>
                                <asp:Label Text='<%#Eval("ID") %>' runat="server" />
                            </ItemTemplate>
                        </asp:TemplateField>
                        <asp:TemplateField HeaderText="meetID">
                            <ItemTemplate>
                                <asp:Label Text='<%#Eval("meetID") %>' runat="server" />
                            </ItemTemplate>
                        </asp:TemplateField>
                        <asp:TemplateField HeaderText="Table1 Column1">
                            <ItemTemplate>
                                <asp:Label Text='<%#Eval("col1") %>' runat="server" />
                            </ItemTemplate>
                        </asp:TemplateField>
                        <asp:TemplateField HeaderText="Table1 Column1">
                            <ItemTemplate>
                                <a href='DetailFromTable1.aspx?meetID=<%# Eval("meetID")%>'>View Details</a>
                            </ItemTemplate>
                        </asp:TemplateField>
                    </Columns>
                </asp:GridView>
            </div>
        </form>
    CodeBehind: string conStr = ConfigurationManager.ConnectionStrings["conStr"].ToString(); protected void Page_Load(object sender, EventArgs e) { if (!Page.IsPostBack) { fillGridView(); } } private void fillGridView() { DataTable dt = new DataTable(); using (SqlConnection conn = new SqlConnection(conStr)) { string query = "select * from table1"; using (SqlCommand cmd = new SqlCommand(query, conn)) { SqlDataAdapter sda = new SqlDataAdapter(cmd); sda.Fill(dt); } } GV1.DataSource = dt; GV1.DataBind(); }
    Page2:
    
    <form id="form1" runat="server">
            <div>
                <asp:GridView runat="server" ID="GV1" AutoGenerateColumns="false">
                    <Columns>
                        <asp:TemplateField HeaderText="meetID">
                            <ItemTemplate>
                                <asp:Label Text='<%#Eval("meetID") %>' runat="server" />
                            </ItemTemplate>
                        </asp:TemplateField>
                        <asp:TemplateField HeaderText="Table2 Column1">
                            <ItemTemplate>
                                <asp:Label Text='<%#Eval("column1") %>' runat="server" />
                            </ItemTemplate>
                        </asp:TemplateField>
                        <asp:TemplateField HeaderText="Table1 Column2">
                            <ItemTemplate>
                                <asp:Label Text='<%#Eval("column2") %>' runat="server" />
                            </ItemTemplate>
                        </asp:TemplateField>
                    </Columns>
                </asp:GridView>
            </div>
        </form>
    
    CodeBehind:
    string conStr = ConfigurationManager.ConnectionStrings["conStr"].ToString();
            protected void Page_Load(object sender, EventArgs e)
            {
                if (!Page.IsPostBack)
                {
                    int meetID = int.Parse(Request.QueryString["meetID"]);
                    fillGridView(meetID);
                }
            }
    
            private void fillGridView(int meetID)
            {
                DataTable dt = new DataTable();
                using (SqlConnection conn = new SqlConnection(conStr))
                {
                    string query = "select * from table2 where meetID=@meetID";
                    using (SqlCommand cmd = new SqlCommand(query, conn))
                    {
                        cmd.Parameters.AddWithValue("@meetID", meetID);
                        SqlDataAdapter sda = new SqlDataAdapter(cmd);
                        sda.Fill(dt);
                    }
                }
                GV1.DataSource = dt;
                GV1.DataBind();
            }

    Result:

    If I misunderstood something, could you clearly describe your current problem?

    Best regards,

    Xudong Peng

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, March 19, 2021 4:12 AM

All replies

  • User-939850651 posted

    Hi Cristian0910,

    It seems that you want to achieve a function similar to viewing information. If this is the case, you could try to use query string in Url.

    This is my tes:

    Page1: 
    
    <form id="form1" runat="server">
            <div>
                <asp:GridView runat="server" ID="GV1" AutoGenerateColumns="false">
                    <Columns>
                        <asp:TemplateField HeaderText="ID">
                            <ItemTemplate>
                                <asp:Label Text='<%#Eval("ID") %>' runat="server" />
                            </ItemTemplate>
                        </asp:TemplateField>
                        <asp:TemplateField HeaderText="meetID">
                            <ItemTemplate>
                                <asp:Label Text='<%#Eval("meetID") %>' runat="server" />
                            </ItemTemplate>
                        </asp:TemplateField>
                        <asp:TemplateField HeaderText="Table1 Column1">
                            <ItemTemplate>
                                <asp:Label Text='<%#Eval("col1") %>' runat="server" />
                            </ItemTemplate>
                        </asp:TemplateField>
                        <asp:TemplateField HeaderText="Table1 Column1">
                            <ItemTemplate>
                                <a href='DetailFromTable1.aspx?meetID=<%# Eval("meetID")%>'>View Details</a>
                            </ItemTemplate>
                        </asp:TemplateField>
                    </Columns>
                </asp:GridView>
            </div>
        </form>
    CodeBehind: string conStr = ConfigurationManager.ConnectionStrings["conStr"].ToString(); protected void Page_Load(object sender, EventArgs e) { if (!Page.IsPostBack) { fillGridView(); } } private void fillGridView() { DataTable dt = new DataTable(); using (SqlConnection conn = new SqlConnection(conStr)) { string query = "select * from table1"; using (SqlCommand cmd = new SqlCommand(query, conn)) { SqlDataAdapter sda = new SqlDataAdapter(cmd); sda.Fill(dt); } } GV1.DataSource = dt; GV1.DataBind(); }
    Page2:
    
    <form id="form1" runat="server">
            <div>
                <asp:GridView runat="server" ID="GV1" AutoGenerateColumns="false">
                    <Columns>
                        <asp:TemplateField HeaderText="meetID">
                            <ItemTemplate>
                                <asp:Label Text='<%#Eval("meetID") %>' runat="server" />
                            </ItemTemplate>
                        </asp:TemplateField>
                        <asp:TemplateField HeaderText="Table2 Column1">
                            <ItemTemplate>
                                <asp:Label Text='<%#Eval("column1") %>' runat="server" />
                            </ItemTemplate>
                        </asp:TemplateField>
                        <asp:TemplateField HeaderText="Table1 Column2">
                            <ItemTemplate>
                                <asp:Label Text='<%#Eval("column2") %>' runat="server" />
                            </ItemTemplate>
                        </asp:TemplateField>
                    </Columns>
                </asp:GridView>
            </div>
        </form>
    
    CodeBehind:
    string conStr = ConfigurationManager.ConnectionStrings["conStr"].ToString();
            protected void Page_Load(object sender, EventArgs e)
            {
                if (!Page.IsPostBack)
                {
                    int meetID = int.Parse(Request.QueryString["meetID"]);
                    fillGridView(meetID);
                }
            }
    
            private void fillGridView(int meetID)
            {
                DataTable dt = new DataTable();
                using (SqlConnection conn = new SqlConnection(conStr))
                {
                    string query = "select * from table2 where meetID=@meetID";
                    using (SqlCommand cmd = new SqlCommand(query, conn))
                    {
                        cmd.Parameters.AddWithValue("@meetID", meetID);
                        SqlDataAdapter sda = new SqlDataAdapter(cmd);
                        sda.Fill(dt);
                    }
                }
                GV1.DataSource = dt;
                GV1.DataBind();
            }

    Result:

    If I misunderstood something, could you clearly describe your current problem?

    Best regards,

    Xudong Peng

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, March 19, 2021 4:12 AM
  • User-1164293554 posted

    Thank you so much, you saved me.

    It works fine!

    Friday, March 19, 2021 6:30 AM