Web Service Calls for VB.Net managed code

S

Shawn

Can someone please provide an example of how to call a web service from
vb.net managed code that needs to have an input parameter and return an
ado.net recordset? I have a date field and on the 'OnAfterChange' event I
need to call a web service that will utilize that date to run a stored
procedure and pass back a recordset.

Thanks,
Shawn
 
S

Scott L. Heim [MSFT]

Hi Shawn,

Here are sample steps (using the Orders table from the SQL Server Northwind
database) to pass a date parameter to a stored procedure that subsequently
returns a dataset that can be consumed by InfoPath.

If possible, I would encourage you to complete these steps as documented so
you can see how this works and then migrate the functionality to your own
application.

- Step A: Create the VB.NET Web Service

1) Create a new VB.NET Web Service named: GetOrdersByDate
2) Add the following code to Service1.asmx:

<WebMethod()> _
Public Function GetOrdersByDate(ByVal myDate As Date) As DataSet
Dim Con As New SqlConnection("Data Source=<Your SQL Server>;Initial
Catalog=Northwind;Integrated Security=SSPI")
Con.Open()

Dim daOrders As New SqlDataAdapter("[GetOrdersByDate]", Con)
Dim PRM As New SqlParameter("@OrdDate", myDate)
daOrders.SelectCommand.Parameters.Add(PRM)
daOrders.SelectCommand.CommandType = CommandType.StoredProcedure
daOrders.SelectCommand.Parameters(0).Direction =
ParameterDirection.Input

Dim dsOrders As New DataSet
daOrders.Fill(dsOrders)
Return dsOrders
End Function

** NOTE: You will need to change <Your SQL Server> noted above in the
connection string to the name of your SQL Server.

3) Compile and save the web service
4) Test the web method by running this in debug mode. Once Service1.asmx is
displayed, click the GetOrdersByDate link, enter: '4/1/1998' in the myDate
field and click the Invoke button. This should return about 4 records.

- Step B: Create the InfoPath solution

1) Create a new InfoPath solution and choose "From Data Connection" and
complete the following screens:
- First screen: Receive Data
- Second screen: Enter the URL to the web service created above (i.e.
http://localhost/service1.asmx - if you created this directly at:
C:\Inetpub\wwwroot)
- Third screen: Select GetOrdersByDate
- Fourth screen: Click Set Sample Value and enter: 1/1/1900
- Fifth screen: Click Finish

2) From the displayed Data Source Task Pane, expand queryFields and drag
"myDate" to the queryFields area on the form
3) Expand dataFields, drag "Table" to the dataFields area on the form and
select the option: Repeating Section with Controls
4) Preview the form and enter: 4/1/1998 as the date and click the Run Query
button - your Repeating Section should be populate with the same 4 records!

I hope this helps!

Best Regards,

Scott L. Heim
Microsoft Developer Support

This posting is provided "AS IS" with no warranties, and confers no rights.
 
S

Shawn

Scott,

Thank you very much, that worked great as a means of getting the dataset
back from within InfoPath.

A few follow-up questions:
Right now I have an instance where I would like to submit a query to a web
service directly from the vb.net managed code and NOT from an InfoPath rule.
Specifically, I have a Web Service set up to send emails that I am using as a
means of ad-hoc workflow. I can run the code below and successfully call the
web service:

Dim colWSAdapters As DataAdapters = e.XDocument.DataAdapters
Dim daSendMail As WebServiceAdapter2

daSendMail = CType(colWSAdapters("InfoPath_SendEmail"), _
WebServiceAdapter2)
daSendMail.Query()

However, the SendMail web service has five input parameters that I am having
to set via rules. My question is how, from a syntax standpoint, can I set
these parameters from within the VB.Net managed code? If so, PLEASE provide
a syntax example.

Also, I have another instance where I need to make a control read-only from
the managed code, NOT from a conditional formatting standpoint. Is this
possible to do from the .Net managed code and if so could you please provide
a specific syntax example?

Finally, what is a good source for in-depth examples of using VB.net managed
code?

Thanks in advance for your help.

Shawn

Scott L. Heim said:
Hi Shawn,

Here are sample steps (using the Orders table from the SQL Server Northwind
database) to pass a date parameter to a stored procedure that subsequently
returns a dataset that can be consumed by InfoPath.

If possible, I would encourage you to complete these steps as documented so
you can see how this works and then migrate the functionality to your own
application.

- Step A: Create the VB.NET Web Service

1) Create a new VB.NET Web Service named: GetOrdersByDate
2) Add the following code to Service1.asmx:

<WebMethod()> _
Public Function GetOrdersByDate(ByVal myDate As Date) As DataSet
Dim Con As New SqlConnection("Data Source=<Your SQL Server>;Initial
Catalog=Northwind;Integrated Security=SSPI")
Con.Open()

Dim daOrders As New SqlDataAdapter("[GetOrdersByDate]", Con)
Dim PRM As New SqlParameter("@OrdDate", myDate)
daOrders.SelectCommand.Parameters.Add(PRM)
daOrders.SelectCommand.CommandType = CommandType.StoredProcedure
daOrders.SelectCommand.Parameters(0).Direction =
ParameterDirection.Input

Dim dsOrders As New DataSet
daOrders.Fill(dsOrders)
Return dsOrders
End Function

** NOTE: You will need to change <Your SQL Server> noted above in the
connection string to the name of your SQL Server.

3) Compile and save the web service
4) Test the web method by running this in debug mode. Once Service1.asmx is
displayed, click the GetOrdersByDate link, enter: '4/1/1998' in the myDate
field and click the Invoke button. This should return about 4 records.

- Step B: Create the InfoPath solution

1) Create a new InfoPath solution and choose "From Data Connection" and
complete the following screens:
- First screen: Receive Data
- Second screen: Enter the URL to the web service created above (i.e.
http://localhost/service1.asmx - if you created this directly at:
C:\Inetpub\wwwroot)
- Third screen: Select GetOrdersByDate
- Fourth screen: Click Set Sample Value and enter: 1/1/1900
- Fifth screen: Click Finish

2) From the displayed Data Source Task Pane, expand queryFields and drag
"myDate" to the queryFields area on the form
3) Expand dataFields, drag "Table" to the dataFields area on the form and
select the option: Repeating Section with Controls
4) Preview the form and enter: 4/1/1998 as the date and click the Run Query
button - your Repeating Section should be populate with the same 4 records!

I hope this helps!

Best Regards,

Scott L. Heim
Microsoft Developer Support

This posting is provided "AS IS" with no warranties, and confers no rights
 
S

Scott L. Heim [MSFT]

Hi Shawn,

Let me address each question individually:

- SendMail web service
- What types of Rules are you currently using? How do these work now?
- Can you provide an example of your web service code?

- Read only control:
- InfoPath is data driven so you have no access to controls
programmatically. You will need to use Conditional Formatting but maybe
what you could do is base the control being read-only on, say, a check box
node and you could set this to toggle programmatically. Just a thought...

- Resource for VB.NET Managed code
- There are a number of InfoPath books available and one I have seen
recommended a number of times is:

Developing Solutions with Microsoft InfoPath
By: Vani Mandava Teredesai, Patrick Halstead, Matthew Blain

Best regards,

Scott L. Heim
Microsoft Developer Support

This posting is provided "AS IS" with no warranties, and confers no rights.
 
S

Shawn

Scott,

Thanks again for the reply. I will also try to address each of the points.

- SendMail Web Service
- the SendMail web service is something that I am using as a
means of ad-hoc workflow. The type of form I am dealing with is an IT
Incident Form which is utilized with all software related issues in our
company. This form is initiated by either our help desk or users as issues
arise and published to a SharePoint form library once the pertinent
information is collected. Once there someone from the IT Management team
assigns it to be completed by one of the developers or themselves. Upon
completion and further documentation on the form we require digital
signatures from both the IT Business Manger (from the business area impacted)
and the IT Application development manager responsible for who completed the
work and depending on who did the work the application director. Therefore,
I have a button on the bottom of the form that utilizes conditional
formatting and only becomes enabled when all appropriate information is
completed by the person who fixed the problem. At that point they can click
the button and it will email everyone who must sign the form. Behind that
button I am using "Rules" to pre-populate the Input Parameters that the Web
Service SendMail function requires (sToAddress, sSubject, sMessage, etc.).
After all of the Input Parameters are completed it calls a section of managed
vb.net code to apply some additional logic (don't send to someone who has
already signed, etc.) and then actually performs the .Query() function on the
Web Service within the managed code via the Data Adapter object. However,
WHAT I WOULD LIKE TO DO is to be able to set my "Rules" or web service Input
Parameters from within the code so that I can be informative with the Email
message and build a link back to the document via HTML within the email.
This is where I am stuck. I cannot figure out how to set the input
parameters to the web service from within the vb.net managed code. Any
specific example would be greatly appreciated.

- Read only controls
- Your suggestion here worked perfectly!!! Thank you very much.

- Books. I will check out the ones you recommended.

Thanks again for your help. I look forward to your response.

Shawn
 
S

Scott L. Heim [MSFT]

Hi Shawn,

What about something like this on the click event of your button:

Dim objEmailAdapter
Set objEmailAdapter = XDocument.DataAdapters("My email submit")
objEmailAdapter.To = "(e-mail address removed)"
objEmailAdapter.Subject = "Test Report"
objEmailAdapter.Submit

Let me know if this helps!

Scott L. Heim
Microsoft Developer Support

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