When I add a new row to my data set it is visible on that specific form in the datagridview, however when I switch to another form with the same data bound datagridview the new row is not there. When I close the program my new row is completely gone then. I want to to save the new row to the Access database it is reading from.
Public Class frmAddStudent
Private Sub btnBack_Click(sender As Object, e As EventArgs) Handles btnBack.Click
Me.Hide()
frmUserControls.Show()
End Sub
Private Sub frmAddStudent_Load(sender As Object, e As EventArgs) Handles MyBase.Load
'TODO: This line of code loads data into the 'StudentRecords1DataSet.tblLecturer' table. You can move, or remove it, as needed.
Me.TblLecturerTableAdapter.Fill(Me.StudentRecords1DataSet.tblLecturer)
'TODO: This line of code loads data into the 'StudentRecords1DataSet.tblStudents' table. You can move, or remove it, as needed.
Me.TblStudentsTableAdapter.Fill(Me.StudentRecords1DataSet.tblStudents)
End Sub
Private Sub btnAdd_Click(sender As Object, e As EventArgs) Handles btnAdd.Click
Dim MyNewRow As DataRow
MyNewRow = StudentRecords1DataSet.tblStudents.NewRow
Try
With MyNewRow
.Item(1) = txtID.Text
.Item(2) = txtFirstName.Text
.Item(3) = txtSurname.Text
End With
Me.Validate()
Me.TblStudentsBindingSource.EndEdit() 'Change this to your binding source ' eg table'
Me.TableAdapterManager.UpdateAll(Me.StudentRecords1DataSet) ' chnage this to your database name'
MessageBox.Show("The Data Has Been Saved", "Information", MessageBoxButtons.OK)
Catch ex As Exception
'if there is a problem saving the data, it will show a messagebox with the problem as to why it could'nt save the data'
MessageBox.Show(ex.Message)
End Try
StudentRecords1DataSet.tblStudents.Rows.Add(MyNewRow)
StudentRecords1DataSet.tblStudents.AcceptChanges()
End Sub
Private Sub txtFirstName_MaskInputRejected(sender As Object, e As MaskInputRejectedEventArgs)
End Sub
Private Sub TblStudentsBindingNavigator_RefreshItems(sender As Object, e As EventArgs) Handles TblStudentsBindingNavigator.RefreshItems
End Sub
End Class
Any suggestions please?
UPDATe X2 based off your question update.
try this,
Public Class frmAddStudent
Private Sub btnBack_Click(sender As Object, e As EventArgs) Handles btnBack.Click
Me.Hide()
frmUserControls.Show()
End Sub
Private Sub frmAddStudent_Load(sender As Object, e As EventArgs) Handles MyBase.Load
'TODO: This line of code loads data into the 'StudentRecords1DataSet.tblLecturer' table. You can move, or remove it, as needed.
Me.TblLecturerTableAdapter.Fill(Me.StudentRecords1DataSet.tblLecturer)
'TODO: This line of code loads data into the 'StudentRecords1DataSet.tblStudents' table. You can move, or remove it, as needed.
Me.TblStudentsTableAdapter.Fill(Me.StudentRecords1DataSet.tblStudents)
End Sub
Private Async Sub btnAdd_Click(sender As Object, e As EventArgs) Handles btnAdd.Click
Try
Me.Validate()
Me.TblStudentsBindingSource.EndEdit() 'Change this to your binding source ' eg table'
Me.TableAdapterManager.UpdateAll(Me.StudentRecords1DataSet) ' chnage this to your database name'
MessageBox.Show("The Data Has Been Saved", "Information", MessageBoxButtons.OK)
Await Task.Delay(100)
TblStudentsBindingNavigator_RefreshItems.studentsBindingSource.AddNew()
Catch ex As Exception
'if there is a problem saving the data, it will show a messagebox with the problem as to why it could'nt save the data'
MessageBox.Show(ex.Message)
End Try
End Sub
Private Sub txtFirstName_MaskInputRejected(sender As Object, e As MaskInputRejectedEventArgs)
End Sub
Private Sub TblStudentsBindingNavigator_RefreshItems(sender As Object, e As EventArgs) Handles TblStudentsBindingNavigator.RefreshItems
End Sub
End Class
Related
I am using VB.Net 2013 with SQL Server
What is the problem with my code? Is there something missing?
When I try to select the location, it show me in the Form the name, location and the image.
Everything looks good, the only problem is that the image in report viewer doesn't change.
Imports System.IO
Imports Microsoft.Reporting.WinForms
Public Class Form1
Private Sub Table_locationBindingNavigatorSaveItem_Click(sender As Object, e As EventArgs) Handles Table_locationBindingNavigatorSaveItem.Click
Me.Validate()
Me.Table_locationBindingSource.EndEdit()
Me.TableAdapterManager.UpdateAll(Me.KankonDataSet)
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
'TODO: This line of code loads data into the 'KankonDataSet.Table_location' table. You can move, or remove it, as needed.
Me.Table_locationTableAdapter.Fill(Me.KankonDataSet.Table_location)
If NamelocationComboBox.Text = Nothing Then
NamelocationComboBox.Text = "No thing"
End If
If IdlocationTextBox.Text = Nothing Then
IdlocationTextBox.Text = "No thing"
End If
Dim Param1 As New ReportParameter("ReportParameterlocation", NamelocationComboBox.Text)
ReportViewer1.LocalReport.SetParameters(Param1)
Dim Param2 As New ReportParameter("ReportParameterwhere", IdlocationTextBox.Text)
ReportViewer1.LocalReport.SetParameters(Param2)
Me.ReportViewer1.RefreshReport()
End Sub
Private Sub ReportViewer1_Load(sender As Object, e As EventArgs) Handles ReportViewer1.Load
Me.ReportViewer1.RefreshReport()
End Sub
Private Sub NamelocationComboBox_SelectedIndexChanged(sender As Object, e As EventArgs) Handles NamelocationComboBox.SelectedIndexChanged
Dim Param1 As New ReportParameter("ReportParameterlocation", NamelocationComboBox.Text)
ReportViewer1.LocalReport.SetParameters(Param1)
Me.ReportViewer1.RefreshReport()
End Sub
Private Sub IdlocationTextBox_TextChanged(sender As Object, e As EventArgs) Handles IdlocationTextBox.TextChanged
Dim Param2 As New ReportParameter("ReportParameterwhere", IdlocationTextBox.Text)
ReportViewer1.LocalReport.SetParameters(Param2)
Me.ReportViewer1.RefreshReport()
End Sub
End Class
Image in Attachments!
I try to close or hide the Form cnx if a file exist and open the Form Product.
But Something is wrong and i don't understand why this dont work.
Private Sub cnx_Load(sender As Object, e As EventArgs) Handles MyBase.Load
strFileName = "app.txt"
strBasePath = Application.StartupPath
If My.Computer.FileSystem.FileExists(strFileName) = True Then
Product.Show()
Me.Hide()
ElseIf My.Computer.FileSystem.FileExists(strFileName) = False Then
MessageBox.Show("File App.config is Missing! Create a new Database.",
"Something is Wrong!", MessageBoxButtons.OK, MessageBoxIcon.Warning)
End If
End Sub
Thanks.
You could do something like this, although probably not optimal.
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
hideForm(Me)
Form2.Show()
End Sub
Private Sub hideForm(form As Form)
form.Opacity = 0.0F
form.ShowInTaskbar = False
End Sub
Also remember to add under Form2 or your program will stay open after closing Form2.
Private Sub Form2_FormClosing(sender As Object, e As FormClosingEventArgs) Handles MyBase.FormClosing
Form1.Close()
End Sub
I am trying to create a new and delete button. The new button works but I cant get the delete button to work.
Code:
Imports System.Data.SqlClient
Public Class AssetCategory
Dim cn As New SqlConnection("Data Source=DESKTOP-PHILIP\SQLEXPRESS;Initial Catalog=PECS_Asset;Integrated Security=True")
Dim cmd As New SqlCommand
Dim dir As SqlDataReader
Private txtupdatename As Object
Private Sub AssetCategoryBindingNavigatorSaveItem_Click(sender As Object, e As EventArgs)
Me.Validate()
Me.TableAdapterManager.UpdateAll(Me.PECS_AssetDataSet)
cmd.Connection = cn
End Sub
Private Sub AssetCategory_Load(sender As Object, e As EventArgs) Handles MyBase.Load
'TODO: This line of code loads data into the 'PECS_AssetDataSet.AssetCategory' table. You can move, or remove it, as needed.
Me.AssetCategoryTableAdapter.Fill(Me.PECS_AssetDataSet.AssetCategory)
End Sub
Private Sub ReturnToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles ReturnToolStripMenuItem.Click
Me.Hide()
Form1.Show()
End Sub
Private Sub MenuStrip1_ItemClicked(sender As Object, e As ToolStripItemClickedEventArgs) Handles MenuStrip1.ItemClicked
End Sub
Private Sub AssetsToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles AssetsToolStripMenuItem.Click
Me.Hide()
Form1.Show()
End Sub
Private Sub ExitToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles ExitToolStripMenuItem.Click
Application.Exit()
End Sub
Private Sub btnNew_Click(sender As Object, e As EventArgs) Handles btnNew.Click
Me.Validate()
Me.AssetCategoryBindingSource.AddNew()
Me.TableAdapterManager.UpdateAll(Me.PECS_AssetDataSet)
End Sub
Private Sub btnSave_Click(sender As Object, e As EventArgs) Handles btnSave.Click
Try
Me.AssetCategoryBindingSource.EndEdit()
Me.TableAdapterManager.UpdateAll(Me.PECS_AssetDataSet)
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
Private Sub btnDelete_Click(sender As Object, e As EventArgs)
If MsgBox("Are you sure you want to delete this record?", MsgBoxStyle.YesNo, Title:="CONFIRM") - vbYes Then
Else
TableAdapterManager.UpdateAll([dbo].[AssetCategory])
End If
End Sub
End Class
I'm very frustrated trying to get my code to work.
I'm trying to have a selected item in a listbox removed also in the textbox.
Getting ready to remove text;
Removed the text;
But it's still in the textbox.
Here is my code
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
ListBox1.Items.Add(TextBox1.Text)
TextBox2.Text += TextBox1.Text & vbNewLine
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
ListBox1.Items.Remove(ListBox1.SelectedItem)
'
'//HOW TO REMOVE THE SELECTED TEXT IN THE LISTBOX ALSO REMOVED IN THE TEXTBOX2??
'
'
End Sub
Private Sub Form1_FormClosing(sender As Object, e As FormClosingEventArgs) Handles Me.FormClosing
Dim filenames As String = "C:\log\log.txt"
My.Computer.FileSystem.WriteAllText(filenames, TextBox2.Text, False)
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim filenames As String = "C:\log\log.txt"
If My.Computer.FileSystem.FileExists(filenames) Then
TextBox2.Text = My.Computer.FileSystem.ReadAllText(filenames)
Dim items()
items = TextBox2.Lines()
For Each item In items
ListBox1.Items.Add(item)
Next
End If
End Sub
Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
Clipboard.SetText(ListBox1.SelectedItem)
End Sub
End Class
The worst part is that every time I try to look it up online, there are no errors until I click the button that says 'Value Cannot Be Null'
It happened every single time.
Please, before you mash the -1 button, at least tell me why. I'm new to this.
This should work for you
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
TextBox1.Text = TextBox1.Text.Replace(ListBox1.Items(ListBox1.SelectedIndex), Nothing)
ListBox1.Items.RemoveAt(ListBox1.SelectedIndex)
End Sub
End Class
I am using vb.net designer to connect to access database .
On my Form1 I have a DataGridView And Two Button For Add And Edit
I Make Form2 To Add Data Into Database And Worked OK ..
Imake Form3 Wiht Same form2 Content
Now I need When i selcet row in DataGridView And Clic Edit Button The data of selected row show on form3 for Edit it
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Me.SalesTableAdapter.Fill(Me.OrdersDataSet.sales)
Me.DateTimePicker1.Value = Date.Today
End Sub
Private Sub DateTimePicker1_ValueChanged(sender As Object, e As EventArgs) Handles DateTimePicker1.ValueChanged
SalesBindingSource.Filter = String.Format("date = '{0}'", DateTimePicker1.Value.ToShortDateString())
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Form2.Show()
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Form3.Show()
End Sub
Private Sub Label1_Click(sender As Object, e As EventArgs) Handles Label1.Click
End Sub
Private Sub SalesDataGridView_CellContentClick(sender As Object, e As DataGridViewCellEventArgs) Handles SalesDataGridView.CellContentClick
End Sub
End Class
You need to approach this in a modal/dialog way. You only need one form for both add and edit.
Add/Edit form
Add a parameterized constructor to the form.
Public Sub New(row As DataRowView)
Me.InitializeComponent()
'Me.ctlAge: NumericUpDown control.
'Me.ctlBirthday: DateTimePicker control.
'Me.ctlName: TextBox control.
If (row Is Nothing) Then
'Add mode, set default values:
Me.ctlAge.Value = 0
Me.ctlBirthday.Value = Date.Now
Me.ctlName.Text = String.Empty
Else
'Edit mode, set current values:
Me.ctlAge.Value = CDec(row.Item("AGE"))
Me.ctlBirthday.Value = CDate(row.Item("BIRTHDAY"))
Me.ctlName.Text = CStr(row.Item("NAME"))
End If
End Sub
You also need an accept button and a cancel button.
Friend Sub btnAcceptClicked(sender As Object, e As EventArgs) Handles btnAccept.Click
Me.DialogResult = Windows.Forms.DialogResult.OK
Me.Close()
End Sub
Friend Sub btnCancelClicked(sender As Object, e As EventArgs) Handles btnCancel.Click
Me.DialogResult = Windows.Forms.DialogResult.Cancel
Me.Close()
End Sub
Main form
Add method:
Private Sub btnAddClicked(sender As Object, e As EventArgs) Handles btnAdd.Click
Try
Using f As New AddOrEditForm(CType(Nothing, DataRowView))
If (f.ShowDialog() = Windows.Forms.DialogResult.OK) Then
Dim view As DataView = TryCast(Me.SalesDataGridView.DataSource, DataView)
If (view Is Nothing) Then
Throw New InvalidCastException()
End If
Dim viewRow As DataRowView = view.AddNew()
viewRow.EndEdit()
viewRow.Item("AGE") = f.ctlAge.Value
viewRow.Item("BIRTHDAY") = f.ctlBirthday.Value
viewRow.Item("NAME") = f.ctlName.Text
viewRow.EndEdit()
End If
End Using
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub
Edit method:
Private Sub btnEditClicked(sender As Object, e As EventArgs) Handles btnEdit.Click
Try
Me.SalesDataGridView.EndEdit()
If (Me.SalesDataGridView.SelectedRows.Count > 0) Then
Dim gridRow As DataGridViewRow = Me.SalesDataGridView.SelectedRows(0)
Dim viewRow As DataRowView = TryCast(gridRow.DataBoundItem, DataRowView)
If (viewRow Is Nothing) Then
Throw New InvalidCastException()
End If
Using f As New AddOrEditForm(viewRow)
If (f.ShowDialog() = Windows.Forms.DialogResult.OK) Then
viewRow.BeginEdit()
Try
viewRow.Item("AGE") = f.ctlAge.Value
viewRow.Item("BIRTHDAY") = f.ctlBirthday.Value
viewRow.Item("NAME") = f.ctlName.Text
viewRow.EndEdit()
Catch ex As Exception
viewRow.CancelEdit()
Throw ex
End Try
End If
End Using
End If
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub