I am selecting a distinct user from IT_Cases_List and stored it in an arraystaff().
From this array, I will then call a Stored Procedure to count the no of cases attended by this user,(arraystaff(i)) and loop it until arraystaff.length-1
but the problem is that the count does not tally.
Sub getStaff(ByVal month As String)
If Not con.State = ConnectionState.Closed Then
con.Open()
End If
Dim s As String = "select distinct Attended_by from IT_Cases_List where month(Resolution_date) ='" & month & "' "
s = s & "And Year(Resolution_date) ='" & ddyear.SelectedValue & "' and Attended_by is not null "
cmd = New SqlCommand(s, con)
da = New SqlDataAdapter
ds = New DataSet
da.SelectCommand = cmd
da.Fill(ds)
If ds.Tables(0).Rows.Count > 0 Then
staffcount = ds.Tables(0).Rows.Count
ReDim arrstaff(staffcount - 1)
For Me.i = 0 To staffcount - 1
arrstaff(i) = ds.Tables(0).Rows(i).Item("Attended_by")
Next
getCases()
End If
End Sub
Sub getCases()
If con.State = ConnectionState.Closed Then
con.Open()
End If
ReDim arrdata(arrstaff.Length - 1, 0)
For Me.i = 0 To arrstaff.Length - 1
cmd = New SqlCommand("get_cases", con)
cmd.CommandType = CommandType.StoredProcedure
cmd.Parameters.Add("@ename", SqlDbType.VarChar).Value = arrstaff(i)
cmd.Parameters.Add("@Yr", SqlDbType.VarChar).Value = ddyear.SelectedValue
cmd.Parameters.Add("month", SqlDbType.VarChar).Value = m
cmd.ExecuteNonQuery()
da = New SqlDataAdapter()
da.SelectCommand = cmd
ds = New DataSet
da.Fill(ds)
If Not IsDBNull(ds.Tables(0).Rows(i).Item("NoCase")) Then
arrdata(i, 0) = ds.Tables(0).Rows(i).Item("NoCase")
End If
Next
cmd = New SqlCommand("delete from cases_Temp", con)
cmd.ExecuteNonQuery()
con.Close()
End Sub
and this is my stored procedure
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER procedure [dbo].[get_cases](
@Ename varchar(100),
@Yr varchar(50),
@month varchar(30))
AS
BEGIN
declare @NoCase as int
select @NoCase=COUNT(*)
from IT_Cases_List
where Attended_by= @Ename and month(Resolution_date) =@month and
Year(Resolution_date)=@Yr and Attended_by is not null
insert into cases_temp(Ename,NoCase)
values(@Ename,@NoCase)
select * from cases_Temp
end
i don’t know what have i done wrong.
any help will be much appreciated.
UPDATE
ok, i’ve called only once getcases but i’m still having the same problem.
this is what i get when i run the program:

if i get Count(*) from the database, the total no of cases i should be getting is 132, whereas the total of cases i get from the program is 157 (7+7+20+20+49+49+5)
if i run the query from sql, this is what i should be getting

i notice that the Count number gets duplicated (7,7,20,20,49,49,5) instead of (7,20,49,5,10,27,13)
can anybody tell me what have i done wrong?
You should have called
getCases()only once because inside it has a loop for each staff (arraystaff). Another thing, can you provide us a little more info regarding the problem? eg. sample records, desired output so we can more help you 🙂UPDATE 1
move the
ds = New DataSetbefore the For Loop, and pass theCommand Objectto theDataAdapter Object.