none
BindingSource Manual Updates

    Question

  • I am new to Visual Basic 2017 and I am trying to figure out the best way to do this.

    I have a form with three fields and I have written code for command buttons to Add new record,  move next, first, last and previous and they all work.  Now I am working on the Save button.  Since my fields aren't corrected to fields,  I need to move them in manually. 

    For opening the database,  I have this:

    Private AfterConn AsNewOleDb.OleDbConnection
    PrivateContactAdp AsNewOleDb.OleDbDataAdapter
    PrivateContactDS AsNewDataSet
    PrivateContactSRC AsNewBindingSource

    AfterConn.ConnectionString = "Provider = Microsoft.Jet.OLEDB.4.0;Data Source=C:\TEMP\testDB0514.mdb"

           AfterConn.Open()

            ContactAdp = NewOleDb.OleDbDataAdapter("Select* from Contacts", AfterConn)

            ContactAdp.Fill(ContactTable)

            ContactDS = NewDataSet
            ContactAdp.Fill(ContactDS, "Contacts")

            ContactSRC.DataSource = ContactDS

    Then I have a cmdSave button,  but not sure how to move the text fields into the ContactSRC.  

    Or is there a better way to do this than the BindingSource?   One reason I went with the binding source is so when I create a Search button that I can do the binding source.filter option.

    Thanks for your help,

    Ken

    Monday, May 20, 2019 9:12 PM

Answers

  • It isn't resolved yet.  I am not sure what I need to get my issue resolved.

    I did kind of switch things around in that maybe I could update the table and then refresh the dataset/binding source.  I cant find any examples on that either,  so I am kind of stuck with this issue.

    • Marked as answer by KenTrinity Thursday, May 23, 2019 5:05 PM
    Wednesday, May 22, 2019 1:31 PM

All replies

  • Hi,

    Do you want to modify the database?I made a demo.

    Imports System.Data.SqlClient
    Public Class Form1
        Dim constr As String = "Data Source = (localdb)\MSSQLLocalDB; Integrated Security = True ;AttachDbFileName= C:\Users\alexl2\Desktop\DataBase\Alex\alex.mdf"
        Dim conn As SqlConnection
        Dim sda As SqlDataAdapter
        Dim dt As New DataTable
        Dim bind As New BindingSource
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            conn = New SqlConnection(constr)
            conn.Open()
            sda = New SqlDataAdapter("Select * From Student", conn)
            Dim SQLCommandBuild As SqlCommandBuilder = New SqlCommandBuilder(sda)
            sda.Fill(dt)
            bind.DataSource = dt
            DataGridView1.DataSource = bind
    
            TextBox1.DataBindings.Add(New Binding("Text", bind, "Id"))
            TextBox2.DataBindings.Add(New Binding("Text", bind, "Name"))
            TextBox3.DataBindings.Add(New Binding("Text", bind, "Age"))
            TextBox4.DataBindings.Add(New Binding("Text", bind, "Uid"))
        End Sub
    
    
    
        Private Sub cmdSave_Click(sender As Object, e As EventArgs) Handles cmdSave.Click
            bind.EndEdit()
            sda.Update(dt)
        End Sub
    
        Private Sub cmdNext_Click(sender As Object, e As EventArgs) Handles cmdNext.Click
            bind.MoveNext()
        End Sub
    
        Private Sub cmdPrevious_Click(sender As Object, e As EventArgs) Handles cmdPrevious.Click
            bind.MovePrevious()
        End Sub
    End Class


    Best Regards,

    Alex


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, May 21, 2019 3:22 AM
    Moderator
  • Yes,  I need to update the database.  For the text fields though,  I didn't want them to have data bindings.   On the move first/next, etc.  I was just going to move the fields from the text boxes to the database and then do a database update command.  Unfortunately,  I am not sure where to move the text box data to in order to od the save.

    I have code like this,  but it doesn't work:   Here are two different things I have tried but both fail:

    1. ContactSRC.DataMember("ContactName").ToString()
    2. ContactSRC.Item("ContactName") = UCase(txtName.Text)

    these do work,  I think:

    ContactSRC.EndEdit()

    ContactAdp.Update(ContactDS, "Contacts")

    Thanks for your help,
    Ken

    Tuesday, May 21, 2019 1:41 PM
  • Yes,  I need to update the database.  For the text fields though,  I didn't want them to have data bindings.   On the move first/next, etc.  I was just going to move the fields from the text boxes to the database and then do a database update command.  Unfortunately,  I am not sure where to move the text box data to in order to od the save.

    I have code like this,  but it doesn't work:   Here are two different things I have tried but both fail:

    1. ContactSRC.DataMember("ContactName").ToString()
    2. ContactSRC.Item("ContactName") = UCase(txtName.Text)

    these do work,  I think:

    ContactSRC.EndEdit()

    ContactAdp.Update(ContactDS, "Contacts")

    Thanks for your help,
    Ken

    Hi,

    Do you resolve the issue? If you resolve the issue, please mark the helpful as answer.

    Best Regards,

    Alex


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, May 22, 2019 3:07 AM
    Moderator
  • It isn't resolved yet.  I am not sure what I need to get my issue resolved.

    I did kind of switch things around in that maybe I could update the table and then refresh the dataset/binding source.  I cant find any examples on that either,  so I am kind of stuck with this issue.

    • Marked as answer by KenTrinity Thursday, May 23, 2019 5:05 PM
    Wednesday, May 22, 2019 1:31 PM