Results 1 to 4 of 4

Thread: Re: Retrieving and Saving Data in Databases

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Jan 2008
    Location
    United Kingdom
    Posts
    168

    Re: Retrieving and Saving Data in Databases

    This is what I have so far, it just displays the data in 3 datagridviews. I am not too sure what to do next with regards to making command object to select a customer from a DGV and stock item from a DGV to create a booking.

    Code:
    Public Class frm_bookings
        Inherits System.Windows.Forms.Form
    
        Dim connectionString As String = "Integrated Security=SSPI;Persist Security Info=False;" & "Initial Catalog=bookingsdb;Data Source=MY-PC\SQLEXPRESS"
        Dim conn As New SqlConnection(connectionString) 'define the connection as an sql connection
        Dim ds As New DataSet 'give the dataset the name of "ds"
        Dim dataTable As DataTable 'give the datatable the name of datatable
    
        Dim CustomersDataAdapter As New SqlDataAdapter("SELECT * from tblcustomers", conn)
        Dim CustomersBindingSource As New BindingSource() 'for tblcustomers
    
        Dim StockDataAdapter As New SqlDataAdapter("SELECT * from tblstock", conn)
        Dim StockBindingSource As New BindingSource() 'for tblstock
    
        Dim BookingsDataAdapter As New SqlDataAdapter("SELECT * from tblbookings", conn)
        Dim BookingsBindingSource As New BindingSource() 'for tblbookings
    
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    
            CustomersDataAdapter.MissingSchemaAction = MissingSchemaAction.AddWithKey
            CustomersDataAdapter.Fill(ds, "tblcustomers")
            dataTable = ds.Tables("tblcustomers") 'When the form loads, the datatable uses the "tblcustomers" table.
            CustomersBindingSource.DataSource = ds
            CustomersBindingSource.DataMember = "tblCustomers"
            dgvCustomers.DataSource = CustomersBindingSource
            dgvCustomers.SelectionMode = DataGridViewSelectionMode.FullRowSelect
    
            StockDataAdapter.MissingSchemaAction = MissingSchemaAction.AddWithKey
            StockDataAdapter.Fill(ds, "tblstock")
            dataTable = ds.Tables("tblstock") 'When the form loads, the datatable uses the "tblcustomers" table.
            StockBindingSource.DataSource = ds
            StockBindingSource.DataMember = "tblstock"
            dgvStock.DataSource = StockBindingSource
            dgvStock.SelectionMode = DataGridViewSelectionMode.FullRowSelect
    
            BookingsDataAdapter.MissingSchemaAction = MissingSchemaAction.AddWithKey
            BookingsDataAdapter.Fill(ds, "tblbookings")
            dataTable = ds.Tables("tblbookings") 'When the form loads, the datatable uses the "tblcustomers" table.
            BookingsBindingSource.DataSource = ds
            BookingsBindingSource.DataMember = "tblbookings"
            dgvBookings.DataSource = BookingsBindingSource
            dgvBookings.SelectionMode = DataGridViewSelectionMode.FullRowSelect
        End Sub

  2. #2
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Retrieving and Saving Data in Databases

    Split from this CodeBank thread.

  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: Retrieving and Saving Data in Databases

    You can use DataGridView.SelectedIndex to get the selected row in the dgv. You can therefore use DataGridView1.Rows[DataGridView1.SelectedIndex-1] to read the primary key of the row of data that has been selected.

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

    Re: Retrieving and Saving Data in Databases

    As you've bound your grid to a BindingSource you'd use its Current property to get the selected DataRowView. You can then get any field value from that row. If you need to add a row to a grid you call AddNew on the bound BindingSource to create a new DataRowView, set its field values and then call EndEdit on the BindingSource to add it to the underlying DataTable.
    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

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