ado.net newbie question

  • 17 years ago

    how do you implement this on ado.net


    recordcount
    movefirst
    movenext
    moveprevious
    movelast


    will i use the datareader or dataset


    and lastly how do you fill the datagrid with all the records in your database.


    thanks...

  • 17 years ago

    Hi,
    Check out this
    Use Databinding context to bind your controls with the Dataset's table or view


    Here is the sample code



       Private _curManager As CurrencyManager ' declare it globally on top of ur form


     Private Sub BindControls()
           Dim dtSample As New DataTable
           
    dtSample= Dataset.Tables(0) '<< Write your dataset's table here


              txtGroupName.DataBindings.Add(New Binding("Text", dtSample, "GroupName"))
              _curManager = CType(Me.BindingContext(
    dtSample), CurrencyManager)
              _curManager.Position = 0
     End Sub



    Use Currency manager to Navigate your records


    Here is the sample - code




    Public Sub MoveToNextRecord(ByVal CurMgr As CurrencyManager)
           If CurMgr.Position = CurMgr.Count - 1 Then
               'MessageBox.Show("You are at end of the records.", Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Information)
           Else
               CurMgr.Position += 1
           End If
       End Sub


       Public Sub MoveToFirstRecord(ByVal CurMgr As CurrencyManager)
           CurMgr.Position = 0
       End Sub


       Public Sub MoveToLastRecord(ByVal CurMgr As CurrencyManager)
           CurMgr.Position = CurMgr.Count - 1
       End Sub


       Public Sub MoveToPrevRecord(ByVal CurMgr As CurrencyManager)
           If CurMgr.Position = 0 Then
               'MessageBox.Show("You are at the beginning of the records.", Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Information)
           Else
               CurMgr.Position -= 1
           End If
       End Sub



    Binding the Grid with Datasource




     Private Sub BindGrid()
       Dim dvMenu as dataview
       
    dvMenu = Dataset.tables(0).DefaultViewManager
           With <DataGridName>
               .CaptionText = "XXXX"
               .DataSource = _dvMenu
           End With


           Dim grdTableStyle1 As New DataGridTableStyle
           With grdTableStyle1
               .MappingName = _dvMenu.Table.TableName
               .AlternatingBackColor = System.Drawing.Color.Silver
               .BackColor = System.Drawing.Color.White
               .GridLineStyle = System.Windows.Forms.DataGridLineStyle.None
               .GridLineColor = Color.White
           End With
           If grdTableStyle1.RowHeadersVisible = True Then grdTableStyle1.RowHeaderWidth = 10 'grdTableStyle1.RowHeadersVisible = False
           Dim grdCol1 As New DataGridTextBoxColumn
           With grdCol1
               .MappingName = "MENUID"
               .HeaderText = "Menu ID"
               .Width = 0
               .ReadOnly = True
           End With
           Dim grdCol2 As New DataGridTextBoxColumn
           With grdCol2
               .MappingName = "MENUNAME"
               .HeaderText = "Menu Name"
               .Width = 180
               '.Format = "c"
               .ReadOnly = True
           End With
           Dim grdCol3 As New DataGridTextBoxColumn
           With grdCol3
               .MappingName = "FORMID"
               .HeaderText = "Form Id"
               .Width = 0
               .ReadOnly = True
           End With
           grdTableStyle1.GridColumnStyles.AddRange(New DataGridColumnStyle() {grdCol1, grdCol2, grdCol14, grdCol3})
           <YourDataGridName>.TableStyles.Add(grdTableStyle1)
       End Sub


    Record Count



    Dataset.Tables(0).Rows.Count


    DonRamesh

  • 17 years ago

    sir is that the easiest way .... im kinda confuse with the code




    Public Sub MoveToNextRecord(ByVal CurMgr As CurrencyManager)
          If CurMgr.Position = CurMgr.Count - 1 Then
              'MessageBox.Show("You are at end of the records.", Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Information)
          Else
              CurMgr.Position += 1
          End If
      End Sub


    sir are this codes a procedure or an event of a button


    what is a currencymanager


    thanks....

  • 17 years ago

    Hi,
    I feel this is the coolest way.
    Currency manager is basically to manage the list of Binding objects. u can get more clarity if u go through the MSDN doc for currencymanger.


    See, the objective is very simple,
    1. Assume that u have got a Dataset
    2. From that dataset extract a datatable
    3. Now Bind the Control's Text, Tag, or Value property (depends on the type of control) with the Object's (ur data table) property (this is your column name of the tables )
    eg.
      TextBox1.DataBindings.Add(New Binding("Text", <ur DataTable>, "Col Name"))


    This is basically the binding of Control's property (i.e., TextBox1) with objects's property (DataTable's Column Name)


    This list of binding in ur form is managed by a CurrencyManager
    Once after the binding with the control is over, now set ur currency manager


    Eg.
      myCurrencyManager= CType(Me.BindingContext(<ur DataTable&gt, CurrencyManager)
    'Set the initial position  
      _myCurrencyManager.Position = 0


    I hope this would clarify u.


    All codes are procedures.


    DonRamesh

Post a reply

Enter your message below

Sign in or Join us (it's free).

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“Perl - The only language that looks the same before and after RSA encryption.” - Keith Bostic