?
Solved

C Sharp / MS SQL - Parameterised Query problem

Posted on 2011-02-16
3
Medium Priority
?
503 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 23

Accepted Solution

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

Assisted Solution

by:HSIHSA
HSIHSA earned 1000 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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…

719 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