Consume Webservice question

K

kai

Hi, All
I have following Webservice code rom a book, it works well, it returns a
dataset contains customerOrderHis base on CustomerID. But I cannot display
the returning dataset in datagrid.



<WebMethod(Description:="Get an instance of the custom class " & _

"CustomerAndOrderHistoryInfo, which has a field containing a typed " & _

"DataSet of products that the customer has ordered and a field " & _

"for the company name.")> _

Public Function GetCustomerOrderHistory(ByVal custID As String) _

As CustomerAndOrderHistoryInfo

Dim IsConnecting As Boolean = True

While IsConnecting



Dim scnnNW As New SqlConnection(conn)





Dim scmd As New SqlCommand("CustOrderHist", scnnNW)

Dim cohi As New CustomerAndOrderHistoryInfo()



Try

scmd.CommandType = CommandType.StoredProcedure

scmd.Parameters.Add(New SqlParameter("@CustomerID", _

SqlDbType.NChar, 5)).Value = custID



Dim sda As New SqlDataAdapter(scmd)



Dim dsOrderHistory As New dsCustOrderHist()



sda.Fill(dsOrderHistory.CustOrderHist)

..

IsConnecting = False





cohi.Orders = dsOrderHistory





scmd.CommandText = _

"SELECT CompanyName " & _

"FROM Customers " & _

"WHERE CustomerID = @CustomerID"





scmd.CommandType = CommandType.Text



scnnNW.Open()

Dim objReturnVal As Object = scmd.ExecuteScalar()

If Not IsDBNull(objReturnVal) Then

cohi.CompanyName = objReturnVal.ToString

End If



Catch exSql As SqlException

Throw New Exception(exSql.Message)



Catch ex As Exception

If conn = SQL_CONNECTION_STRING Then



conn = MSDE_CONNECTION_STRING

Else



Throw New Exception(ex.Message)

End If

Finally

scnnNW.Close()

End Try



Return cohi

End While

End Function



I use the following code to consume:

Private Sub btnRetrive_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles btnRetrive.Click

Dim ds As DataSet

Dim ds1 As DataSet

Dim CustomerID As String

Dim svc As New Expensive.Main

ds = svc.GetCustomerOrderHistory("ALFKI")

DataGrid2.DataSource = ds

DataGrid2.DataBind()

End Sub


What is my problem?

Thanks

kai
 
P

Peter Huang

Hi Kai,

Here is my test code which will work.
[Comsumer]
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Dim ds As DataSet
Dim ws As New localhost.Service1
ds = ws.GetCustomerOrderHistory("Test")
DataGrid1.DataSource = ds
'DataGrid1.DataMember = "Customers"
DataGrid1.DataBind()
'Put user code to initialize the page here
End Sub

[WebService]
<WebMethod()> _
Public Function GetCustomerOrderHistory(ByVal custID As String) As
DataSet
SqlDataAdapter1.Fill(DataSet11)
Return DataSet11
End Function

You may have a try and let me know the result.
From your post,
Public Function GetCustomerOrderHistory(ByVal custID As String) _

As CustomerAndOrderHistoryInfo

What is the CustomerAndOrderHistoryInfo, is it a dataset?

To isolate the problem ,please test my code , I will appreciate your effort.

You may also debug into the consumer app to see if the ds has returned
correct.

ds = svc.GetCustomerOrderHistory("ALFKI")
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Check the line above in the debugger.

Here is a link you may take a look
Consuming a DataSet from an XML Web Service

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/htm
l/cpconconsumingdatasetfromwebservice.asp

Regards,
Peter Huang
Microsoft Online Partner Support
Get Secure! www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
K

kai

Hi, Peter
Your code works like magic, thank you so much. Now I get return dataset in
a datagrid.

Kai
Peter Huang said:
Hi Kai,

Here is my test code which will work.
[Comsumer]
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Dim ds As DataSet
Dim ws As New localhost.Service1
ds = ws.GetCustomerOrderHistory("Test")
DataGrid1.DataSource = ds
'DataGrid1.DataMember = "Customers"
DataGrid1.DataBind()
'Put user code to initialize the page here
End Sub

[WebService]
<WebMethod()> _
Public Function GetCustomerOrderHistory(ByVal custID As String) As
DataSet
SqlDataAdapter1.Fill(DataSet11)
Return DataSet11
End Function

You may have a try and let me know the result.
From your post,
Public Function GetCustomerOrderHistory(ByVal custID As String) _

As CustomerAndOrderHistoryInfo

What is the CustomerAndOrderHistoryInfo, is it a dataset?

To isolate the problem ,please test my code , I will appreciate your effort.

You may also debug into the consumer app to see if the ds has returned
correct.

ds = svc.GetCustomerOrderHistory("ALFKI")
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Check the line above in the debugger.

Here is a link you may take a look
Consuming a DataSet from an XML Web Service

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/htm
l/cpconconsumingdatasetfromwebservice.asp

Regards,
Peter Huang
Microsoft Online Partner Support
Get Secure! www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top