hello, I 'm still a noob at programming in vb.net

I have a problem. I 'm trying to get my selected rows values to another page. Basically, there two values that not being displayed in the gridview, but is part of the selected statement in the store procedure that populated the gridview. I need to pass these values to another page which then use them to call a store procedure, but I don't know how I can do it I would like to pass them through a session to another page.


hopefully someone can help me.
Thank you in advance for your help.

Dani AI

Generated

Brief diagnosis and next steps based on the code you posted: the page reached Default2.aspx with an empty query string because the two local variables were never populated and the query string concatenation was malformed. was right to suggest DataKeyNames or Session, but when you have more than one DataKey you must read each key explicitly, and you must build the query string with the correct separators and URL-encode values.

Make sure the GridView has the keys in its DataKeyNames (these must match column names returned by your SELECT). When multiple keys are declared use the DataKeys(index).Values collection to get each value; it is not the same as the single-key DataKeys(index).Value. See the official docs for details: GridView.DataKeys and DataKey.Values.

Example pattern (VB.NET) for SelectedIndexChanged that avoids the problems in your post and URL-encodes values:

Protected Sub gvPatientsCharts_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) Handles gvPatientsCharts.SelectedIndexChanged
    Dim idx As Integer = gvPatientsCharts.SelectedIndex
    If idx < 0 OrElse idx >= gvPatientsCharts.DataKeys.Count Then Return

    Dim personId As String = Convert.ToString(gvPatientsCharts.DataKeys(idx).Values("person_id"))
    Dim practiceId As String = Convert.ToString(gvPatientsCharts.DataKeys(idx).Values("practice_id"))

    ' Option A: pass on querystring (encode values)
    Dim target As String = String.Format("Default2.aspx?person_id={0}&practice_id={1}", _
                    HttpUtility.UrlEncode(personId), HttpUtility.UrlEncode(practiceId))
    Response.Redirect(target)

    ' Option B: store in session and redirect (does not expose values in URL)
    ' HttpContext.Current.Session("person_id") = personId
    ' HttpContext.Current.Session("practice_id") = practiceId
    ' Response.Redirect("Default2.aspx")
End Sub

On Default2.aspx read either Request.QueryString or Session and validate before calling your stored procedure. Use HttpUtility.UrlEncode when building query strings to avoid malformed URLs: HttpUtility.UrlEncode.

Quick troubleshooting: confirm DataKeyNames exactly match returned field names, ensure the GridView row is actually selected (SelectedIndex >= 0), and inspect values in a debugger or write them temporarily to a label to verify they are present before redirect.

do you have your fields you want passed as datakeys in the grid?

No, but if it need to be then I can add them.

<asp:GridView ID="grdEmpList" runat="server" AllowPaging="true" PagerStyle-HorizontalAlign="center"
 DataSourceid="dSource"
   DataKeyNames="Field1,Field2.."
Private Sub grdEmpList_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles grdEmpList.SelectedIndexChanged
            Dim i As Integer
            'get row index
            i = grdEmpList.SelectedIndex
            Dim strField1 As String
            RecId = grdEmpList.DataKeys(i).Value
            Response.Redirect("DetailsPage.aspx?Field1=" & strField1..)

if you want it through session, i think you'd just do ..

Session("field1") = strfield1

Thank you Oreo1982. I'll try it.

Oreo1982 or any can help me.

I am not able to pass the two values to the DetailsPage. The page loaded but no data.
"http://localhost:2182/WebSite1/Default2.aspx?person_id=practice_id%="
Below is my codes:

Dim i As Integer
Dim RecId
'get row index
i = gvPatientsCharts.SelectedIndex
Dim strperson_id, strpractice_id As String
RecId = gvPatientsCharts.DataKeys(i).Value
Response.Redirect("Default2.aspx?person_id=" & strperson_id & "practice_id =" & strpractice_id)


Thanks

Dim strperson_id, strpractice_id As String
RecId = gvPatientsCharts.DataKeys(i).Value
Response.Redirect("Default2.aspx?person_id=" & strperson_id & "practice_id =" & strpractice_id)


Thanks

try taking the space out from behind practice_id. & "practice_id=" &

try taking the space out from behind practice_id. & "practice_id=" &

Also... you need an ampersand in the string between variables.


"detailspage.aspx?var1=" & value & "&practice_id=" & value2

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.