Click here to Skip to main content
15,891,513 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to Read a Specific column . this is my code. It Reads my AfternoonTime-In column from Table_DTR whether it has data on it, but it keeps on Displaying "Has Rows" even if there is nothing in . how can I fix this ?

What I have tried:

VB
Connect = New SqlConnection(ConnectionString)
       Connect.Open()

       Dim Query1 As String = "Select  [AfternoonTime-Out] From Table_DTR Where Date = @Date and EmployeeID = @EmpID "
       Dim cmd1 As SqlCommand = New SqlCommand(Query1, Connect)
       cmd1.Parameters.AddWithValue("@Date", DTRform.datetoday.Text)
       cmd1.Parameters.AddWithValue("@EmpID", DTRform.DTRempID.Text)


       Using Reader As SqlDataReader = cmd1.ExecuteReader()
           If Reader.HasRows Then
               MsgBox("Has rows")
               Reader.Close()

           Else
               MsgBox("empty")
           End If
       End Using
Posted
Updated 9-Oct-16 8:21am
Comments
[no name] 9-Oct-16 14:23pm    
You put a breakpoint on your function, step through it and see what the reader has in the way of data. We wouldn't have any idea what you have in your database.
Member 12723543 9-Oct-16 15:02pm    
how do I do it ?

1 solution

If the HasRows property returns true, the result set most likely really has rows. However, initially you're not positioned in the first row so if you try to fetch the values, you'll get an exception.

Use the Read mehtod[^] to advance in the result set and see what it contains.

To clarify the situation it could be helpful if you include columns Date and emplyoeeid to the result set.

Also notice that since date is a reserved word the column name should be surrounded with brackets: .. WHERE [Date] = ...

And one more thing. The result set will contain rows as long as the WHERE condition is satisfied, regardless if the AfternoonTime-Out column as a value or not. You can see if the column contains a NULL value by comparing it to DBNull.Value [^] in C#. If you want to exclude null rows, you need to modify the WHERE clause, for example:
VB
Dim Query1 As String = "Select  [AfternoonTime-Out] From Table_DTR Where Date = @Date and EmployeeID = @EmpID AND [AfternoonTime-Out] IS NOT NULL"
 
Share this answer
 
v3
Comments
Member 12723543 9-Oct-16 15:07pm    
what is the most possible way to achieve it ? I hava no idea .
Wendelius 9-Oct-16 23:21pm    
What do you want to achieve?

If you just want to eliminate the NULL records from the result set, add the IS NOT NULL condition to the where clause.

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