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>
<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
|
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
|
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
|