Questions tagged [sqlparameter]

a .NET class for representing SQL parameters in SqlCommands

SqlParameter is a noninheritable .NET class in the System.Data.SqlClient namespace whose instances represent a parameter for a SqlCommand.

319 questions
231
votes
20 answers

Assign null to a SqlParameter

The following code gives an error - "No implicit conversion from DBnull to int." SqlParameter[] parameters = new SqlParameter[1]; SqlParameter planIndexParameter = new SqlParameter("@AgeIndex", SqlDbType.Int); planIndexParameter.Value =…
Relativity
  • 6,690
  • 22
  • 78
  • 128
75
votes
4 answers

Size property has an invalid size of 0

I am working on a social network, one of my procedures returns a VARCHAR output. So this is what I wrote: SqlParameter job1 = cmd2.Parameters.Add("@job", SqlDbType.VarChar); job1.Direction = ParameterDirection.Output; However this error comes…
RuMoR
  • 775
  • 1
  • 5
  • 7
42
votes
1 answer

How to create NVarchar(max) Sqlparameter in C#?

I've got the following code to pull back a DataTable using a stored procedure and inputting a string parameter @JobNumbers, which is dynamically created string of job numbers (and therefore length is unknown): using (SqlConnection connection = new…
sr28
  • 4,728
  • 5
  • 36
  • 67
33
votes
2 answers

Why does the SqlParameter name/value constructor treat 0 as null?

I observed a strange problem in a piece of code where an adhoc SQL query was not producing the expected output, even though its parameters matched records in the data source. I decided to enter the following test expression into the immediate…
Bradley Smith
  • 13,353
  • 4
  • 44
  • 57
33
votes
9 answers

WHERE IN (array of IDs)

I have webservice which is passed an array of ints. I'd like to do the select statement as follows but keep getting errors. Do I need to change the array to a string? [WebMethod] public MiniEvent[] getAdminEvents(int buildingID, DateTime…
user17510
  • 1,549
  • 5
  • 20
  • 37
32
votes
2 answers

SqlParameter with default value set to 0 doesn't work as expected

I was doing something like this: SqlParameter param = new SqlParameter("@Param", 0) { SqlDbType = SqlDbType.Int }; private void TestParam(SqlParameter param) { string test = param.Value.ToString(); // Getting NullReferenceException here } But…
Meryovi
  • 6,121
  • 5
  • 42
  • 65
29
votes
5 answers

How to pass XML from C# to a stored procedure in SQL Server 2008?

I want to pass xml document to sql server stored procedure such as this: CREATE PROCEDURE BookDetails_Insert (@xml xml) I want compare some field data with other table data and if it is matching that records has to inserted in to the…
Geeth
  • 5,282
  • 21
  • 82
  • 133
27
votes
4 answers

SqlCommand Parameters size confusion

I have the following line of code: sqlcommand.Parameters.Add("@LinkID", SqlDbType.Int, 4).Value = linkID; But, I'm slightly confused about the use of size. Is this saying that its 4 bytes in size? Or a length of 4 so 1234 is acceptable but 12345…
Neil Knight
  • 47,437
  • 25
  • 129
  • 188
24
votes
2 answers

What is the purpose of System.Data.SqlClient.SqlParameter.IsNullable?

I'm currently trying to write a simple C# wrapper class for all the stored procedures in a database. While building some parameters in C#, I noticed the property SqlParameter.IsNullable and wondered what this is for. As far as I am aware, it is not…
Martyn
  • 1,446
  • 2
  • 19
  • 30
21
votes
3 answers

SQLCommand.Parameters.Add - How to give decimal value size?

How would you specify this: Decimal(18,2) In this: SqlComm.Parameters.Add("@myValue", SqlDbType.Decimal, 0, "myValue"); Currently I have defined precision = 2 from the design side properties. I'm just curious as to how to accomplish this from the…
Tony D.
  • 551
  • 1
  • 10
  • 26
19
votes
7 answers

Best method of assigning NULL value to SqlParameter

I have a number of optional input parameters I am using in a C# class method. Since the optional syntax creates a value of '0' when the parameter is not used, the SQL insert command I call in the method winds up inserting as such. However, I need…
NealR
  • 10,189
  • 61
  • 159
  • 299
15
votes
5 answers

Passing null as SQLParameter DateTime value

I have the following query: INSERT INTO CWS_FORWARDING_PROFILE (TNR_COMPANY_PROFILE,BOL_FORWARD_MAIL,BOL_FORWARD_SMS,BOL_FORWARD_MESSAGES ,DT_MO_FROM1,DT_MO_FROM2,DT_MO_FROM3,DT_MO_TO1,DT_MO_TO2,DT_MO_TO3 …
JeremyVm
  • 385
  • 2
  • 3
  • 12
15
votes
5 answers

Is it possible to get the parsed text of a SqlCommand with SqlParameters?

What I am trying to do is create some arbitrary sql command with parameters, set the values and types of the parameters, and then return the parsed sql command - with parameters included. I will not be directly running this command against a sql…
Burg
  • 1,988
  • 1
  • 18
  • 22
12
votes
1 answer

How does SqlCommand sanitize parameters?

Using SqlParameters is a recommended method to prevent SQL Injection in your database queries. Where can I find the code/function that internally sanitizes these parameters? I'd like to re-use this function in a custom implementation of mine. I…
Josh Stodola
  • 81,538
  • 47
  • 180
  • 227
12
votes
2 answers

SqlParameterCollection only accepts non-null SqlParameter type objects, not DBNull objects

When I add the SQL parameter p to the collection I get an InvalidCastException with the message from the post title. parentId is a nullable integer and a nullable integer in the database. Why do I get this exception and how can I solve it? I do not…
Elisabeth
  • 20,496
  • 52
  • 200
  • 321
1
2 3
21 22