Dim str As String = "select * from table1"
'try to write "select distinct * from table1"
Dim con As String = ConfigurationSettings.AppSettings("preeconn")
Dim cmd As New SqlCommand(str, New SqlConnection(con))
cmd.Connection.Open()
Dim da As New SqlDataAdapter(str, con)
Dim ds As New DataSet
da.Fill(ds, "table1")
Dim maxrws As Integer
Dim i As Integer
maxrws = ds.Tables("table1").Rows.Count 'used to get the count of number of rows in a table
i = 0
For i = 0 To maxrws - 1
id_txt.Text = ds.Tables("table1").Rows(i).Item("ID").ToString()
name_txt.Text = ds.Tables("table1").Rows(i).Item("Name").ToString()
age_txt.Text = ds.Tables("table1").Rows(i).Item("Age").ToString()
sex_txt.Text = ds.Tables("table1").Rows(i).Item("Sex").ToString()
area_txt.Text = ds.Tables("table1").Rows(i).Item("Area").ToString()
Next i
name_txt.Text = ds.Tables("table1").Rows(i).Item("Name").ToString()
age_txt.Text = ds.Tables("table1").Rows(i).Item("Age").ToString()
sex_txt.Text = ds.Tables("table1").Rows(i).Item("Sex").ToString()
area_txt.Text = ds.Tables("table1").Rows(i).Item("Area").ToString()
----------------this is codin under next_button click.but at the moment its displayin only first record from the database on the form..plz tell m whats the problem...

Recommended Answers

All 6 Replies

this code is under next button click ?

have you checked what is stored in maxrws ?

what integer value does it contain ?

Ok
Your code won't display the next record , it will always display the same record over and over , any way I think it’s the last one in table not the first.
The problem is u not keep track of which the next record is ! the for loop here dose NOT do that too.
To solve this, first define global variable

Public next_record As Integer

Double click Next Record button to access the code and write this

If next_record <= maxrws - 1  Then
id_txt.Text = ds.Tables("table1").Rows(i).Item("ID").ToString()
name_txt.Text = ds.Tables("table1").Rows(i).Item("Name").ToString()
age_txt.Text = ds.Tables("table1").Rows(i).Item("Age").ToString()
sex_txt.Text = ds.Tables("table1").Rows(i).Item("Sex").ToString()
area_txt.Text = ds.Tables("table1").Rows(i).Item("Area").ToString()
next_record = next_record + 1    ' here we keep track of which the Next record is
Else                 
   …………………    ' write what u want to do when it reach the last record ,  maybe to start all over again or anything 


 End If

Good luck

thanks for the quick reply for both of u,
but i dont c any changes-inspite i declare it has global variables& else if i use while-loop instead of if-loop...(And also it returns maxrws=26)
plz helpmm

but i dont c any changes-inspite i declare it has global variables & else if i use while-loop instead of if-loop And also it returns maxrws=26)
plz helpmm

first of all if is NOT loop u use it to test condition and i do not know why u using the for or while loop !
in ur code each time u press next button what will happen is
1/ make connection to database
2/get number of raws in table
3/goes through ALL the record but u actually display the same one each time .

but when u define global varaible , each time user press next button it will incremnt by one so u keep track of the next record . and NO need for for loop .

i hope its clear now , :S

easiest way is to store your data in a multidimensional array on the page load.

then when a user presses a button i.e. next record just increment the array value. the same idea is used for getting values from a database for every request.

you need some way of storing where you are at in the list of records that are returned from the db. And you wont want to loop through with a for loop everytime. if u change that for loop to add the data to an array then use the array youll find its alot easier

yes,sir
I understood what u said,but inspite i declare global variable&also i dont use loops-im n2 able to access the records--
can u plz give m details to how to write the code...
thanks..

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.