Hello Members,
I am trying to call a sql function in C# ,it work fine when I call for
SqlCommand cmd = new SqlCommand("SELECT dbo.Calc(1, 18, 4) as totalCost ",cnn);
But when I try to so the same with calling parameters it doesn't show the result.
e.g
SqlCommand cmd = new SqlCommand("SELECT dbo.Calc(Par1,Par2,Par3) as totalCost ",cnn);
Any answers

Passing parameters in C#
Jim Perry
You'll need to pass the values of the 'parameters', not the names:
SqlCommand cmd = new SqlCommand("SELECT dbo.Calc(" + Par1.ToString() + ", " + Par2.ToString() + ", " + Par3.ToString() + ") as totalCost ",cnn);
Also, it's highly recommended to use stored procedures rather than constructing sql strings in code.
David Anton
www.tangiblesoftwaresolutions.com
Instant C#: VB to C# converter
Instant VB: C# to VB converter
Instant C++: C# to C++ converter, VB to C++ converter
Instant Python: C# to Python converter, VB to Python converter
OSPACS
The second is not only nicer, but also securer.
vonlinkerstain
Figo,
Thanks for the input ,its working for me.
Michael Ressler
Two options:
1.-
SqlCommand cmd = new SqlCommand("SELECT dbo.Calc(" + par1 + "," + par2 + "," + par3 + ") AS TotalCost", cnn);
2.-
SqlCommand cmd = new SqlCommand("SELECT dbo.Calc(@par1, @par2, @par3) AS TotalCost", cnn);
cmd.Parameters.Add("@par1", par1);
cmd.Parameters.Add("@par2", par2);
cmd.Parameters.Add("@par3", par3);
The first is the quick no so nice option. The second is the more complicated but nicer.