The dr.read on the below returns false in if cionditon but return true in while loop. why. this is my program
Imports System.Data.OleDb
Public Class Form1
Dim cn As OleDbConnection
Dim cmd As OleDbCommand
Dim dr As OleDbDataReader
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Try
cn = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Documents and Settings\user\My Documents\school.mdb")
cn.Open()
Dim qry As String
qry = "select name from stud where rollno=" & TextBox1.Text
cmd = New OleDbCommand(qry, cn)
dr = cmd.ExecuteReader
If dr.Read() Then
' code a
While dr.Read()
TextBox2.Text = dr.Item("name")
End While
' a ends
Else
MsgBox("error")
End If
Catch ex As Exception
MsgBox(ex.Message)
End Try
dr.Close()
cn.Close()
End Sub
End Class
When i execute and enter vaild rollno on textbox1 the textbox2 is empty that is nothing happen when click button1. but when i use only the while not using if that is code block a (showed in comment above while loop) textbox2 display the name .
My qus is why dr.read shows false in if
Imports System.Data.OleDb
Public Class Form1
Dim cn As OleDbConnection
Dim cmd As OleDbCommand
Dim dr As OleDbDataReader
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Try
cn = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Documents and Settings\user\My Documents\school.mdb")
cn.Open()
Dim qry As String
qry = "select name from stud where rollno=" & TextBox1.Text
cmd = New OleDbCommand(qry, cn)
dr = cmd.ExecuteReader
If dr.Read() Then
' code a
While dr.Read()
TextBox2.Text = dr.Item("name")
End While
' a ends
Else
MsgBox("error")
End If
Catch ex As Exception
MsgBox(ex.Message)
End Try
dr.Close()
cn.Close()
End Sub
End Class
When i execute and enter vaild rollno on textbox1 the textbox2 is empty that is nothing happen when click button1. but when i use only the while not using if that is code block a (showed in comment above while loop) textbox2 display the name .
My qus is why dr.read shows false in if