Groups > Asp .Net > ASP.NET XML Web Services > Re: How can I pass parameter from client to web service




How can I pass parameter from client to web service

How can I pass parameter from client to web service
Thu, 27 Mar 2008 18:07:14 +000
Hi,

How can I pass parameter to web service side, below code is fine, but just
return all field, if I put product id on textbox and pass it to web service side
and respond to client, how can I modify the codes in "default.aspx.vb"
and "Product.vb" file.

 

client side 

 "default.aspx"

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        &nbsp;
        <br />
        <asp:Button ID="Button1" runat="server"
Text="Get Product" /><br />
        <br />
        <asp:GridView ID="GridView1" runat="server">
        </asp:GridView>
    
    </div>
    </form>
</body>
</html>

 

 "default.aspx.vb"

Partial Class Default2
    Inherits System.Web.UI.Page
    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Button1.Click
        Dim ws As New ProjectNorthWind.Product()
        GridView1.DataSource = ws.GetProduct()
        GridView1.DataBind()
    End Sub

End Class

web service side

"product.vb"

Imports System.Web
Imports System.Web.Services
Imports System.Web.Services.Protocols
Imports System.Data
Imports System.Data.SqlClient


<WebService(Namespace:="http://www.xxx.com/product")> _
<WebServiceBinding(ConformsTo:=WsiProfiles.BasicProfile1_1)> _
Public Class Product
    Inherits System.Web.Services.WebService
    <WebMethod()> _
    Public Function GetProduct() As DataSet
        Dim conn As SqlConnection
        Dim myDataAdapter As SqlDataAdapter
        Dim myDataSet As DataSet
        Dim cmdString As String = "Select * From Product"
        conn = New
SqlConnection(ConfigurationManager.ConnectionStrings("ProjectString").
ConnectionString)
        myDataAdapter = New SqlDataAdapter(cmdString, conn)
        myDataSet = New DataSet()
        myDataAdapter.Fill(myDataSet, "Product")
        Return myDataSet
    End Function
End Class
Post Reply
Re: How can I pass parameter from client to web service
Thu, 27 Mar 2008 18:54:41 +000
First of all your  GetProduct() function should be changed to accept the
parameter you would like to pass eg

public Function GetProduct(byval ID as integer)  as DataSet

..your code...

End Function

 

Then refresh the web reference in the client and  change the code here

Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs)
Handles Button1.Click

dim iProductID as integer=99
        Dim ws As New ProjectNorthWind.Product()
        GridView1.DataSource = ws.GetProduct(iProductID )
        GridView1.DataBind()
    End Sub
Post Reply
Re: How can I pass parameter from client to web service
Fri, 28 Mar 2008 06:56:31 +000
Hi, 

 

The web service side have problem:

The code i have already changed as below

<WebMethod()> _
    Public Function GetProduct(ByVal pname As String) As DataSet
        Dim conn As SqlConnection
        Dim myDataAdapter As SqlDataAdapter
        Dim myDataSet As DataSet
        Dim cmdString As String = "Select productid,productname From
Product where productname like '%'+ pname + '%'"
        conn = New
SqlConnection(ConfigurationManager.ConnectionStrings("ProjectString").
ConnectionString)
        myDataAdapter = New SqlDataAdapter(cmdString, conn)
        myDataSet = New DataSet()
        myDataAdapter.Fill(myDataSet, "Product")
        Return myDataSet
    End Function
End Class

 

but generate follow error:

System.Data.SqlClient.SqlException: Invalid column name 'pname'.
   at System.Data.SqlClient.SqlConnection.OnError(SqlException exception,
Boolean breakConnection)
   at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException
exception, Boolean breakConnection)
   at
System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject
stateObj)
   at System.Data.SqlClient.TdsParser.Run(RunBehavior runBehavior, SqlCommand
cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler,
TdsParserStateObject stateObj)
   at System.Data.SqlClient.SqlDataReader.ConsumeMetaData()
   at System.Data.SqlClient.SqlDataReader.get_MetaData()
   at System.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds,
RunBehavior runBehavior, String resetOptionsString)
   at System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior
cmdBehavior, RunBehavior runBehavior, Boolean returnStream, Boolean async)
   at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior
cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method,
DbAsyncResult result)
   at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior
cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method)
   at System.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior behavior,
String method)
   at System.Data.SqlClient.SqlCommand.ExecuteDbDataReader(CommandBehavior
behavior)
   at
System.Data.Common.DbCommand.System.Data.IDbCommand.ExecuteReader(CommandBehavio
r behavior)
   at System.Data.Common.DbDataAdapter.FillInternal(DataSet dataset, DataTable[]
datatables, Int32 startRecord, Int32 maxRecords, String srcTable, IDbCommand
command, CommandBehavior behavior)
   at System.Data.Common.DbDataAdapter.Fill(DataSet dataSet, Int32 startRecord,
Int32 maxRecords, String srcTable, IDbCommand command, CommandBehavior
behavior)
   at System.Data.Common.DbDataAdapter.Fill(DataSet dataSet, String srcTable)
   at Product.GetProduct(String pname) in
C:\Inetpub\WebService\App_Code\Customer.vb:line 21
Post Reply
Re: How can I pass parameter from client to web service
Fri, 28 Mar 2008 13:48:05 +000
Replace this 

Dim cmdString As String = "Select productid,productname From Product where
productname like '%'+ pname + '%'"

with this

Dim cmdString As String = String.Format( "Select productid,productname From
Product where productname like '%%'",pname)

However I would strongly advise using paramters instead of concatentated sql
Post Reply
about | contact