Article
.NET Web Services Made Easy
addQuote() WebMethod
Now we can return the quotes, we're going to build the functionality to allow someone to add a quote:
<WebMethod(Description:="Allows anyone
to add a quote to the database" _
& "<br />Takes 3 inputs" _
& "<br /><strong>1. quoteConent</strong> - Content
of the quote" _
& "<br /><strong>2. submittedBy</strong> - Name of the
person who submitted the quote (50 chars)" _
& "<br /><strong>3. webSite</strong> - Web Site of the
person who submitted the quote (50 chars)" _
& "<br /><br />Returns true or false depending if
the quote was successfully added")> _
Public Function addQuote(quoteContent As String,
submittedBy As String, webSite As String) As Boolean
Dim objConnection As New SQLConnection(strConnection)
Dim objCmd As SQLCommand
Dim objParam As SQLParameter
Try
objCmd = New SQLCommand("INSERT INTO tblQuotes
(displayContent, submittedBy, webSite) VALUES
(@displayContent, @submittedBy, @webSite)", objConnection)
objParam = objCmd.Parameters.Add(New SQLParameter
("@displayContent", SQLDBType.Text))
objParam.Value = quoteContent
objParam = objCmd.Parameters.Add(New SQLParameter
("@submittedBy", SQLDBType.VarChar, 50))
objParam.Value = submittedBy
objParam = objCmd.Parameters.Add(New SQLParameter
("@webSite", SQLDBType.VarChar, 50))
objParam.Value = webSite
objCmd.Connection.Open()
objCmd.ExecuteNonQuery()
objCmd.Connection.Close
Return True
Catch ex As Exception
Return False
End Try
End Function
If you've ever used ASP.NET, the above code should be very familiar (so should everything else really!). The first thing we do, as you already know, is declare the WebMethod. But this time you'll notice that the description is longer. As you can see, it supports HTML, which means that you can supply a detailed description with your Web Service! We take three inputs, quoteContent, submittedBy and website) and return a Boolean (True or False).
<WebMethod(Description:="Allows anyone to
add a quote to the database" _
& "<br />Takes 3 inputs" _
& "<br /><strong>1. quoteConent</strong> - Content of the quote" _
& "<br /><strong>2. submittedBy</strong> -
Name of the person who submitted the quote (50 chars)" _
& "<br /><strong>3. webSite</strong> - Web Site
of the person who submitted the quote (50 chars)" _
& "<br /><br />Returns true or false depending if
the quote was successfully added")> _
Public Function addQuote(quoteContent As String,
submittedBy As String, webSite As String) As Boolean
Next, we declare all the objects we're going to use:
Dim objConnection As New SQLConnection(strConnection)
Dim objCmd As SQLCommand
Dim objParam As SQLParameter
Then we insert the data supplied into the database using a Try…Catch…Finally block. As you can see, we do not validate of the data, which means the client would have to.
Try
objCmd = New SQLCommand("INSERT INTO tblQuotes
(displayContent, submittedBy, webSite) VALUES
(@displayContent, @submittedBy, @webSite)", objConnection)
objParam = objCmd.Parameters.Add(New SQLParameter
("@displayContent", SQLDBType.Text))
objParam.Value = quoteContent
objParam = objCmd.Parameters.Add(New SQLParameter
("@submittedBy", SQLDBType.VarChar, 50))
objParam.Value = submittedBy
objParam = objCmd.Parameters.Add(New SQLParameter("@webSite",
SQLDBType.VarChar, 50))
objParam.Value = webSite
objCmd.Connection.Open()
objCmd.ExecuteNonQuery()
objCmd.Connection.Close
Return True
Catch ex As Exception
Return False
End Try
It's as simple as that! If we surf along to our quote.asmx file now, we'll see something like this:

Why not try the Web Services we created? They're both fully functional!