Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Function runs only once

Author  Topic 

TJFrank
Starting Member

3 Posts

Posted - 2010-09-02 : 08:55:34
I have a function to get some Totals (INT) the function works as configured except that it will only run once then, with no error, all processes after it stop and the form loads with no data in datagrids which are loaded with built in tableadapters.

The function should run 5 times to get 5 different totals but only runs once with no visible error, here is the creation code

CREATE FUNCTION dbo.GetClassName
(
@ClassName VARCHAR (10)
)
RETURNS INT
AS
BEGIN
DECLARE @cnt INT
select @cnt = count(Votes.Truckclass)
From Votes
Where TruckClass = @ClassName
RETURN @cnt
END;

Here is my calling function code

private int GetClassCount(string func, string clas) {
SetConnection();
var Icount = 0;
var command = new SqlCommand(func, _sqConnection) {
CommandType = CommandType.StoredProcedure
};

command.Parameters["@ClassName"].Value = clas;
command.Parameters.Add("@cnt", SqlDbType.Int);
command.Parameters["@cnt"].Direction = ParameterDirection.Output;

command.Connection.Open();
command.ExecuteNonQuery();
Icount = (int) command.Parameters["@cnt"].Value;
command.Connection.Close();

return Icount;
}

public void GetSetClassNums() {
_iBobTail = GetClassCount(GetClassFunc,"BobTail");
_iCombo = GetClassCount(GetClassFunc,"Combo");
_iSt = GetClassCount(GetClassFunc,"ST");
_iClassic = GetClassCount(GetClassFunc,"Classic");
_iFleet = GetClassCount(GetClassFunc,"Fleet");
_iTowTruck = GetClassCount(GetClassFunc,"TowTruck");

RunProcedure("DeleteClassAmounts");
ExecuteNonQuery(string.Format("INSERT INTO ClassAmounts VALUES(1,{0},{1},{2},{3},{4},{5});", _iBobTail, _iCombo, _iSt, _iClassic, _iFleet, _iTowTruck));
}


Two questions
Why would it run only once successfully
Why does it error internally and stop all processes after execution and return no visible error

Thanks

Still Around...

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2010-09-02 : 09:00:43
How did you call that function?

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

TJFrank
Starting Member

3 Posts

Posted - 2010-09-02 : 09:05:08
I added the Code for calling it

Still Around...
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2010-09-02 : 09:25:46
A function is NOT a stored procedure...



N 56°04'39.26"
E 12°55'05.63"
Go to Top of Page

TJFrank
Starting Member

3 Posts

Posted - 2010-09-02 : 09:30:27
Ouch...

Thank you for pointing out my error, not enough knowledge

Still Around...
Go to Top of Page
   

- Advertisement -