none
Writing new row to local database RRS feed

  • Question

  • Hi,

    I am trying to start with databases. I created a standalone application with a local database file.

    CREATE TABLE [dbo].[Logins] (
        [ID]            INT          IDENTITY (1, 1) NOT NULL,
        [UserName]      VARCHAR (32) NULL,
        [PasswordSalt]  CHAR (32)    NULL,
        [PasswordHash]  CHAR (32)    NULL,
        [RelatedUserID] INT          NULL,
        PRIMARY KEY CLUSTERED ([ID] ASC)
    );

    I added a single row value to this table UserName = Admin, on my windows form is a listbox to show the users in my database and it works fine. But when I add a new userrow, close the form and reload the form my new user does not show up. What am I doing wrong?

    Public Class Users
        Private LoginsTableAdaptor As TestDataBaseDataSetTableAdapters.LoginsTableAdapter = New TestDataBaseDataSetTableAdapters.LoginsTableAdapter()
        Private Logins As TestDataBaseDataSet.LoginsDataTable = LoginsTableAdaptor.GetData
        Public Sub New()
    
            ' This call is required by the designer.
            InitializeComponent()
    
            ' Add any initialization after the InitializeComponent() call.
    
            UsersOverviewListbox.DataSource = Logins
            UsersOverviewListbox.DisplayMember = "UserName"
    
        End Sub
    
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            MainMenu.Show()
            LoginsTableAdaptor.Update(Logins)
            Me.Close()
        End Sub
    
        Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
    
            Dim newUser As TestDataBaseDataSet.LoginsRow = Logins.NewLoginsRow()
            newUser.UserName = "JEEE"
            Logins.AddLoginsRow(newUser)
            newUser.AcceptChanges()
        End Sub
    End Class

    Tuesday, October 30, 2018 1:35 PM

Answers

  • Please show the code for opening the form. I ask because if you create the form new each time there is zero reasons for this to happen.

    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    This code opens the form from the main menu (just now this is all there is on the main menu)

        Private Sub UsersWindowButton_Click(sender As Object, e As EventArgs) Handles UsersWindowButton.Click
            Users.Show()
            Me.Close()
        End Sub

    If you don't create the form each time that is a problem, what you showed does not create the form each time so any code in the new constructor is only called once when you creat the form e.g. Dim Users As New Users, if not using that at all and calling the default instance of the form this will not work either.

    Lastly, read the edit line in my last reply as that needs to go away.


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    • Marked as answer by E_Jef Tuesday, October 30, 2018 4:34 PM
    Tuesday, October 30, 2018 4:16 PM
    Moderator

All replies

  • Under properties in solution explorer for the database, currently "Copy to output Directory" is most likely "Copy always", change this to "Copy if newer" and you will be fine.


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Tuesday, October 30, 2018 1:48 PM
    Moderator
  • Hi Karen,

    I know that if I build my database is overwritten. But this is all happening during runtime.

    1. Load the form with data from the database. Admin is shown in listbox
    2. Add new row to dataset
    3. Close form (not application)
    4. Reload the form with data from database. The new user JEEE is not shown in the listbox.
    Tuesday, October 30, 2018 2:12 PM
  • Please show the code for opening the form. I ask because if you create the form new each time there is zero reasons for this to happen.

    EDIT Also get rid of this line

     newUser.AcceptChanges()


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites


    Tuesday, October 30, 2018 4:02 PM
    Moderator
  • Please show the code for opening the form. I ask because if you create the form new each time there is zero reasons for this to happen.

    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    This code opens the form from the main menu (just now this is all there is on the main menu)

        Private Sub UsersWindowButton_Click(sender As Object, e As EventArgs) Handles UsersWindowButton.Click
            Users.Show()
            Me.Close()
        End Sub

    Tuesday, October 30, 2018 4:13 PM
  • Please show the code for opening the form. I ask because if you create the form new each time there is zero reasons for this to happen.

    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    This code opens the form from the main menu (just now this is all there is on the main menu)

        Private Sub UsersWindowButton_Click(sender As Object, e As EventArgs) Handles UsersWindowButton.Click
            Users.Show()
            Me.Close()
        End Sub

    If you don't create the form each time that is a problem, what you showed does not create the form each time so any code in the new constructor is only called once when you creat the form e.g. Dim Users As New Users, if not using that at all and calling the default instance of the form this will not work either.

    Lastly, read the edit line in my last reply as that needs to go away.


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    • Marked as answer by E_Jef Tuesday, October 30, 2018 4:34 PM
    Tuesday, October 30, 2018 4:16 PM
    Moderator