Click here to Skip to main content
15,890,527 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all,

I am able to run this code but instead the record goes to last item than second.

Dim sqlconn As New SqlClient.SqlConnection

sqlconn.ConnectionString = "Data Source=C8067;Initial Catalog=msdb;Integrated Security=True"
sqlconn.Open()
Dim da As New SqlDataAdapter("select * from regions", sqlconn)
Dim ds As New DataSet
da.Fill(ds)
For i As Integer = 1 To 4

txtRegion_ID.Text = ds.Tables(0).Rows(i).Item(0).ToString
txtRegion_Name.Text = ds.Tables(0).Rows(i).Item(1).ToString

Next

sqlconn.Close()
Posted

1 solution

Your code
C#
For i As Integer = 1 To 4

txtRegion_ID.Text = ds.Tables(0).Rows(i).Item(0).ToString
txtRegion_Name.Text = ds.Tables(0).Rows(i).Item(1).ToString

Next

is running on every record.

Step out after the second record and you will have only the second record displayed.

C#
For i As Integer = 1 To 4

txtRegion_ID.Text = ds.Tables(0).Rows(i).Item(0).ToString
txtRegion_Name.Text = ds.Tables(0).Rows(i).Item(1).ToString

if i = 2 then break
end if

Next
 
Share this answer
 
v3
Comments
vivianpinto 4-Jan-16 22:59pm    
Hi Abhinav.

Thank you very much for quick response.

I did not present my question properly sorry for that.

What actually i want is SQL Move first, move next Move last Move previous operation.

I am beginning with Move next operation, so i am giving a try for 5 records.

You are exactly right. the loop is running on every record.

i want only one increment in "i" every time the button is pressed.


Protected Sub btnNext_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnNext.Click
Dim sqlconn As New SqlClient.SqlConnection

sqlconn.ConnectionString = "Data Source=C8067;Initial Catalog=msdb;Integrated Security=True"
sqlconn.Open()
Dim da As New SqlDataAdapter("select * from regions", sqlconn)
Dim ds As New DataSet
da.Fill(ds)d


For i As Integer = ( index of current record) To (end of record)

txtRegion_ID.Text = ds.Tables(0).Rows(i).Item(0).ToString
txtRegion_Name.Text = ds.Tables(0).Rows(i).Item(1).ToString
If reaching last record msg box" end of record"
End If
Next

sqlconn.Close()

End Sub

Thank you.

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