question

mush-9787 avatar image
0 Votes"
mush-9787 asked mush-9787 commented

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

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






sql-server-generaldotnet-csharpdotnet-aspnet-general
· 4
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi @mush-9787 ,
What's your meanings? Do you want to pass one columns's values from one page to another page?
Best regards,
Yijing Sun

0 Votes 0 ·

yes but not all the values. just one selected value from a grid view. for example if I have id 7 , id 6, id 4 and I clicked id 6 from a grid view . I want only that id to be passed to the other page

182682-assi.png



when I click the link in submit here column I want only that id to be passed.

0 Votes 0 ·
assi.png (16.8 KiB)

Hi @mush-9787 ,
Maybe I know what you want.I want the selected row's id and then pass to the other page. So,your problem is that you don't know how to get the selected id' value. You could create a linkbutton. In the event of the linkbutton , you could get the row index.Just like this:

  var rowIndex = ((GridViewRow)((Control)sender).NamingContainer).RowIndex;

Best regards,
Yijing Sun

1 Vote 1 ·
Show more comments

1 Answer

AlbertKallal-4360 avatar image
0 Votes"
AlbertKallal-4360 answered mush-9787 commented

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





image.png (67.1 KiB)
image.png (195.4 KiB)
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Thank you so much sir! I've tried it and it worked perfectly. thanks again.

0 Votes 0 ·