Changed stored procedure causes VB runtime error ALTER  Procedure [dbo].[HRptForm3A]
@StrCond Varchar(1000),
@SelYear int,
@Type varchar(1)
as
Declare @Tbl varchar(30)
    if ( @Type = 'O')
        begin
            set @Tbl = 'TblResult'
        end
    else if( @Type = 'I')
        begin
            set @Tbl = 'TblGResult'
        end
Declare @FromPayPeriodId Int
Declare @ToPayPeriodId Int
    select @FromPayPeriodId=isnull(Min(PayPeriodId),0),@ToPayPeriodId=isnull(Max(PayPeriodId),0) from tblPayPeriodMst Where 
    Month(FromDate) in (4,5,6,7,8,9,10,11,12) and Year(FromDate)=@SelYear or
    Month(FromDate) in (1,2,3)and Year(FromDate)=@SelYear+1
Declare @Qry2 Varchar(1000)
Set @Qry2='Select E.Empid,E.Empcode,E.Empname,E.EmpFhName,E.Condate,E.PfNo as AccountNo,E.VPF,E.Leftdate,E.ReasonForLeft,R.payperiodId,
R.Lop,R.LWF,R.PFGross,R.PF,R.EPFE,R.EPFR, **Month(P.FromDate)+1** as Monthnumber ,Year(P.FromDate) as YearNumber,
Case 
When Month(P.FromDate) in(4,5,6,7,8,9,10,11,12) then  Month(P.FromDate)
when Month(P.FromDate) in(1,2,3) then  Month(P.FromDate)
end as Mnth
From 
PayEmpMstView as E,' + @Tbl + ' as R,tblPayPeriodMst as P 
where  E.Empid=R.Empid  and len(E.PFNo)&gt;0
and R.PayPeriodId=P.PayPeriodid  and 
P.PayPeriodid between ' + Convert(Varchar(10),@FromPayPeriodId) + ' and '+ Convert(Varchar(10),@ToPayPeriodId)+ @StrCond +
' Order by E.EmpCode,Year(P.FromDate),Month(P.ToDate)'
Exec(@Qry2)


Exec ('Select E.EmpCode,E.EmpId  from tblPayEmployee E Where  0=0  ' +  @StrCond )

GO


when i change the highlighted value Month(P.FromDate) to Month(P.FromDate)+1. its showing exception error in the coding part of vb.net as HRESULT:800340 error
this change is working in sql server 2000 but not in 2005.
how to overcome this

Karthik added this code in the comments. It appears to be VB.NET code. It's not super legible.

MNth(4) = "Apr-" &amp; Year : 
MNth(5) = "May-" &amp; Year.ToString : 
MNth(6) = "Jun-" &amp; Year.ToString : 
MNth(7) = "Jul-" &amp; Year.ToString 
MNth(8) = "Aug-" &amp; Year : 
MNth(9) = "Sep-" &amp; Year.ToString : 
MNth(10) = "Oct-" &amp; Year.ToString : 
MNth(11) = "Nov-" &amp; Year.ToString 
MNth(0) = "Dec-" &amp; Year : 
MNth(1) = "Jan-" &amp; (Year + 1).ToString : 
MNth(2) = "Feb-" &amp; (Year + 1).ToString : 
MNth(3) = "Mar-" &amp; (Year + 1).ToString
Dim J As Integer J = 4 ' k = 1 
  IdOfEmp = Val(DS.Tables(0).Rows(TotalCnt).Item("EmpId")) 
  While J &lt; 13 
    Try
      While IdOfEmp = Val(DS.Tables(0).Rows(TotalCnt).Item("EmpId")) 
        If J = Val(DS.Tables(0).Rows(TotalCnt).Item("MonthNumber")) 
          Then .PrintString(" | | | | | | | | |") : 
               .NewLine() : 
               .Tab(.LeftMargin)
        If J = 12 
          Then J = 0 
        End If 
       .PrintString(MNth(J), 9, DosPrint.Align.Left)


I'm not sure that the best approaches are being followed here, but ... I'm just trying to clean things up.
So many problems with this code.  The error is right here:

.PrintString(MNth(J), 9, DosPrint.Align.Left)


Since J is a number between 2 and 13 when it returns from sql and then in the set 0,2..11,13 after the if statement, when it is 13 MNth(13) will throw an array out of bounds.

The fix is in - add the following line (if statement for context.)

 J = J - 1  ' now J goes from 1-12
 If J = 12 
      Then J = 0 
 End If 




Another note (I can't resist pointing this one out but there are many...)

This part of your query:

Case 
When Month(P.FromDate) in(4,5,6,7,8,9,10,11,12) then  Month(P.FromDate)
when Month(P.FromDate) in(1,2,3) then  Month(P.FromDate)
end as Mnth


could be written as:

Month(P.FromDate) as Mnth


Since Month() is defined to return a value from 1..12