Results 1 to 6 of 6

Thread: reply clearly

  1. #1

    Thread Starter
    Fanatic Member karthikeyan's Avatar
    Join Date
    Oct 2005
    Location
    inside .net
    Posts
    919

    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

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: reply clearly

    Quote 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.

  4. #4
    Fanatic Member MetalKid's Avatar
    Join Date
    Aug 2005
    Location
    Green Bay, Wisconsin
    Posts
    534

    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.

  5. #5
    Evil Genius alex_read's Avatar
    Join Date
    May 2000
    Location
    Espoo, Finland
    Posts
    5,538

    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:
    1. ' To use this sample, write these lines as the very top lines of code in this class or moule.
    2. Imports System.Data
    3. Imports System.Data.OleDb
    4.  
    5. Public Class Form1
    6.     Inherits System.Windows.Forms.Form
    7.  
    8. " Windows Form Designer generated code "
    9.  
    10.  
    11.     ' Variables available to each method (sub or function procedure) of this class.
    12.     Private m_dtNameAgeData As System.Data.DataTable
    13.     Private m_intNameAgeDataRowIndex As Integer
    14.  
    15.  
    16.  
    17.     Private Sub Form1_Load1(ByVal sender As Object, _
    18.     ByVal e As System.EventArgs) Handles MyBase.Load
    19.         Dim strOleDBAccessConnString As String
    20.         Dim cnnOleDBAccessConn As OleDbConnection
    21.         Dim daOleDBAccessDataAdapter As OleDbDataAdapter
    22.         Dim dsOleDBAccessDataSet As DataSet
    23.  
    24.         Me.Refresh()
    25.  
    26.         ' Set the connection string of the database we want to access.
    27.         strOleDBAccessConnString = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
    28.         "data source=C:\DatabaseName.mdb"
    29.  
    30.         ' Open up a connection to this database.
    31.         cnnOleDBAccessConn = New OleDb.OleDbConnection(strOleDBAccessConnString)
    32.         cnnOleDBAccessConn.Open()
    33.  
    34.         ' Open up a dataadapter to run an SQL statement
    35.         ' against this above-opened database connection.
    36.         daOleDBAccessDataAdapter = New OleDbDataAdapter("SELECT Name,Age FROM Emp", _
    37.         cnnOleDBAccessConn)
    38.  
    39.         ' We can then retreive these results into a dataset object.
    40.         dsOleDBAccessDataSet = New DataSet
    41.         daOleDBAccessDataAdapter.Fill(dsOleDBAccessDataSet)
    42.         daOleDBAccessDataAdapter.Dispose()
    43.         daOleDBAccessDataAdapter = Nothing
    44.  
    45.         ' Notice how I've been disposing of the variables I no longer need, as I'm
    46.         ' going along here? We've got our data, so now we can close our connection
    47.         ' to the database.
    48.         cnnOleDBAccessConn.Close()
    49.         cnnOleDBAccessConn.Dispose()
    50.         cnnOleDBAccessConn = Nothing
    51.  
    52.         ' Then take the results from the 1st table of this dataset object (ok, in
    53.         ' this example we've only queried 1 table, so there's only going to be 1
    54.         ' in the dataset), and store them in the variable "m_dtNameAgeData".
    55.         m_dtNameAgeData = dsOleDBAccessDataSet.Tables(0)
    56.         dsOleDBAccessDataSet.Dispose()
    57.         dsOleDBAccessDataSet = Nothing
    58.  
    59.         ' Set the index of the row to look at, to be the 1st one in our datatable.
    60.         ' note how this is starting at 0, rather than 1.
    61.         m_intNameAgeDataRowIndex = 0
    62.         setWindowsFormControlValues(m_intNameAgeDataRowIndex)
    63.     End Sub
    64.  
    65.  
    66.     Private Sub Button1_Click(ByVal sender As System.Object, _
    67.     ByVal e As System.EventArgs) Handles Button1.Click
    68.         ' If there is a record/row in the table following the current one, then set the  
    69.         ' row index to look at, to this value. Then call the below method to look at
    70.         ' this row and populate the form's controls from it's values.
    71.         If Not (m_intNameAgeDataRowIndex >= m_dtNameAgeData.Rows.Count - 1) Then
    72.             m_intNameAgeDataRowIndex = m_intNameAgeDataRowIndex + 1
    73.             setWindowsFormControlValues(m_intNameAgeDataRowIndex)
    74.         End If
    75.     End Sub
    76.  
    77.  
    78.     Private Sub Button2_Click(ByVal sender As System.Object, _
    79.     ByVal e As System.EventArgs) Handles Button2.Click
    80.         ' If there is a record/row in the table preceeding the current one, then set the  
    81.         ' row index to look at, to this value. Then call the below method to look at
    82.         ' this row and populate the form's controls from it's values.
    83.         If Not (m_intNameAgeDataRowIndex <= 0) Then
    84.             m_intNameAgeDataRowIndex = m_intNameAgeDataRowIndex - 1
    85.             setWindowsFormControlValues(m_intNameAgeDataRowIndex)
    86.         End If
    87.     End Sub
    88.  
    89.  
    90.     Private Sub setWindowsFormControlValues(ByVal intRowIndexToUse As Integer)
    91.         ' Populate the textboxes, with the details from the datatable row. This
    92.         ' row to use is deciphered by the row index integer number passed into this
    93.         ' sub. Note that I'm convertin the values from the datatable to string
    94.         ' variable (data) types before putting them into the textboxes...
    95.         TextBox1.Text = CType(m_dtNameAgeData.Rows(intRowIndexToUse).Item("Name"), String)
    96.         TextBox2.Text = CType(m_dtNameAgeData.Rows(intRowIndexToUse).Item("Age"), String)
    97.     End Sub
    98.  
    99.  
    100. End Class

    Please rate this post if it was useful for you!
    Please try to search before creating a new post,
    Please format code using [ code ][ /code ], and
    Post sample code, error details & problem details

  6. #6
    Evil Genius alex_read's Avatar
    Join Date
    May 2000
    Location
    Espoo, Finland
    Posts
    5,538

    Re: reply clearly

    Or I guess another way of achieving this would be to bind the textboxes:
    VB Code:
    1. ' To use this sample, write these lines as the very top lines of code in this class or moule.
    2. Imports System.Data
    3. Imports System.Data.OleDb
    4.  
    5.  
    6. Public Class Form1
    7.     Inherits System.Windows.Forms.Form
    8.  
    9.     " Windows Form Designer generated code "
    10.  
    11.  
    12.     ' Variables available to each method (sub or function procedure) of this class.
    13.     Private m_dtNameAgeData As System.Data.DataTable
    14.     Private m_intNameAgeDataRowIndex As Integer
    15.     Private m_bmbFormControlBindMgr As BindingManagerBase
    16.  
    17.     Private Sub Form1_Load1(ByVal sender As Object, _
    18.     ByVal e As System.EventArgs) Handles MyBase.Load
    19.         Dim strOleDBAccessConnString As String
    20.         Dim cnnOleDBAccessConn As OleDbConnection
    21.         Dim daOleDBAccessDataAdapter As OleDbDataAdapter
    22.         Dim dsOleDBAccessDataSet As DataSet
    23.  
    24.         Me.Refresh()
    25.  
    26.         ' Set the connection string of the database we want to access.
    27.         strOleDBAccessConnString = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
    28.         "data source=C:\DatabaseName.mdb"
    29.  
    30.         ' Open up a connection to this database.
    31.         cnnOleDBAccessConn = New OleDb.OleDbConnection(strOleDBAccessConnString)
    32.         cnnOleDBAccessConn.Open()
    33.  
    34.         ' Open up a dataadapter to run an SQL statement
    35.         ' against this above-opened database connection.
    36.         daOleDBAccessDataAdapter = New OleDbDataAdapter("SELECT Name,Age FROM Emp", _
    37.         cnnOleDBAccessConn)
    38.  
    39.         ' We can then retreive these results into a dataset object.
    40.         dsOleDBAccessDataSet = New DataSet
    41.         daOleDBAccessDataAdapter.Fill(dsOleDBAccessDataSet)
    42.         daOleDBAccessDataAdapter.Dispose()
    43.         daOleDBAccessDataAdapter = Nothing
    44.  
    45.         ' Notice how I've been disposing of the variables I no longer need, as I'm
    46.         ' going along here? We've got our data, so now we can close our connection
    47.         ' to the database.
    48.         cnnOleDBAccessConn.Close()
    49.         cnnOleDBAccessConn.Dispose()
    50.         cnnOleDBAccessConn = Nothing
    51.  
    52.         ' Then take the results from the 1st table of this dataset object (ok, in
    53.         ' this example we've only queried 1 table, so there's only going to be 1
    54.         ' in the dataset), and store them in the variable "m_dtNameAgeData".
    55.         m_dtNameAgeData = dsOleDBAccessDataSet.Tables(0)
    56.         dsOleDBAccessDataSet.Dispose()
    57.         dsOleDBAccessDataSet = Nothing
    58.  
    59.         ' Bind the textboxes to the datatable, then set an object to manage this binding.
    60.         TextBox1.DataBindings.Add(New Binding("Text", m_dtNameAgeData, "Name"))
    61.         TextBox2.DataBindings.Add(New Binding("Text", m_dtNameAgeData, "Age"))
    62.         m_bmbFormControlBindMgr = Me.BindingContext(m_dtNameAgeData)
    63.     End Sub
    64.  
    65.  
    66.     Private Sub Button1_Click(ByVal sender As System.Object, _
    67.     ByVal e As System.EventArgs) Handles Button1.Click
    68.         ' If there is a record/row in the table following the current one, then increment
    69.         ' the position of the datatable looked at by the m_bmbFormControlBindMgr object.
    70.         ' As the textbox controls are bound to this table, they will update.
    71.         If Not (m_bmbFormControlBindMgr.Position >= m_dtNameAgeData.Rows.Count - 1) Then
    72.             m_bmbFormControlBindMgr.Position += 1
    73.         End If
    74.     End Sub
    75.  
    76.  
    77.     Private Sub Button2_Click(ByVal sender As System.Object, _
    78.     ByVal e As System.EventArgs) Handles Button2.Click
    79.         ' Does the reverse of button 1.
    80.         If Not (m_bmbFormControlBindMgr.Position <= 0) Then
    81.             m_bmbFormControlBindMgr.Position -= 1
    82.         End If
    83.     End Sub
    84.  
    85.  
    86. End Class

    Please rate this post if it was useful for you!
    Please try to search before creating a new post,
    Please format code using [ code ][ /code ], and
    Post sample code, error details & problem details

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width