473,394 Members | 1,752 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Invalid CastException with SqlDBType

I am having a strange problem with invalid type casts. I am trying to
update a MS SQL Database with a stored procedure. When I setup the
parameters collection for the command object I get a invalid cast
exception error:

Compiler Error Message: BC30311: Value of type 'Date' cannot be
converted to 'Integer'.

The real problem here is that the type in the database and the stored
prcedure aren't integers at all rather they are datetime types. No
matter what date format or information I pass to the parameter setting
up the date I get the error or a varient of it but all are trying to
cast it to an integer.

Here's my Code:
Dim ConnectionString As String =
"server=(local);database=poolMaint;trusted_connect ion=true"
dim CommandText as string = "insert_Ph_Calibrations_1"

dim myConnection as new SqlConnection(ConnectionSTring)
dim myCommand as new SqlCommand(CommandText, myConnection)
dim workParam as new SqlParameter()

myCommand.CommandType = CommandType.StoredProcedure

' setup the parameters for the stored procedure

myCommand.Parameters.Add("@PhCalibration_ID_1",
SqlDBType.nchar, 1, "PhCalibration_ID_1")
myCommand.Parameters.Add("@DateRecord_2", SqlDBType.DateTime,
datetime.now, "DateRecord_2")
myCommand.Parameters.Add("@Tech_3", SqlDBType.nchar,
txtTech.text, "Tech_3")

myCommand.Connection.Open()
myCommand.ExecuteNonQuery()
myCommand.Connection.Close()
Here's My Stored Procedure:

CREATE PROCEDURE [insert_PhCalibrations_1]
(@PhCalibration_ID_1 [uniqueidentifier],
@DateRecord_2 [datetime],
@Tech_3 [char](16))

AS INSERT INTO [PoolMaint].[dbo].[PhCalibrations]
( [PhCalibration_ID],
[DateRecord],
[Tech])

VALUES
( @PhCalibration_ID_1,
@DateRecord_2,
@Tech_3)
GO

Any help you can offer me will be greatly appreciated as I have no
clue about this casting problem

jeremiah

Nov 18 '05 #1
2 2220
I am a C# coder, so I will try my best a vb syntax.

Your myCommand.Parameters.Add needs some work

First the overloads:

http://msdn.microsoft.com/library/de...ssaddtopic.asp

You are trying to call the Add method passing
("@PhCalibration_ID_1",SqlDBType.nchar, 1, "PhCalibration_ID_1")
since you are passing 4 parameters it is trying to match it up to the
compiler is matching it to:
Add( string*, SqlDbType, int, string** )
* is the parameter name
**is the column name.

Now typically I have only seen the ** string used with the SqlDataAdapter
and its update command, so I don't think this is the one you are intending
to use.

The problem I believe you are having is the Add("@DateRecord_2",
SqlDBType.DateTime, datetime.now, "DateRecord_2")

The DateTime.Now is trying to cast into a int. This isn't going to work.

What I think you need to do is:

dim myParam = new SqlParameter( "@DateRecord_2", SqlDbType.DateTime )
myParam.Value = DateTime.Now
myCommand.Parameters.Add( myParam )

myParam = new SqlParameter( "@Tech_3", SqlDbType.NChar, 16 )
myParamer.Value = txtTech.Text
myCommand.Parameters.Add( myParam )

Also it appears as though you are trying to place an ID on the table. Why
not in Sql set up the column as identity and let it seed itself. You won't
know what identity to pass it and you might run into concurrency issues if
you don't let sql create its own identity.

HTH,

bill
<ad******@comcast.net> wrote in message
news:sv********************************@4ax.com...
I am having a strange problem with invalid type casts. I am trying to
update a MS SQL Database with a stored procedure. When I setup the
parameters collection for the command object I get a invalid cast
exception error:

Compiler Error Message: BC30311: Value of type 'Date' cannot be
converted to 'Integer'.

The real problem here is that the type in the database and the stored
prcedure aren't integers at all rather they are datetime types. No
matter what date format or information I pass to the parameter setting
up the date I get the error or a varient of it but all are trying to
cast it to an integer.

Here's my Code:
Dim ConnectionString As String =
"server=(local);database=poolMaint;trusted_connect ion=true"
dim CommandText as string = "insert_Ph_Calibrations_1"

dim myConnection as new SqlConnection(ConnectionSTring)
dim myCommand as new SqlCommand(CommandText, myConnection)
dim workParam as new SqlParameter()

myCommand.CommandType = CommandType.StoredProcedure

' setup the parameters for the stored procedure

myCommand.Parameters.Add("@PhCalibration_ID_1",
SqlDBType.nchar, 1, "PhCalibration_ID_1")
myCommand.Parameters.Add("@DateRecord_2", SqlDBType.DateTime,
datetime.now, "DateRecord_2")
myCommand.Parameters.Add("@Tech_3", SqlDBType.nchar,
txtTech.text, "Tech_3")

myCommand.Connection.Open()
myCommand.ExecuteNonQuery()
myCommand.Connection.Close()
Here's My Stored Procedure:

CREATE PROCEDURE [insert_PhCalibrations_1]
(@PhCalibration_ID_1 [uniqueidentifier],
@DateRecord_2 [datetime],
@Tech_3 [char](16))

AS INSERT INTO [PoolMaint].[dbo].[PhCalibrations]
( [PhCalibration_ID],
[DateRecord],
[Tech])

VALUES
( @PhCalibration_ID_1,
@DateRecord_2,
@Tech_3)
GO

Any help you can offer me will be greatly appreciated as I have no
clue about this casting problem

jeremiah

Nov 18 '05 #2
Thanks for your post, but I did figure out the problem eventually. You
are correct, the problem was iwith my parameters.add method.

I assumed there was a way to get SQL to generate the ID automatically
but I didn't know what it was. I will look into setting up the column
as an identity.

thanks for the help
On Wed, 26 May 2004 17:42:41 -0500, "William F. Robertson, Jr."
<wf*********@kpmg.com> wrote:
I am a C# coder, so I will try my best a vb syntax.

Your myCommand.Parameters.Add needs some work

First the overloads:

http://msdn.microsoft.com/library/de...ssaddtopic.asp

You are trying to call the Add method passing
("@PhCalibration_ID_1",SqlDBType.nchar, 1, "PhCalibration_ID_1")
since you are passing 4 parameters it is trying to match it up to the
compiler is matching it to:
Add( string*, SqlDbType, int, string** )
* is the parameter name
**is the column name.

Now typically I have only seen the ** string used with the SqlDataAdapter
and its update command, so I don't think this is the one you are intending
to use.

The problem I believe you are having is the Add("@DateRecord_2",
SqlDBType.DateTime, datetime.now, "DateRecord_2")

The DateTime.Now is trying to cast into a int. This isn't going to work.

What I think you need to do is:

dim myParam = new SqlParameter( "@DateRecord_2", SqlDbType.DateTime )
myParam.Value = DateTime.Now
myCommand.Parameters.Add( myParam )

myParam = new SqlParameter( "@Tech_3", SqlDbType.NChar, 16 )
myParamer.Value = txtTech.Text
myCommand.Parameters.Add( myParam )

Also it appears as though you are trying to place an ID on the table. Why
not in Sql set up the column as identity and let it seed itself. You won't
know what identity to pass it and you might run into concurrency issues if
you don't let sql create its own identity.

HTH,

bill


Nov 18 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Jean-Francois Gagnon | last post by:
Hi, FYI, We were able to fix a problem we had with SQLServer after Hotfix 815495 was installed. Our application, written in VB.NET, with ADO.NET under .NET framework 1.0, would generate an...
0
by: Stephen Witter | last post by:
I am getting the error: Parameter 4: '@IDENTITY' of type: String, the property Size has an invalid size: 0 When trying to execute a sproc with an varchar output. Here is the asp.net code: ...
7
by: cnu | last post by:
Hi I have to write images(.gif/.bmp/.jpg/.ico), to db and read them. Uploading images to db works fine for me. Reading from db to byte is also ok. But, when I try to display them in my form...
0
by: CW | last post by:
I get this FieldCount error when I attempt to bind a datagrid with a dataset, not a datareader object. The code snippet is as belows: 'PopulateForm called in Page Load event Private Sub...
9
by: Jerome | last post by:
Hi, I keep getting the same compiler error: 'Name 'SqlDbType' is not declared' and I don't know why? I've declared the following: <%@ Page Language="VB" ContentType="text/html"...
1
by: Andy G | last post by:
I've been getting this error all day. Could someone please look at my stored procedure and the code. I have a form that grabs and email address the user types in, calls a stored procedure with an...
1
by: Hifni Shahzard | last post by:
Hi, I got a stored procedure, where it returns a value. But if I execute it. It gives an error as "Invalid cast from System.Int32 to System.Byte.". To make clear how do I execute this, below I'm...
4
by: daz_oldham | last post by:
Hi All I am trying to execute a stored procedure that does a very simple lookup and returns a text field. However, when I try to execute it, I am getting a rather strange error that I can't seem...
4
by: Chris Bordeman | last post by:
I have a DataColumn, want to derive the DbType. I can do column.GetType() but that's a system type, not a db type. How do I convert it to the corresponding type? Thanks much! Chris B.
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.