William Vaughn
3/11/2008 1:49:00 AM
Add
If MyDataReader.HasRows Then...
--
__________________________________________________________________________
William R. Vaughn
President and Founder Beta V Corporation
Author, Mentor, Dad, Grandpa
Microsoft MVP
(425) 556-9205 (Pacific time)
Hitchhiker???s Guide to Visual Studio and SQL Server (7th Edition)
____________________________________________________________________________________________
"Pramod" <Pramod @discussions.microsoft.com> wrote in message
news:523A9A4C-D2AB-418C-9B03-19F83CB189EC@microsoft.com...
> The error message "Invalid attempt to read when no data is present"
> it arises in a While executing datareader when the table Or Rows which
> is read is
> empty
> Since the read property return a Boolean value Either true or false
>
> Just Assigin like this
> when i got The Same error i rectified it as follows
> rdVacationInfo = cmdVaccation.ExecuteReader
> rdVacationInfo.Read()
> If rdVacationInfo.Read = True Then
> If Not (IsDBNull(rdVacationInfo.Item("leavefrom"))) Then
> LeaveFrom = rdVacationInfo.Item("leavefrom")
> End If
> /////--------------------------------/////
> i will show an example
>
> Private Function GetleaveDays(ByVal EmpId As Integer)
> Try
> EmpOnLeave = False
> LeaveFrom = DummyDate.Date
> LeaveTo = DummyDate.Date
> Dim cnvaccation As New
> SqlClient.SqlConnection(ConnectionString)
> If cnvaccation.State = ConnectionState.Closed Then
> cnvaccation.Open()
> Dim cmdVaccation As New SqlClient.SqlCommand
>
> With cmdVaccation
> .CommandType = CommandType.StoredProcedure
> .CommandText = "Pr_GetVaccationDays"
> .CommandTimeout = 0
> .Connection = cnvaccation
> .Parameters.Clear()
> .Parameters.Add("@EmpID", EmpId)
> End With
> rdVacationInfo = cmdVaccation.ExecuteReader
> rdVacationInfo.Read()
> If rdVacationInfo.Read = True Then
> If Not (IsDBNull(rdVacationInfo.Item("leavefrom")))
> Then
> LeaveFrom = rdVacationInfo.Item("leavefrom")
> End If
> If Not (IsDBNull(rdVacationInfo.Item("leaveTo"))) Then
> LeaveTo = rdVacationInfo.Item("leaveTo")
> End If
> rdVacationInfo.Close()
> Else
> LeaveFrom = DummyDate.Date
> LeaveTo = DummyDate.Date
> End If
> End If
> Return LeaveFrom & "" & LeaveTo
> Catch ex As Exception
> MsgBox(ex.Message.ToString)
> End Try
> End Function
>
> Regards
> Pramod
> s/w Engineer
> Group Harwal
>
>