Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Gridview - select

Posted on 2006-10-19
20
Medium Priority
?
429 Views
Last Modified: 2008-01-09
I have a column (buttons "Select") I want to click on a button and disply more info about a customer on a second page from a different table. I have set DatakeyNames on  Gridview  properties to CustomerID. What else do I need to do?
0
Comment
Question by:VBdotnet2005
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
20 Comments
 
LVL 9

Expert Comment

by:peterdungan
ID: 17772574
open the second page with post parameters of the foreign key from the selected record
0
 

Author Comment

by:VBdotnet2005
ID: 17774520
I'm not so sure how to do that. Could you help?
0
 
LVL 10

Expert Comment

by:jnhorst
ID: 17777518
Your data grid should have an ItemCommand event that fires when you click the button.  The event handler will have an argument (the variable is "e") from which you will be able to get info about the ID.  You should have the CustomerID as a non-visible field in your grid (maybe make it the very first column).  In the ItemCommand event, do this:

// this assumes the customer id is the first column, but can be made not visible.
string custID = e.Item.Cells[0].Text;

// redirect the browser to the other page.
string url = String.Concat("OtherPage.aspx?CustID=", custID);
Response.Redirect(url);

This will cause the other page to show with the customer id in the querystring (the part of the url after the question mark).

Then in OtherPage.aspx, you can code the Page_Load event to get this value like this:

// get the id...
int custID = Convert.ToInt32(Request.QueryString["CustID"]);

Then use this to query your database for that customer's info and show on the page.

John
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

Author Comment

by:VBdotnet2005
ID: 17778857
I'm using VB. The equavalent of ItemCommand in GridView is RowCommand. However, I don't see "item" here.
   
Protected Sub GridView1_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles GridView1.RowCommand

        Dim custID As String = e.item.Cells(0).Text             ----------------------------------------error item
        Dim url As String = [String].Concat("OtherPage.aspx?CustID=", custID)
        Response.Redirect(url)

    End Sub
0
 
LVL 29

Accepted Solution

by:
Nightman earned 2000 total points
ID: 17781935
Try this (derived from the documentation at http://msdn2.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.rowcommand.aspx)

Protected Sub GridView1_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles GridView1.RowCommand
  Dim index As Integer = Convert.ToInt32(e.CommandArgument)
  Dim row As GridViewRow = CustomersGridView.Rows(index)
  Dim custID As String = row.Cells(0).Text
  Dim url As String = [String].Concat("OtherPage.aspx?CustID=", custID)
  Response.Redirect(url)
End Sub
0
 

Author Comment

by:VBdotnet2005
ID: 17782720

OtherPage.aspx

I get this error - Input string was not in a correct format.


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


        Dim custID As Integer = Convert.ToInt32(Request.QueryString("CustID"))   ------->>>>Input string was not in a correct format.

        If Not IsPostBack Then
            Try

                Dim Constr As String = " mystring"
                Dim con As New SqlConnection(Constr)
                Dim Comstr As String = "select *from residence where id = CustID"
                Dim da As New SqlDataAdapter(Comstr, con)
                Dim ds As New DataSet

                da.Fill(ds, "table1")



                Me.GridView1.DataSource = ds
                Me.GridView1.DataMember = "table1"
                Me.GridView1.DataBind()



            Catch ex As Exception

                Response.Write(ex.ToString)


            End Try


        End If


    End Sub

0
 
LVL 29

Expert Comment

by:Nightman
ID: 17782844
What is customerID? Text?
0
 

Author Comment

by:VBdotnet2005
ID: 17782860
It is number.
0
 

Author Comment

by:VBdotnet2005
ID: 17782873
System.InvalidCastException: Conversion from string "" to type 'Integer' is not valid. ---> System.FormatException: Input string was not in a correct format. at Microsoft.VisualBasic.CompilerServices.Conversions.ParseDouble(String Value, NumberFormatInfo NumberFormat) at Microsoft.VisualBasic.
0
 
LVL 29

Expert Comment

by:Nightman
ID: 17782883
No, it's an empty string. Conversion from string "" to Integer is not valid. The value is not being passed to that page.
0
 

Author Comment

by:VBdotnet2005
ID: 17782888
I set a property "DatakeyNames"  to CustomerID         ------this is on  Gridview1
0
 

Author Comment

by:VBdotnet2005
ID: 17782892
How can I correct it then?
0
 
LVL 29

Expert Comment

by:Nightman
ID: 17782893
The code that I helped you with takes the text  value of the Cell(0) of the grid and passes it through. What is this value?
0
 

Author Comment

by:VBdotnet2005
ID: 17782904
It is empty  -  ""
0
 
LVL 29

Expert Comment

by:Nightman
ID: 17782911
If that is the correct value that you are trying to pass through, and it's empty, look into why.
Is the data correct?
Is this a null value coming back from the database?
Should you be trying to pass a different cell (e.g. cell(1) or cell(2))?
Is there another property that you should be passing (e.g. DatakeyNames)?
0
 

Author Comment

by:VBdotnet2005
ID: 17782944
I changed this
Dim ID As String = row.Cells(0).Text

to

Dim ID As String = row.Cells(1).Text

On Otherpage.aspx - I get the same table from my first page
It does display the record the selected on the address bar  - http://localhost/Sample/OtherPage.aspx?ID=6

Why I won't display just the record I select on Gridview?

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


        Dim custID As Integer = Convert.ToInt32(Request.QueryString("CustID"))  
        If Not IsPostBack Then
            Try

                Dim Constr As String = " mystring"
                Dim con As New SqlConnection(Constr)
                Dim Comstr As String = "select *from residence where id = CustID"
                Dim da As New SqlDataAdapter(Comstr, con)
                Dim ds As New DataSet

                da.Fill(ds, "table1")



                Me.GridView1.DataSource = ds
                Me.GridView1.DataMember = "table1"
                Me.GridView1.DataBind()



            Catch ex As Exception

                Response.Write(ex.ToString)


            End Try


        End If


    End Sub

0
 

Author Comment

by:VBdotnet2005
ID: 17782948
correction   - Dim Comstr As String = "select *from residence where id = ID"
0
 

Author Comment

by:VBdotnet2005
ID: 17782955

I corrected this line -   Dim Comstr As String = "select *from residence where id = ID"

to

  Dim Comstr As String = "select *from residence where id =" & ID

I finally works...wow

Goodnight, Nightman - Thank you so much for the help
0
 
LVL 29

Expert Comment

by:Nightman
ID: 17782966
Hehe, good morning you mean (9.30 am here in sunny South Africa).

Cheers
Night
0
 
LVL 29

Expert Comment

by:Nightman
ID: 17783412
Don't forget to PAQ this question. Also, don't forget to delete the pointer question (http://www.experts-exchange.com/Programming/Programming_Languages/Dot_Net/ASP_DOT_NET/Q_22033181.html) - I deliberately didn't post anything there so that you can delete it quickly and get the 50 points refunded.
0

Featured Post

[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

One of the pain points with developing AJAX, JavaScript, JQuery, and other client-side behaviors is that JavaScript doesn’t allow for cross domain request for pulling content. For example, JavaScript code on www.johnchapman.name could not pull conte…
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
Suggested Courses

609 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question