Click here to Skip to main content
15,881,139 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I am Trying to Get the Data from the column [MorningTime-In],[AfternoonTime-In] in the table Table_DTR . its Datatype is time(7) but I keep on getting an Error message: System.IndexOutOfRangeExcemption:[MorningTime-In] .

What I have tried:

C#
Dim timeInAm As DateTime
Dim timeOutAm As Date
Dim timeInPm As Date
Dim timeOutPm As Date


Dim xAm As Date = Date.Parse("08:00")
Dim yAm As Date = Date.Parse("12:00")
Dim xPm As Date = Date.Parse("01:00")
Dim yPm As Date = Date.Parse("05:00")
Try
    Connect = New SqlConnection(ConnectionString)
    Connect.Open()

    Dim dt = Convert.ToDateTime(DTRform.datetoday.Text)
    Dim id = Convert.ToInt32(DTRform.DTRempID.Text)

    Using Connect = New SqlConnection(ConnectionString)
        Connect.Open()
        Dim Query1 As String = "Select * From Table_DTR
                   Where Date = @Date and EmployeeID = @EmpID"
        Dim cmd1 As SqlCommand = New SqlCommand(Query1, Connect)
        cmd1.Parameters.Add("@Date", SqlDbType.DateTime).Value = dt
        cmd1.Parameters.Add("@EmpID", SqlDbType.Int).Value = id
        Using Reader As SqlDataReader = cmd1.ExecuteReader()
            Do While Reader.Read()
                timeInAm = Reader("MorningTime-In").ToString()
                timeOutAm = Reader("MorningTime-Out").ToString()
                timeInPm = Reader("AfternoonTime-In").ToString()
                timeOutPm = Reader("AfternoonTime-Out").ToString()



            Loop


        End Using
    End Using
Posted
Updated 10-Oct-16 3:06am
v2

1 solution

The angle brackets, [], are used in SQL Server to surround names in SQL Server that contains special characters or are reserved words. However when the column is returned to the client, the actual name of the column is used.

So try the following:
VB
timeInAm = Reader("MorningTime-In").ToString()

Also it looks like you're storing a time in a string variable. If that's the case, I would recommend using System.Datetime[^] which makes formatting, calculations, conversions, and so on much easier
 
Share this answer
 
Comments
Member 12723543 10-Oct-16 8:59am    
the timeInAm variable their is a Datetime type already . now its having an error of "Conversion from string "" to type 'Date' is not valid. " I want to get the data from that column to be subtracted from the AfternoonTime-In column . how can I do it ?
Wendelius 10-Oct-16 9:08am    
If the receiving variable is DateTime, don't use solely ToString method, instead use DateTime.Parse Method (System)[^] or simply cast the object from the datareader to DateTime using CType Function (Visual Basic)[^]
Wendelius 10-Oct-16 9:10am    
Also if you need to calculate difference between two datetime's, use DateTime.Subtract Method (System)[^]

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900