'How can I Read data of specific column from an ASP.NET Grid View and make a session to pass it to another page?

I'm trying to create a session based on a column field of an ASP.NET GridView and pass it to another page. Is there a way to do that?

This is my code:

 Private Sub BindGrid()


    Dim constr As String = ConfigurationManager.ConnectionStrings("dbconnection").ConnectionString
    Using con As New SqlConnection(constr)
        Using cmd As New SqlCommand()
            cmd.CommandText = "select  distinct Name,assignment_Id , Description from assignment 
        INNER Join crmsLecturerXCrsSection ON crmsLecturerXCrsSection.emp_key = assignment.emp_key
        INNER Join CRMSStudentsXCrsSection ON CRMSStudentsXCrsSection.CrsSec_id = crmsLecturerXCrsSection.CrsSec_id
        INNER JOIN CRMSCOURSExSECTION ON CRMSCOURSExSECTION.CrsSec_id = CRMSStudentsXCrsSection.CrsSec_id
        INNER JOIN CRMSECTIONS ON CRMSECTIONS.SEC_ID = CRMSCOURSExSECTION.SEC_ID
        INNER JOIN CRMSCOURSExSECTION cs ON CRMSCOURSExSECTION.SEC_ID = CRMSECTIONS.SEC_ID
        INNER JOIN CRMSSEMESTER ON CRMSSEMESTER.SEM_ID = CRMSCOURSExSECTION.SEM_ID
        where  CRMSSEMESTER.SEM_ID='1'   
        and crmsLecturerXCrsSection.emp_key='436' "

           
            cmd.Connection = con
            con.Open()
            GridView1.DataSource = cmd.ExecuteReader()
            GridView1.DataBind()



            Session("aid") = cmd.ExecuteReader.GetValue("assignment_Id")

          
            con.Close()
        End Using


Solution 1:[1]

It is not at all clear which row and THEN column value you want here? It would seem strange to pass "columns", but selecting a particular row? Sure, that is common.

So, say we have a GV, like this:

        <asp:GridView ID="GHotels" runat="server" CssClass="table"
            width="50%" DataKeyNames="ID" >
            <Columns>
                <asp:TemplateField HeaderText = "View" ItemStyle-HorizontalAlign ="Center" >
                    <ItemTemplate>
                    <asp:Button ID="cmdRow" runat="server" Text="View" 
                       CssClass="btn" 
                        OnClick="cmdRow_Click"
                     />
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
        </asp:GridView>

Note how I dropped in a plane jane asp.net button.

to load our data, we have this:

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

    void LoadData()
    {
        using (SqlConnection conn = new SqlConnection(Properties.Settings.Default.TEST4))
        {
            string strSQL = 
                "SELECT TOP 6 ID, FirstName, LastName, HotelName, Description FROM tblHotels " +
                "WHERE Description is not null ORDER BY HotelName";
            using (SqlCommand cmdSQL = new SqlCommand(strSQL, conn))
            {
                DataTable rstData = new DataTable();
                conn.Open();
                rstData.Load(cmdSQL.ExecuteReader());
                GHotels.DataSource = rstData;
                GHotels.DataBind();
            }
        }
    }

And we now have this:

enter image description here

So, lets add a click event to that button like this:

(type into the markup OnClick= (we do it this way, since I can't just click on a button inside the GV to create a click event).

enter image description here

and now in code behind, we can get the data row we clicked on, and the data like this:

    protected void cmdRow_Click(object sender, EventArgs e)
    {
        Button btn = sender as Button;
        GridViewRow gRow = btn.NamingContainer as GridViewRow;

        Debug.Print("Row index click = " + gRow.RowIndex);
        Debug.Print("ID (data keys) = " + GHotels.DataKeys[gRow.RowIndex]["ID"].ToString());
        Debug.Print("FirstName = " + gRow.Cells[1].Text);
        Debug.Print("Last Name = " + gRow.Cells[2].Text);
    }

Output:

Row index click = 5
ID (data keys) = 70
FirstName = David
Last Name = Super

In most cases, I tend to get some PK or row ID, and pass that to the next page. The next page thus can load that one row of data based on say a PK row id.

so I would pass the PK "ID" from this row, and then ONLY place that one value into session, and jump to the next page. It would (could) then pull the data row based on that PK value. I mean, you could shove multiple values from the cells[] collection into session(), but in most cases, I would pass JUST the PK id, since then that page can be used + called from other places in the side, and only one session() value would be required (some kind of PK row value).

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1