|
-
Nov 15th, 2005, 09:57 PM
#1
Thread Starter
Fanatic Member
reply clearly
Hi,
I Have Two Textboxes And One Button In My Form .
I Created Table Named As Emp Which Has Two Columns Such As Name ,age,
My Problem Is If I Click That Button One Row Only Retreiving,so I Created One More Button Named As Next And If I Click That Next Button All The Row Has To Be Retreive From The Table By Clicking That Next Button Each Time. I Connected My Database With Vb.net Using Odbc Connection.
So If I Click Next One Record Has To Fetch, Again If I Click Next Another Record Has To Fetch, I Need Coding For That.
Mendhak replied the following to do
Assuming you're using a dataset, create an integer variable which stores the current position in the dataset, like
TextBox1.Text = ds.Tables(0).Rows(i).Item(0).ToString()
Where i is the variable.
In your next button's click event, you can increment i and then redisplay the text in the textbox.
this reply is not clear and i am not using the dataset,please reply clearly
-
Nov 15th, 2005, 10:07 PM
#2
Re: reply clearly
Why are you creating another thread if one already exists for this question? Do you really think that the thread title will make people reply more clearly than they would otherwise?
If you want to retrieve the next row then you would need to get the primary key of the current row and execute a query similar to this:
Code:
SELECT TOP * FROM MyTable WHERE ID > {currentRowID}
where you need to supply the currentRowID. I must say though, it may be a better idea to get all the records in the first place then just display them from a local DataTable rather than accessing the database each time you want a row.
-
Nov 16th, 2005, 08:58 AM
#3
Re: reply clearly
 Originally Posted by karthikeyan
Hi,
I Have Two Textboxes And One Button In My Form .
I Created Table Named As Emp Which Has Two Columns Such As Name ,age,
My Problem Is If I Click That Button One Row Only Retreiving,so I Created One More Button Named As Next And If I Click That Next Button All The Row Has To Be Retreive From The Table By Clicking That Next Button Each Time. I Connected My Database With Vb.net Using Odbc Connection.
So If I Click Next One Record Has To Fetch, Again If I Click Next Another Record Has To Fetch, I Need Coding For That.
Mendhak replied the following to do
Assuming you're using a dataset, create an integer variable which stores the current position in the dataset, like
TextBox1.Text = ds.Tables(0).Rows(i).Item(0).ToString()
Where i is the variable.
In your next button's click event, you can increment i and then redisplay the text in the textbox.
this reply is not clear and i am not using the dataset,please reply clearly
Dude, why don't you just reply to the thread you started? You end up confusing everyone, we all have very simple minds.
-
Nov 16th, 2005, 09:25 AM
#4
Fanatic Member
Re: reply clearly
If you are just getting back a datatable with all the records, you can take a similar approach. At the top of the class, dim row as int32 = 0 and dim tblEmp as Datatable
Then when you display the first record, you can do something like:
txtAge.Text = tblEmp.rows(row)("Age")
Then when they hit next, it will just do
row += 1
if row = tblEmp.rows.count
row -= 1
end if
UpdateInfo()
UpdateInfo will just do:
txtAge.Text = tblEmp.rows(row)("Age")
...
For all the fields you are displaying.
-
Nov 16th, 2005, 10:15 AM
#5
Re: reply clearly
Try this one! Create a new VB.Net windows application project, then put 2 textboxes and 2 buttons on the form. Make your code window look like this, then run the project.
textbox1 = name
textbox2 = age
button1 = next record/row data
button2 = previous record/row data
Note that you'll need to change my connection string to point to your own database & the provider part if you're not using an MS Access database...
VB Code:
' To use this sample, write these lines as the very top lines of code in this class or moule.
Imports System.Data
Imports System.Data.OleDb
Public Class Form1
Inherits System.Windows.Forms.Form
" Windows Form Designer generated code "
' Variables available to each method (sub or function procedure) of this class.
Private m_dtNameAgeData As System.Data.DataTable
Private m_intNameAgeDataRowIndex As Integer
Private Sub Form1_Load1(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
Dim strOleDBAccessConnString As String
Dim cnnOleDBAccessConn As OleDbConnection
Dim daOleDBAccessDataAdapter As OleDbDataAdapter
Dim dsOleDBAccessDataSet As DataSet
Me.Refresh()
' Set the connection string of the database we want to access.
strOleDBAccessConnString = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
"data source=C:\DatabaseName.mdb"
' Open up a connection to this database.
cnnOleDBAccessConn = New OleDb.OleDbConnection(strOleDBAccessConnString)
cnnOleDBAccessConn.Open()
' Open up a dataadapter to run an SQL statement
' against this above-opened database connection.
daOleDBAccessDataAdapter = New OleDbDataAdapter("SELECT Name,Age FROM Emp", _
cnnOleDBAccessConn)
' We can then retreive these results into a dataset object.
dsOleDBAccessDataSet = New DataSet
daOleDBAccessDataAdapter.Fill(dsOleDBAccessDataSet)
daOleDBAccessDataAdapter.Dispose()
daOleDBAccessDataAdapter = Nothing
' Notice how I've been disposing of the variables I no longer need, as I'm
' going along here? We've got our data, so now we can close our connection
' to the database.
cnnOleDBAccessConn.Close()
cnnOleDBAccessConn.Dispose()
cnnOleDBAccessConn = Nothing
' Then take the results from the 1st table of this dataset object (ok, in
' this example we've only queried 1 table, so there's only going to be 1
' in the dataset), and store them in the variable "m_dtNameAgeData".
m_dtNameAgeData = dsOleDBAccessDataSet.Tables(0)
dsOleDBAccessDataSet.Dispose()
dsOleDBAccessDataSet = Nothing
' Set the index of the row to look at, to be the 1st one in our datatable.
' note how this is starting at 0, rather than 1.
m_intNameAgeDataRowIndex = 0
setWindowsFormControlValues(m_intNameAgeDataRowIndex)
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button1.Click
' If there is a record/row in the table following the current one, then set the
' row index to look at, to this value. Then call the below method to look at
' this row and populate the form's controls from it's values.
If Not (m_intNameAgeDataRowIndex >= m_dtNameAgeData.Rows.Count - 1) Then
m_intNameAgeDataRowIndex = m_intNameAgeDataRowIndex + 1
setWindowsFormControlValues(m_intNameAgeDataRowIndex)
End If
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button2.Click
' If there is a record/row in the table preceeding the current one, then set the
' row index to look at, to this value. Then call the below method to look at
' this row and populate the form's controls from it's values.
If Not (m_intNameAgeDataRowIndex <= 0) Then
m_intNameAgeDataRowIndex = m_intNameAgeDataRowIndex - 1
setWindowsFormControlValues(m_intNameAgeDataRowIndex)
End If
End Sub
Private Sub setWindowsFormControlValues(ByVal intRowIndexToUse As Integer)
' Populate the textboxes, with the details from the datatable row. This
' row to use is deciphered by the row index integer number passed into this
' sub. Note that I'm convertin the values from the datatable to string
' variable (data) types before putting them into the textboxes...
TextBox1.Text = CType(m_dtNameAgeData.Rows(intRowIndexToUse).Item("Name"), String)
TextBox2.Text = CType(m_dtNameAgeData.Rows(intRowIndexToUse).Item("Age"), String)
End Sub
End Class
-
Nov 16th, 2005, 11:01 AM
#6
Re: reply clearly
Or I guess another way of achieving this would be to bind the textboxes:
VB Code:
' To use this sample, write these lines as the very top lines of code in this class or moule.
Imports System.Data
Imports System.Data.OleDb
Public Class Form1
Inherits System.Windows.Forms.Form
" Windows Form Designer generated code "
' Variables available to each method (sub or function procedure) of this class.
Private m_dtNameAgeData As System.Data.DataTable
Private m_intNameAgeDataRowIndex As Integer
Private m_bmbFormControlBindMgr As BindingManagerBase
Private Sub Form1_Load1(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
Dim strOleDBAccessConnString As String
Dim cnnOleDBAccessConn As OleDbConnection
Dim daOleDBAccessDataAdapter As OleDbDataAdapter
Dim dsOleDBAccessDataSet As DataSet
Me.Refresh()
' Set the connection string of the database we want to access.
strOleDBAccessConnString = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
"data source=C:\DatabaseName.mdb"
' Open up a connection to this database.
cnnOleDBAccessConn = New OleDb.OleDbConnection(strOleDBAccessConnString)
cnnOleDBAccessConn.Open()
' Open up a dataadapter to run an SQL statement
' against this above-opened database connection.
daOleDBAccessDataAdapter = New OleDbDataAdapter("SELECT Name,Age FROM Emp", _
cnnOleDBAccessConn)
' We can then retreive these results into a dataset object.
dsOleDBAccessDataSet = New DataSet
daOleDBAccessDataAdapter.Fill(dsOleDBAccessDataSet)
daOleDBAccessDataAdapter.Dispose()
daOleDBAccessDataAdapter = Nothing
' Notice how I've been disposing of the variables I no longer need, as I'm
' going along here? We've got our data, so now we can close our connection
' to the database.
cnnOleDBAccessConn.Close()
cnnOleDBAccessConn.Dispose()
cnnOleDBAccessConn = Nothing
' Then take the results from the 1st table of this dataset object (ok, in
' this example we've only queried 1 table, so there's only going to be 1
' in the dataset), and store them in the variable "m_dtNameAgeData".
m_dtNameAgeData = dsOleDBAccessDataSet.Tables(0)
dsOleDBAccessDataSet.Dispose()
dsOleDBAccessDataSet = Nothing
' Bind the textboxes to the datatable, then set an object to manage this binding.
TextBox1.DataBindings.Add(New Binding("Text", m_dtNameAgeData, "Name"))
TextBox2.DataBindings.Add(New Binding("Text", m_dtNameAgeData, "Age"))
m_bmbFormControlBindMgr = Me.BindingContext(m_dtNameAgeData)
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button1.Click
' If there is a record/row in the table following the current one, then increment
' the position of the datatable looked at by the m_bmbFormControlBindMgr object.
' As the textbox controls are bound to this table, they will update.
If Not (m_bmbFormControlBindMgr.Position >= m_dtNameAgeData.Rows.Count - 1) Then
m_bmbFormControlBindMgr.Position += 1
End If
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button2.Click
' Does the reverse of button 1.
If Not (m_bmbFormControlBindMgr.Position <= 0) Then
m_bmbFormControlBindMgr.Position -= 1
End If
End Sub
End Class
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|