print only one selected value of a list from an asp.net grid view

mush 181 Reputation points
2022-03-14T04:03:18.663+00:00

I'm trying to create a session for a selected id from a grid view. the problem is I keep getting all the values but i want only on selected value to be passed. any idea how to do that?

this is the first page code:

Private Sub BindGrid()
 Dim con As New SqlConnection(ConfigurationManager.ConnectionStrings("dbconnection").ConnectionString)
 con.Open()
 Dim cmd As New SqlCommand("select distinct Name,assignment_id, Description ,cod from assignments
 INNER Join crmsLecturerXCrsSection ON crmsLecturerXCrsSection.emp_key = assignments.emp_key
 INNER Join CRMSStudentsXCrsSection ON CRMSStudentsXCrsSection.CrsSec_id = crmsLecturerXCrsSection.CrsSec_id
 INNER JOIN CRMS_CourseXSection ON CRMS_CourseXSection.CrsSec_id = CRMSStudentsXCrsSection.CrsSec_id
 INNER JOIN CRMSECTIONS ON CRMSECTIONS.SEC_ID = CRMS_CourseXSection.SEC_ID
 left JOIN crmscourses ON crmscourses.crs_id = CRMS_CourseXSection.crs_id
 INNER JOIN CRMS_CourseXSection cs ON CRMS_CourseXSection.SEC_ID = CRMSECTIONS.SEC_ID
 INNER JOIN CRMSSEMESTER ON CRMSSEMESTER.SEM_ID = CRMS_CourseXSection.SEM_ID
 where  CRMSSEMESTER.SEM_ID='1'
 and crmsLecturerXCrsSection.emp_key='436' and crmscourses.crs_desc='" + Session("crs") + "'", con)

 Dim da As New SqlDataAdapter(cmd)
 Dim dt As New DataTable()
 da.Fill(dt)


 Dim listIDs As New List(Of String)
 Dim row As DataRow
 For Each row In dt.Rows
 listIDs.Add(row("assignment_id").ToString())
 Next
 Session("assid") = listIDs

this is the second page where i try to display the value in a label:

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        Dim assignments = TryCast(CObj(Session("assid")), List(Of String))
        Label4.Text = (assignments)


    End Sub
ASP.NET
ASP.NET
A set of technologies in the .NET Framework for building web applications and XML web services.
3,417 questions
SQL Server
SQL Server
A family of Microsoft relational database management and analysis systems for e-commerce, line-of-business, and data warehousing solutions.
13,361 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,648 questions
{count} votes

Accepted answer
  1. Albert Kallal 5,231 Reputation points
    2022-03-16T19:29:52.883+00:00

    Ok, so the "real" issue is we need to click on a row - but for now, lets just drop in a button to the GV, and use that.

    Also, as a general rule, you don't need (or want) to expose PK id values in the grid. (it is a security issue, and those numbers tend to mean nothing to the user.

    Thankfully, the elfs and wizards in Redmond cooked up a feature for this purpose (Datakeys of the GV).

    So, in the GV, we can drop in a plane jane asp.net button. Say like this:

    183872-image.png

    And our code to load, looks like this:

        Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load  
            If Not IsPostBack Then  
                LoadGrid()  
            End If  
        End Sub  
      
        Sub LoadGrid()  
      
            Using conn As New SqlConnection(My.Settings.TEST4)  
      
                Dim strSQL As String =  
                    "SELECT ID,Fighter, Engine, Thrust, Description, FirstFlight, ImagePath from Fighters"  
      
                Using cmdSQL As New SqlCommand(strSQL, conn)  
                    conn.Open()  
                    Dim rstData As New DataTable  
                    rstData.Load(cmdSQL.ExecuteReader)  
                    GridView1.DataSource = rstData  
                    GridView1.DataBind()  
                End Using  
            End Using  
      
        End Sub  
    

    Ok, so now we see this:

    183827-image.png

    So, we dropped in that plane jane button, and that will become our row select, and the code looks like this:

       Protected Sub cmdView_Click(sender As Object, e As EventArgs)  
      
            ' get the row   
            Dim btn As Button = sender  
            Dim gRow As GridViewRow = btn.NamingContainer  
      
            Dim iRowIndex As Integer = gRow.RowIndex  
            Dim iPK As Integer = GridView1.DataKeys(iRowIndex).Item("ID")  
      
            ' OK, we have both row index (don't think we need it)  
            ' and we have database PK row ID.  
      
            'lets now jump to a new page to display more information about this row we   
            ' clicked on  
      
            Session("RowID") = iPK  
      
            Response.Redirect("ShowDetails.aspx")  
      
        End Sub  
      
    

    So, once we have that "row id", we can quite much do what we want. So, on the next page, the code to load, get use of that full one database row could be:

       Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load  
      
      
            If Not IsPostBack Then  
      
                ' code here to load the ONE row.  
      
                Dim strSQL As String = "SELECT * from Fighers where id = " & Session("RowID")  
      
                Dim rstData As New DataTable  
                Using conn As New SqlConnection(My.Settings.TEST4)  
                    Using cmdSQL As New SqlCommand(strSQL, conn)  
                        conn.Open()  
                        rstData.Load(cmdSQL.ExecuteReader)  
                    End Using  
                End Using  
      
            End If  
      
        End Sub  
      
    

    Note that you might not need nor want the "whole" row, and you can certain in that row click event code, grab any collum value from gRow, say like this:

     dim strFigher as string = gRow.Cells(0).Text  
    

    So, you can pass in session one value from the gRow, or as noted, pass the "pk" row id, and then in the next web page you jump to, pull the whole database row again in that next page.

    Regards,
    Albert D. Kallal (Access MVP 2003-2017)
    Edmonton, Alberta Canada


0 additional answers

Sort by: Most helpful