Hello, I'm creating a web service that will allow people to enter their
contact information into a SQL Server table. I get it to work when I enter
all of the fields and press the invoke button, but if I leave one of the
integer fields(ie StateID) empty if errors out. I tried to make the
parameters optionally but it says thats not possible with a webmethod. How
do you handle empty integer fields.
I put the code below.
Thanks
<WebMethod(Description:="Inserts a record into a contacts temp table.")> _
Public Function PutContact(ByVal FirstName As String, ByVal
LastName As String, ByVal CompanyName As String, _
ByVal EmailAddress As String, ByVal HomePhone As String, ByVal
CellNumber As String, ByVal WorkPhone As String, ByVal Address1 As String,
ByVal City As String, _
ByVal StateID As Integer, ByVal CountryID As Integer, ByVal
PreferenceID As Integer, ByVal DevID As Integer, ByVal SalesBedroomsID As
Integer, ByVal TrafficSourceID As Integer, ByVal RelatedTenant As Integer,
ByVal Comment As String) As Boolean
Dim objConnection As New SqlConnection(strConnection)
Dim objCmd As SqlCommand
Dim objParam As SqlParameter
Try
objCmd = New SqlCommand("proc_ContactHoldInsert", objConnection)
objCmd.CommandType = CommandType.StoredProcedure
objParam = objCmd.Parameters.Add(New SqlParameter("@FirstName",
SqlDbType.VarChar, 50))
objParam.Value = FirstName
objParam = objCmd.Parameters.Add(New SqlParameter("@LastName",
SqlDbType.VarChar, 50))
objParam.Value = LastName
objParam = objCmd.Parameters.Add(New
SqlParameter("@CompanyName", SqlDbType.VarChar, 100))
objParam.Value = CompanyName
objParam = objCmd.Parameters.Add(New
SqlParameter("@EmailAddress", SqlDbType.VarChar, 50))
objParam.Value = EmailAddress
objParam = objCmd.Parameters.Add(New SqlParameter("@HomePhone",
SqlDbType.VarChar, 30))
objParam.Value = HomePhone
objParam = objCmd.Parameters.Add(New SqlParameter("@CellNumber",
SqlDbType.VarChar, 30))
objParam.Value = CellNumber
objParam = objCmd.Parameters.Add(New SqlParameter("@WorkPhone",
SqlDbType.VarChar, 30))
objParam.Value = WorkPhone
objParam = objCmd.Parameters.Add(New SqlParameter("@Address1",
SqlDbType.VarChar, 100))
objParam.Value = Address1
objParam = objCmd.Parameters.Add(New SqlParameter("@City",
SqlDbType.VarChar, 20))
objParam.Value = City
objParam = objCmd.Parameters.Add(New SqlParameter("@StateID",
SqlDbType.Int))
objParam.Value = StateID
objParam = objCmd.Parameters.Add(New SqlParameter("@CountryID",
SqlDbType.Int))
objParam.Value = CountryID
objParam = objCmd.Parameters.Add(New
SqlParameter("@PreferenceID", SqlDbType.Int))
objParam.Value = PreferenceID
objParam = objCmd.Parameters.Add(New SqlParameter("@DevID",
SqlDbType.Int))
objParam.Value = DevID
objParam = objCmd.Parameters.Add(New
SqlParameter("@SalesBedroomsID", SqlDbType.Int))
objParam.Value = SalesBedroomsID
objParam = objCmd.Parameters.Add(New
SqlParameter("@TrafficSourceID", SqlDbType.Int))
objParam.Value = TrafficSourceID
'If RelatedTenant <> "" Then
objParam = objCmd.Parameters.Add(New
SqlParameter("@RelatedTenant", SqlDbType.Int))
objParam.Value = RelatedTenant
' End If
objParam = objCmd.Parameters.Add(New SqlParameter("@Comment",
SqlDbType.VarChar, 255))
objParam.Value = Comment
objCmd.Connection.Open()
objCmd.ExecuteNonQuery()
objCmd.Connection.Close()
Return True
Catch ex As Exception
Return False
End Try
End Function
Eugen - 31 Jan 2007 16:05 GMT
Hi Don,
You can do one of the following:
1) create your data member as an Object instead of an Int32
Object m_test = new Int32();
You can assign m_test = null;
You can assign as well: m_test = 1;
Pass this datamember in a function as private void test(Object objInt)
and add a try/catch around this line: int nTest = (Int32)objInt;
In this way you will cacth a null object.
2) pass it as a null string and convert to Int32
3) set a known value if you are not using in the application like -1 (in
case you use only positive values)
4) Use SqlInt32 data type that has a Null property.
Hope that helps,
Best regards,
Eugen
> Hello, I'm creating a web service that will allow people to enter their
> contact information into a SQL Server table. I get it to work when I enter
[quoted text clipped - 104 lines]
>
> End Function