Click here to Skip to main content
15,895,746 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
Hi all,

I have a problem.

Situation:
I have declared a string to hold the data from a query
VB
strSQL=GetPayment(strDateFrm,strDateTo)
cnCMD = New OleDbCommand(strSQL, cnDB)
cnCMD.CommandType = CommandType.Text
rdr = cnCMD.ExecuteReader
If Not (IsNothing(strSQL)) Then
                        If rdr.Read Then  -------> "problem occurs here"
                            While rdr.Read

I want to ask anyone who can solve me for:
If data from database is empty, what should I do? Because when data is not exist in database, it will fill empty to DataSet and DataReader will find nothing and skip the step where I put a condition if nothing in database, it will fill "-NA-" string value.

Or can anyone tell me the other code for inserting data from database to DataSet.
I'm confused about the answer that I found in search engine because mostly they put the data into something such as ListView, DataGridView.
It confuses me.

I use an object objPayment as medium to retrieve data from database and fill in DataSet column.
For example:
VB
If Not (IsDBNull(rdr("intMPAJRecpNo"))) Then
         objChqDataRow.strReceiptNo = rdr("intMPAJRecpNo")
Else
         objChqDataRow.strReceiptNo = "- NA -"
End If

If someone has the other way from my way to manipulate data from using DataReader, kindly help me.
Posted
Updated 13-Nov-11 4:13am
v2

1 solution

You should first test that your rdr has rows.

Typically I write:
VB
While rdr.HasRows ANDALSO rdr.Read
'... code ...
End While


If you do this:
VB
If rdr.Read Then 
    While rdr.Read


You are actually skipping a row as you are calling .Read twice (Once on IF and again entering your WHILE).
 
Share this answer
 
v2

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