Solved

C Sharp / MS SQL - Parameterised Query problem

Posted on 2011-02-16
3
500 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

NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

Question has a verified solution.

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

Suggested Solutions

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

685 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