Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / ASP.NET / Web Services / January 2007

Tip: Looking for answers? Try searching our database.

Failing when Integer is empty

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Don - 31 Jan 2007 14:50 GMT
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

Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.