Solved

Passing parameters to a stored procedure from C#

Posted on 2003-12-03
10
1,899 Views
Last Modified: 2012-06-21
I have a stored procedure in SQLServer that wants a numeric(5,0) as the passed in parameter.  If I go to query analyzer and type in the value -179.73288 for this parameter, the stored procedure executes properly and the value is rounded in the table.  If I make a SqlParameter with this value and try and pass it into the stored procedure, I get the error "{"Parameter value '-179.73288' is out of range." } and the procedure doesn't execute.

I need to be able to pass parameters like that into the stored procedure from C# - how can I get it to put that parameter into the database?

Thanks!
0
Comment
Question by:bluedaisydawg
  • 5
  • 4
10 Comments
 
LVL 3

Expert Comment

by:WiB
ID: 9868023
Just convert parameter into string using ToString method.
0
 
LVL 1

Author Comment

by:bluedaisydawg
ID: 9868083
The parameter is already in the format of a string when it is added to the SqlParameter

0
 
LVL 3

Expert Comment

by:WiB
ID: 9868153
Ahh, don't add the parameter to SQL parameters..
Create a string consisting of the name of the stored procedure and necessary parameters, exactly like you would use in query analizer.
Something like this, for example:

public SqlDataAdapter CallSqlDataProcedure(string procedure, string[] val)
{
      SqlConnection sqlConnection = new SqlConnection("bla-bla-bla")
      string proc = "EXECUTE " + procedure + " ";
      for (int k=0; k<val.Length; k++)
      {
            if (val[k].Length == 0) proc += "''";
            else proc += val[k];
            if (k != val.Length-1) proc += ", ";
      }

      SqlDataAdapter command = new SqlDataAdapter(proc,sqlConnection);
      return command;
}
0
 
LVL 1

Author Comment

by:bluedaisydawg
ID: 9868211
What I have is a C# DLL that gets the stored procedures and their parameters from the database.  Then when I call the DLL, I pass in the procedure name and the parameters that I have from my app.  It loops through and compares parameters and assigns the value from the calling program (in string format) to the parameters that match.  
0
 
LVL 3

Expert Comment

by:WiB
ID: 9868305
bluedaisydawg
 wrote:
"..and assigns the value from the calling program (in string format) to the parameters that match"

Sorry, didn't get it.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 1

Author Comment

by:bluedaisydawg
ID: 9868336
There is a function in my C# DLL callStoredProcedure(spName, spParams)

Then in the C# DLL there is also an array of structs that look like:
struct paramArray
{
    string procedureName;
    SqlParameters[] params;
}
which has been loaded with all the stored procedures and their associated parameters in the database.  When the function is called - the function loops through it's array of parameters that it got from the database and assigns the value passed in (which is of type string) to the value.  Then it executes the stored procedure...

0
 
LVL 1

Author Comment

by:bluedaisydawg
ID: 9868339
I forgot to add - I tried in the calling code to just make the parameters whole numbers and it works fine.  So I think it must be something with the rounding.  
0
 
LVL 4

Accepted Solution

by:
wile_e_coyote earned 45 total points
ID: 9868353
I think the conversion rules used by Transact-SQL (i.e. Query Analyzer) are less strict than those used by the SqlParameter class.  

Here are a couple of workarounds

1.  You could modify your DLL to do an explicit conversion based on the datatype of the SqlParameter object
2.  You could change the datatype of the stored proc parameter to something a little more generic (e.g. Numeric (10,5) or even Varchar(20)) and perform the conversion in the stored proc
0
 
LVL 3

Expert Comment

by:WiB
ID: 9868408
Ok, got it.
First: you still can use the method I described in C# DLL right after assigning the values to Params.

Second and offtopic:
you could use SQL statement to retrieve all possible combinations of parameters from the database and after call stored procedures. In such case you
1. don't need  SqlParameters[] params
2. don't need to compose params you want to pass in procedure
3. you have all possible combinations of existing parameters
0
 
LVL 1

Author Comment

by:bluedaisydawg
ID: 9868427
I picked that comment as the answer because suggestion numer 2 was what I actually did.  I don't have any more points or I would have definitly upped the points on the question and given some to you WiB for working so hard on my question.  Thanks so much for your help!

0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Events in static methods 3 50
Syntax error 9 45
Word Directory is not in the drop down list 4 24
Turn on intranet settings 1 36
Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
This is a video that shows how the OnPage alerts system integrates into ConnectWise, how a trigger is set, how a page is sent via the trigger, and how the SENT, DELIVERED, READ & REPLIED receipts get entered into the internal tab of the ConnectWise …
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

948 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now