Home All Groups Group Topic Archive Search About

Help. Error converting nvarchar to numeric?

Author
26 Aug 2005 11:29 PM
Casey
Okay. I have this function in vb.net.
It's purpose is to query the sql server, and return me a count.
Here is the function:

Private Function _countCallsCameIn(ByVal ApplicNum As String, ByVal date1 As
String, ByVal date2 As String) As Integer
            '...the query were going to use...
            Dim lookupQuery As String = "SELECT COUNT(APPLIC_NUM) AS
theCount FROM ACD_CALLDETAIL WHERE (APPLIC_NUM = @ApplicNum) AND (ORIG_DATE
>= @beginDate) AND (ORIG_DATE < @endDate) AND (TALK_TIME > 0)"
            Dim theCommand As SqlCommand = New SqlCommand(lookupQuery,
sqlConn) 'the command.
            theCommand.Parameters.Add("@ApplicNum", ApplicNum) 'the
applicNum value
            theCommand.Parameters.Add("@beginDate", date1) 'begin date value
            theCommand.Parameters.Add("@endDate", date2) 'end date value
            Dim temp As Integer 'create the temp value for the count
            sqlConn.Open() 'open the connection
            temp = theCommand.ExecuteScalar 'throw the return value into the
temp
            sqlConn.Close() 'close the connection
            Return temp 'return the count!
        End Function

When I try to get that value by using this statement:
value = a.countCallsCameIn(applicNum, beginDate, endDate)

It throws this error:
System.Data.SqlClient.SqlException: Error converting data type nvarchar to
numeric. at System.Data.SqlClient.SqlDataReader.Read() at
System.Data.SqlClient.SqlCommand.ExecuteScalar() at
hnflash.COX.data.Aspect._countCallsCameIn(String ApplicNum, String date1,
String date2) in C:\Documents and
Settings\ckriutzf\VSWebCache\clas0dv02\hnflash\gatherData.vb:line 19...

Any ideas?
When I go into query analyzer, and use this query, with actuall values, it
executes just fine.

Thanks for the help!

Author
27 Aug 2005 6:58 PM
Erland Sommarskog
Casey (Ca***@discussions.microsoft.com) writes:
Show quote
> Okay. I have this function in vb.net.
> It's purpose is to query the sql server, and return me a count.
> Here is the function:
>
> Private Function _countCallsCameIn(ByVal ApplicNum As String, ByVal date1
> As
> String, ByVal date2 As String) As Integer
>             '...the query were going to use...
>             Dim lookupQuery As String = "SELECT COUNT(APPLIC_NUM) AS
> theCount FROM ACD_CALLDETAIL WHERE (APPLIC_NUM = @ApplicNum) AND
> (ORIG_DATE >= @beginDate) AND (ORIG_DATE < @endDate) AND (TALK_TIME > 0)"
>    Dim theCommand As SqlCommand = New SqlCommand(lookupQuery, sqlConn)
>         'the command.
>    theCommand.Parameters.Add("@ApplicNum", ApplicNum) 'the applicNum value

I think you should you specify a parameter type for the parameter.
Now you get a nvarchar parameter, and then there is some conversion
on the SQL Server side. If the number can have a decimal delimiter,
this will fail, if the regional settings specify comma as the delimiter,
as SQL Server only understands decimal points for conversion to numeric.


--
Erland Sommarskog, SQL Server MVP, esq***@sommarskog.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techinfo/productdoc/2000/books.asp
Author
28 Aug 2005 10:34 PM
Casey
So how and where would I do that? In my SQL query?

Show quote
"Erland Sommarskog" wrote:

> Casey (Ca***@discussions.microsoft.com) writes:
> > Okay. I have this function in vb.net.
> > It's purpose is to query the sql server, and return me a count.
> > Here is the function:
> >
> > Private Function _countCallsCameIn(ByVal ApplicNum As String, ByVal date1
> > As
> > String, ByVal date2 As String) As Integer
> >             '...the query were going to use...
> >             Dim lookupQuery As String = "SELECT COUNT(APPLIC_NUM) AS
> > theCount FROM ACD_CALLDETAIL WHERE (APPLIC_NUM = @ApplicNum) AND
> > (ORIG_DATE >= @beginDate) AND (ORIG_DATE < @endDate) AND (TALK_TIME > 0)"
> >    Dim theCommand As SqlCommand = New SqlCommand(lookupQuery, sqlConn)
> >         'the command.
> >    theCommand.Parameters.Add("@ApplicNum", ApplicNum) 'the applicNum value
>
> I think you should you specify a parameter type for the parameter.
> Now you get a nvarchar parameter, and then there is some conversion
> on the SQL Server side. If the number can have a decimal delimiter,
> this will fail, if the regional settings specify comma as the delimiter,
> as SQL Server only understands decimal points for conversion to numeric.
>
>
> --
> Erland Sommarskog, SQL Server MVP, esq***@sommarskog.se
>
> Books Online for SQL Server SP3 at
> http://www.microsoft.com/sql/techinfo/productdoc/2000/books.asp
>
>
Author
28 Aug 2005 11:01 PM
Erland Sommarskog
Casey (Ca***@discussions.microsoft.com) writes:
> So how and where would I do that? In my SQL query?

No, in Parameters.Add. There are a couple of overloads to this function,
and there is one with type as well. Please refer to MSDN Library for
details.


--
Erland Sommarskog, SQL Server MVP, esq***@sommarskog.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techinfo/productdoc/2000/books.asp

AddThis Social Bookmark Button