Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Grid View ASP Net connecting to a web service

Posted on 2006-05-29
12
Medium Priority
?
4,338 Views
Last Modified: 2012-06-27
Hi,

I am binding a grid view to a web service.  The data displays fine, however, I am trying to pull out a column value when the user clicks on the select button within the grid.  I am using the following code:


    Protected Sub GridView1_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles GridView1.RowCommand

        MsgBox(Me.GridView1.SelectedRow.Cells(1).Text)

    End Sub

I recieve a nullreferenceexception error, it as if the grid has not been populated.  IF I connect straight to a SQL server it works fine, so it must be something to do with the way I am connecting to the web service:

        Me.GridView1.DataSource = ConsumeWebService.SearchForEstablishment(Username, Password, Session("SearchEstablishmentName").ToString, "", "", "", "", "", "", "", "", EdacID)
            Me.GridView1.DataBind()
            Me.GridView1.EnableViewState = True
            ConsumeWebService.Dispose()

Is there a property on something I need to set on the grid view?

thanks
0
Comment
Question by:kinton
  • 6
  • 5
12 Comments
 
LVL 1

Expert Comment

by:sguerra
ID: 16785132
Maybe what you need is to check, which is the command the user in clicking on, some thing like this:

Private Sub dgUsers_ItemCommand(ByVal source As System.Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles dgUsers.ItemCommand
   If e.CommandName = "select" Then
     'put your code
   End If
End Sub
0
 
LVL 1

Expert Comment

by:sguerra
ID: 16785154
The code above works if your column has the "Command name" set to "select"

And also if you want the key of the selected row you can use:

dgUsers.SelectedIndex = e.Item.ItemIndex
msgbox(dgUsers.DataKeys(e.Item.ItemIndex))

Hope it helps!
0
 
LVL 4

Expert Comment

by:Ariful Ambia
ID: 16785177
Try with it

Dim Dt As DataTable = GridView1.DataSource
MsgBox(Dt.Rows(GridView1.CurrentRowIndex).Item("Colum_name"))
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 2

Author Comment

by:kinton
ID: 16785528
No luck I'm afraid!  Item is not a recognised word?
0
 
LVL 1

Expert Comment

by:sguerra
ID: 16785641
You mean that Item is not a regnised word of the "e as System.Web.UI.WebControls.DataGridCommandEventArgs" variable of the sub ?
0
 
LVL 2

Author Comment

by:kinton
ID: 16785746
I do, sorry, poor use of language!
0
 
LVL 1

Expert Comment

by:sguerra
ID: 16785782
That may be because you are using the sub "GridView1_RowCommand" of the grid.
Try usign the ItemCommand sub of the grid, this will be executed when you click on any of the Command Button columns agregated to your grid in the Property Builder window (right click on your grid) , in there you can set the "Command name" property I mentioned before.

Doing this the ItemCommand sub will be launched and then you could use the code I placed above

Santiago G.
0
 
LVL 2

Author Comment

by:kinton
ID: 16785939
I can't see the ItemCommand sub on from the grid.  Am I missing something extremely obvious!?
0
 
LVL 1

Accepted Solution

by:
sguerra earned 2000 total points
ID: 16786322
Ok, Sorry about the comments before, I did not realized that you were using framework 2.0, but I got the solution for you which should work, I tried myself

Protected Sub GridView1_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles GridView1.RowCommand
   If e.CommandName = "Select" Then
            Me.GridView1.SelectedIndex = e.CommandArgument
            MsgBox("ID=" + CStr(Me.GridView1.SelectedValue))
   End If
End Sub

In the e.CommandArgument variable you get the index number of the row selected, then I put that value in the SelectedIndex property of the ViewGrid, only then you can use the SelectedValue property or even your choice of code "Me.GridView1.SelectedRow.Cells(1).Text", both work.

Santiago G.
0
 
LVL 2

Author Comment

by:kinton
ID: 16788204
thanks for your continued help.

I now receive

Data keys must be specified on GridView 'GridView1' before the selected data keys can be retrieved.  Use the DataKeyNames property to specify data keys

I'm going to have a hunt online in the mean time - see If I can discover it my self!

0
 
LVL 2

Author Comment

by:kinton
ID: 16788343
ok - I've solved it.  I needed to add the datakeyname into my code, then youur solution works a treat.

Just in case any one else reads this..

Pop this bit of code into the header value for your grid

<asp:GridView ID="GridView1" DataKeyNames="DATAKEYNAME" runat="server" AutoGenerateColumns="False" Width="424px">

Thank you so much for your help, I am truly grateful!
0
 
LVL 1

Expert Comment

by:sguerra
ID: 16789609
Your very welcome
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
Six Sigma Control Plans
Screencast - Getting to Know the Pipeline

810 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