Solved

C Sharp / MS SQL - Parameterised Query problem

Posted on 2011-02-16
3
498 Views
Last Modified: 2012-05-11
Hi,

I have an SQL insert statement that i am using a parameterised query to pass the values within a program i am writing in C#.

the SQL string looks like this:

sql = "Insert Into equities company_name,mnemonic,sedol,code1,code2,primary_analyst,secondary_analyst, sales,sector_id,index_id,created_dttm,mod_dttm,act_inact) ";
                sql = sql + "values (@company_name,@mnemonic,@sedol,@code1,@code2,@analyst1,@analyst2,@sales,@sector,@index,@now,@now,'A')";

my problem is, when i pass a null value for @analyst2 or @sales which are numeric fields in the database, SOMEWHERE its being assumed that these input values should be smalldatetime values. If i pass an int as the value for these values, it correctly assumes these fields to be ints... below i have pasted what is being executed against the SQL Server from a trace using SQL Server profiler. The first one you can see that null values are being passed in and @analyst2 and @sales are thought to be smalldatetime fields, whereas in the second one, ints have have been passed in and the fields have been assumed correctly to be numerics and all works fine. I would like someone to help me shed some light why/where these types get assumed to be of an incorrect type...

EXAMPLE 1 (incorrect value types as smalldatetime)
-----------------------------------------------------------------------------------------------------------------
exec sp_executesql N'Insert Into equities (company_name,mnemonic,sedol,code1,code2,primary_analyst,secondary_analyst, sales,sector_id,index_id,created_dttm,mod_dttm,act_inact) values (@company_name,@mnemonic,@sedol,@code1,@code2,@analyst1,@analyst2,@sales,@sector,@index,@now,@now,''A'')',N'@company_name varchar(7),@mnemonic varchar(4),@sedol varchar(8000),@code1 varchar(8000),@code2 varchar(8000),@analyst1 int,@analyst2 smalldatetime,@sales smalldatetime,@sector int,@index int,@now smalldatetime',@company_name='Tes''t 2',@mnemonic='TST2',@sedol='',@code1='',@code2='',@analyst1=10,@analyst2=NULL,@sales=NULL,@sector=14,@index=5,@now='2011-02-15 18:06:00:000'

EXAMPLE 2 (Correctly assumed types when value is passed)
---------------------------------------------------------------------------------------------------------------
exec sp_executesql N'Insert Into equities (company_name,mnemonic,sedol,code1,code2,primary_analyst,secondary_analyst, sales,sector_id,index_id,created_dttm,mod_dttm,act_inact) values (@company_name,@mnemonic,@sedol,@code1,@code2,@analyst1,@analyst2,@sales,@sector,@index,@now,@now,''A'')',N'@company_name varchar(7),@mnemonic varchar(4),@sedol varchar(9),@code1 varchar(7),@code2 varchar(8),@analyst1 int,@analyst2 int,@sales int,@sector int,@index int,@now smalldatetime',@company_name='Tes''t 8',@mnemonic='TST8',@sedol='dfde34333',@code1='23fgfeg',@code2='dgdr4563',@analyst1=10,@analyst2=12,@sales=11,@sector=17,@index=6,@now='2011-02-16 07:59:00:000'
0
Comment
Question by:alnunn
3 Comments
 
LVL 23

Accepted Solution

by:
wdosanjos earned 250 total points
ID: 34905654
How do you set those parameter values on C#?
0
 
LVL 2

Assisted Solution

by:HSIHSA
HSIHSA earned 250 total points
ID: 34905721
http://www.bigresource.com/MS_SQL-Insert-null-value-in-column-with-int-datatype-SGwVXwG1.html#4mQhAeMD

go through the link

you need to pass System.Data.SqlTypes.SqlInt32.Null
0
 
LVL 1

Author Comment

by:alnunn
ID: 34905802
I have found the error of my ways! I just couldnt see it. It was a typo where i set the values and types in some wrapper functions of mine...

Sorry for the bother!
0

Featured Post

VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …

773 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question