Updating Database from form entry in VB.net - vb.net

I am trying to update an access database with the data entered into a form. When I hit the insert button, it updates the datagridview in the other form (so I can see the data), but it does not populate the database. What am I missing?
Dim newCoinsRow As CoinsDatabaseDataSet.CoinsRow
newCoinsRow = frmCollection.CoinsDatabaseDataSet.Coins.NewCoinsRow
newCoinsRow.Name = strName
newCoinsRow.Year = intYear
newCoinsRow.FaceValue = decFaceValue
newCoinsRow.Currency = strCurrency
newCoinsRow.Metal = strMetal
newCoinsRow.Weight = decWeight
newCoinsRow.Value = decValue
newCoinsRow.IsEncapsulated = boolEncapsulated
frmCollection.CoinsDatabaseDataSet.Coins.Rows.Add(newCoinsRow)
frmCollection.CoinsTableAdapter.Update(frmCollection.CoinsDatabaseDataSet.Coins)
In the form with the datagrid view, I have this:
Private Sub frmCollection_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Me.CoinsTableAdapter.Fill(Me.CoinsDatabaseDataSet.Coins)
End Sub

Related

VB.NET - System.InvalidCastException (Datagridview)

I get the following error:
System.InvalidCastException: 'Unable to cast object of type 'System.EventArgs' to type 'System.Windows.Forms.DataGridViewCellEventArgs'.'
In this procedure:
Private Sub FillUsersGrid()
Dim sql As String = "SELECT * FROM Users"
Dim dataadapter As New SqlDataAdapter(sql, SQLconn)
Dim Users As New DataSet()
SQLconn.Open()
dataadapter.Fill(Users, "Users_table")
SQLconn.Close()
DgdUsersList.DataSource = Users
DgdUsersList.DataMember = "Users_table"
Me.DgdUsersList.AutoResizeColumns()
End Sub
When I change the following procedure's name:
Private Sub DgdUsersList_SelectionChanged(sender As Object, e As DataGridViewCellEventArgs) Handles DgdUsersList.SelectionChanged
If e.RowIndex >= 0 Then
Row = Me.DgdUsersList.Rows(e.RowIndex)
TxtEmployeesUsername.Text = Row.Cells("Username").Value.ToString
TxtEmployeesPassword.Text = Row.Cells("Password").Value.ToString
CmbEmployeesAccessLevel.Text = Row.Cells("AccessLevel").Value
ChkEmployeesPOSAccess.Checked = Row.Cells("POSAccess").Value
CmbEmployeesPOSPosition.Text = Row.Cells("POSPosition").Value.ToString
End If
DgdUsersList.Rows(CurrentRowIndex).Selected = True
End Sub
From:
Private Sub DgdUsersList_CellClicked(sender As Object, e As DataGridViewCellEventArgs) Handles DgdUsersList.CellClicked
To:
Private Sub DgdUsersList_SelectionChanged(sender As Object, e As DataGridViewCellEventArgs) Handles DgdUsersList.SelectionChanged
The scenario is this; I have two Datagridview controls and I want to make it so that when I click on any cell in DgdEmployeesList, the same row (index) is selected in DgdEmployeesList.
This is because I want to populate a form with a record when a record is clicked, and the employees table in the SQL database is linked to the users one in the same database via a primary key (ID - When an employee profile is created, a user profile is created with the same ID using the same form)
Is there any fix for this (I understand it may be easier to code this just by using the datagridview control itself, but I am unaware of a way to make two tables display inside the control and how to separate them upon saving, along with editing records and deleting them)
Any help is greatly appreciated!
James

Multiple Child Forms behaving strangely

I have created a Form that dynamically loads buttons into a panel on the left hand side of an MDI Parent Form. Once loaded, a user can click one of the buttons, and a Child form will load into the right hand side, docked at the top and containing a datagridview with the results of some query. Each child form that is added sits directly below the previous one, due to the docking top property.
All this seems to work fine, however when more than one child forms is open the behaviour of the forms is unexpected. If I click on any child form other than the bottom form, it immediately moves its position to the bottom, rather than staying in place. Is there a way to deal with this - or is there something I am doing in my code that means this is happening - such as trying to dock a window?
My code is below.
Private Sub TableLookupForm_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim mySQLString As String = "Button SQL Query"
Dim i As Integer = 0
myVector.mySQLString = mySQLString
For Each row In myVector.myIngresDataset.Tables(0).Rows
Dim myBtn As New Button
i = i + 1
myBtn.Text = row("table_name")
myBtn.Name = "Btn" & i
myBtn.Dock = DockStyle.Top
AddHandler myBtn.Click, AddressOf Me.Button_Click
Me.Panel1.Controls.Add(myBtn)
Me.Panel1.Controls.SetChildIndex(myBtn, 0)
Next
End Sub
Private Sub Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Dim myTableForm As New TableChildForm
myTableForm.MdiParent = Me
myTableForm.Dock = DockStyle.Top
myTableForm.Tag = sender.text
myTableForm.Text = sender.text
Dim mySQLString As String = "Form SQL Query"
myVector.mySQLString = mySQLString
myTableForm.myTableData.DataSource = myVector.myIngresDataset.Tables(0)
myTableForm.Show()
End Sub
Thanks,
Paul.

VB 2013 load datagridview from a form

I have searched the net looking for how to populate a DataGridView with the New Data "Record" that is entered into a form then save the record using the BindingNavigator.
I have a single form with the DataDridView on it and I pulled the table columns from the DataSource into the form so as to allow the people to enter new records. I then want to populate the DataGrid with the information then save it.
Private Sub BindingNavigatorAddNewItem_Click(sender As Object, e As EventArgs) Handles BindingNavigatorAddNewItem.Click
Dim RR As Integer = ACCOUNT_MOVEDDataGridView.RowCount - 2
For RC As Integer = 0 To RR
If ACCOUNT_MOVEDDataGridView(2, RC).Value Is DBNull.Value Then
'' ACCOUNT_MOVEDDataGridView(2, RR).Value = ACCOUNT_MOVEDDataGridView(15, RR).Value = 0
End If
Next
End Sub
I have figured it out, I was placing the code under BindingNavigatorAddNewItem_Click
when it should have been under the Save Item click.
Private Sub ACCOUNT_MOVEDBindingNavigatorSaveItem_Click(sender As Object, e As EventArgs) Handles ACCOUNT_MOVEDBindingNavigatorSaveItem.Click
Call ACCDEF()
Private Sub ACCDEF()
Dim RR As Integer = ACCOUNT_MOVEDDataGridView.RowCount - 2
For RC As Integer = 0 To RR
If ACCOUNT_MOVEDDataGridView(2, RC).Value Is DBNull.Value Then
ACCOUNT_MOVEDDataGridView(2, RR).Value = Date.Now
ACCOUNT_MOVEDDataGridView(15, RR).Value = 0
Else
ACCOUNT_MOVEDDataGridView(1, RR).Value = Date.Now
End If
Next
End Sub
Which loads the DataGridView with the default values, Thanks to all that have looked at my problem.

Display A Picture From Database

I have a datagridview and when I click inside, it opens another form with the information that is inside the datagridview.
In my Access database I have OLE Object with a picture (bitmap) and I want to display that in the form info too.
This is the code that I have to get the info when I click in the datagridview:
Private Sub DataGridView1_RowHeaderMouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellMouseEventArgs) Handles DataGridView1.RowHeaderMouseClick
Dim curForm As Form = New Form
With curForm
info.TextBox1.Text = DataGridView1.SelectedRows(0).Cells(1).Value
info.TextBox2.Text = DataGridView1.SelectedRows(0).Cells(2).Value
info.TextBox3.Text = DataGridView1.SelectedRows(0).Cells(3).Value
info.TextBox4.Text = DataGridView1.SelectedRows(0).Cells(4).Value
info.TextBox5.Text = DataGridView1.SelectedRows(0).Cells(5).Value
info.Show()
End With
End Sub

I want a selected value in a combobox on form1, to be automatically populated into a textbox/label on form 3

I have 3 forms at the moment, form 1 you select 4 peices of data,
Form 2 you select up to 10.
I want it so when you go to form 3, all selected fields on form 2 are shown on the 3rd form.... as uneditable text fields (labels i guess)
I would attach some diagrams to make this 5000x easier to explain, but i cant yet.
I just need to know how i can make the code grab the values input into combobox's and textbox's and output them on form 3.
Edit;
i have no code in place as i have been trying differnt techniques and have gotten nowhere
Me.ComboBox1.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList
Me.ComboBox1.FormattingEnabled = True
Me.ComboBox1.Items.AddRange(New Object() {"ClaimCode", "PostCode", "AlgPremium", "Etc", "Etc", "Etc"})
Me.ComboBox1.Location = New System.Drawing.Point(27, 33)
Me.ComboBox1.Name = "ComboBox1"
Me.ComboBox1.Size = New System.Drawing.Size(225, 21)
Me.ComboBox1.TabIndex = 2
'
'ComboBox2
'
Me.ComboBox2.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList
Me.ComboBox2.FormattingEnabled = True
Me.ComboBox2.Items.AddRange(New Object() {"Single", "Multi"})
Me.ComboBox2.Location = New System.Drawing.Point(275, 33)
Me.ComboBox2.Name = "ComboBox2"
Me.ComboBox2.Size = New System.Drawing.Size(147, 21)
Me.ComboBox2.TabIndex = 3
'
'Values
'
Me.Values.Location = New System.Drawing.Point(450, 34)
Me.Values.Name = "Values"
Me.Values.Size = New System.Drawing.Size(297, 20)
Me.Values.TabIndex = 4
I want the above values (from FORM2) when selected, taking out and putting into the following fields on FORM3...
'TextBox1
'
Me.TextBox1.Location = New System.Drawing.Point(11, 13)
Me.TextBox1.Name = "TextBox1"
Me.TextBox1.Size = New System.Drawing.Size(259, 20)
Me.TextBox1.TabIndex = 0
'
'TextBox2
'
Me.TextBox2.Location = New System.Drawing.Point(9, 13)
Me.TextBox2.Name = "TextBox2"
Me.TextBox2.Size = New System.Drawing.Size(85, 20)
Me.TextBox2.TabIndex = 1
Nothing is named yet as i just need to understand how to do this, there will be alot more done with the data than just passed into the form, but for now. this will grant me basic understanding i work from.
For a full description, i will have Various Templates of XML, when you select the 4 values on FORM1, a specific template will be picked in the background.
Then in FORM2 you will select values you wish to modify on this template, these will be modified on the XML and the XML posted to a Test harness where various filters/rules will be ran against it (drools),
Then on form3 a list of all Fields you changed on form2, the values you input on FORM2 and how they came back out after the morphs/filters in the Test harness will be displayed and the XML produced will be saved and linked to via FORM3.
I am a Lonnngggg way from having the program at this stage, but i am just trying to understand how to use the GUI and how to pass data from one form to another. or export it to an XML file
You can pass data to a form by setting public properties before displaying it, like this:
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim f As New FormVb2()
f.TextBox1Value = TextBox1.Text
f.ShowDialog()
End Sub
End Class
And then you can use the value in the public property when the form loads, like this:
Public Class Form2
Public Property TextBox1Value As String
Private Sub Form2_Load(sender As Object, e As EventArgs) Handles Me.Load
TextBox1.Text = TextBox1Value
End Sub
End Class
Try this code
val is global variable
Dim Val as string=string.empty
Private Sub ComboBox1_SelectedValueChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedValueChanged
val = ComboBox1.SelectedItem
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim xfrm As New Form3
xfrm.TextBox1.Text = val
xfrm.ShowDialog()
End Sub