Add new row into `DataGridView` and enter data from `TableAdapter` in another form - vb.net

I am trying to add new row of data in the DataGridView that is located in Form4. The data is to be entered in the Form3 into TableAdapter that is bound with DataGridView.
On the Form4 I have a button that opens Form3 and it suppose to add new rows into DatagridView but seems like it is not doing it.
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles
Button1.Click
Form3.Show()
Form4.DbTableDataGridView.Rows.Add() 'this is not executed?
End Sub
On the Form3 there is a TableAdapter that suppose to fill in new added rows in the DbTableDataGridView and then saves the changes. Something like:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Form4.DbTableDataGridView.Rows.Add(Me.DbTableTableAdapter)
End Sub

This works fine.
Public Class Form4
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Form3.ShowDialog()
End Sub
End Class
Public Class Form3
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Form4.DataGridView1.Rows.Add("Add Something here")
End Sub
End Class
Does the data in your datagridview is from database?
You can have a Public Sub in your Form4 to show the records.
Example:
Dim cn As New SqlConnection("your connection string here")
Dim cmd As New SqlCommand
Dim da As New SqlDataAdapter
Dim dt As New DataTable
Public Sub ShowRecord()
cn.Open()
With cmd
.Connection = cn
.CommandText = "Your command text here"
End With
da.SelectCommand = cmd
dt.Clear()
da.Fill(dt)
dataGridView1.DataSource = dt
cn.Close()
End Sub
In your Form3:
you can save the data in the database then call the sub in form4 like
form4.ShowRecord()
This is just a simple example though

Related

update data from a dataset to the database

the remove button actually removes the data from the dataset but when i restart the program it just reappears, tried with "Me.testTableAdapter.Fill(Me.testDataSet.test)", searched a lot and still didnt find anything. any help appreciated!
here is the code:
Imports System
Imports System.Data.OleDb
Imports System.Data
Imports System.Data.Sql
Imports System.Data.SqlClient
Public Class Form1
Dim con As OleDbConnection
Dim com As OleDbCommand
Dim txtconnesione As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\test.mdb"
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
'TODO: questa riga di codice carica i dati nella tabella 'testDataSet.test'. È possibile spostarla o rimuoverla se necessario.
Me.INATableAdapter.Fill(Me.testDataSet.test)
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
con = New OleDbConnection(txtconnesione)
com = New OleDbCommand("INSERT INTO INA (CODICE,Interno,esterno,altezza) VALUES(#CODICE,#Interno,#esterno,#altezza)", con)
con.Open()
com.Parameters.AddWithValue("#CODICE", txtcodice.Text)
com.Parameters.AddWithValue("#Interno", txtinterno.Text)
com.Parameters.AddWithValue("#esterno", txtesterno.Text)
com.Parameters.AddWithValue("#altezza", txtaltezza.Text)
com.ExecuteNonQuery()
MsgBox("New Record")
Me.testTableAdapter.Fill(Me.testDataSet.test)
con.Close()
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
For Each row As DataGridViewRow In DataGridView1.SelectedRows
DataGridView1.Rows.Remove(row)
testDataSet.test.DataSet.AcceptChanges()
Next
End Sub
End Class

Combo Box Declaration expected

When I got the value for the combo box from the MySQL database it seems getting error declaration expected in line 4. Can anyone help me? Here I attach my code
Imports MySql.Data.MySqlClient
Public Class Utama
Private Sub LogoutToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles LogoutToolStripMenuItem.Click
Me.Hide()
Dim utama As New Login
utama.Show()
End Sub
Private Sub KeluarToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles KeluarToolStripMenuItem.Click
Me.Close()
End Sub
Private Sub SupplierToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles SupplierToolStripMenuItem.Click
Me.Hide()
Dim supplier As New Supplier
supplier.Show()
End Sub
Private Sub ProdukToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles ProdukToolStripMenuItem.Click
Me.Hide()
Dim Produk As New Produk
Produk.Show()
End Sub
Private Sub CetakToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles CetakToolStripMenuItem.Click
Me.Hide()
Dim Cetak As New Cetak
Cetak.Show()
End Sub
Dim connection As New MySqlConnection("Server=127.0.0.1;Database=pembelian;Uid=root;Pwd=;")
Dim da As New MySqlDataAdapter("select * from supplier", connection)
Dim dt As DataTable
da.fill(dt)
ComboBox1.Datasource=dt
ComboBox1.DisplayMember = "npwp"
ComboBox1.ValueMember = "npwp"
End Class
You need to put that code inside a method. You can't just put arbitrary code anywhere in a class. The only thing that can be directly in the class is a declaration. The first three lines are declarations, so they're OK. The last four lines are not. That code should be inside a method somewhere. If you want to execute that code when the form loads then it should be in the Load event handler. This is pretty elementary stuff that any beginners tutorial would cover, so maybe you should work your way through such a tutorial.
First, create a method as shown below and check the exception.
Private Sub Utama_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Try
Dim connection As New MySqlConnection("Server=127.0.0.1;Database=pembelian;Uid=root;Pwd=;")
Dim da As New MySqlDataAdapter("select * from supplier", connection)
Dim dt As new DataTable
da.fill(dt)
ComboBox1.Datasource = dt
ComboBox1.DisplayMember = "npwp"
ComboBox1.ValueMember = "npwp"
Catch ex As Exception
MsgBox(ex.ToString)
End Try
End Sub

Vb.net hyperlink Dataview and listing between price range

I am working on a assignment in which I have to use VB.net and Access database in order to make a bookstore. One of the requirements is that I have to make a table with book titles and customer names with a hyperlink on the title and customer name which will redirect them to a new page. I have the table working, but I am not able to add a hyperlink to one of the columns in the table. Also there is a searching feature where I have to print all the books between a price range (ex. $20 < price <= $30) I have the radio buttons setup but I am not able to do the actual searching and get good values. My code is below:
Imports System
Imports System.Data
Imports System.Data.OleDb
Imports System.Data.SqlClient
Public Class CustomerSearching
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
RadioButton1.Visible = False
RadioButton2.Visible = False
RadioButton3.Visible = False
RadioButton4.Visible = False
RadioButton5.Visible = False
End Sub
Protected Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
Dim conn As New OleDb.OleDbConnection("Provider=Microsoft.ACE.Oledb.12.0;Data Source=" + Server.MapPath("~/bookstore.accdb"))
conn.Open()
End Sub
Protected Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
Response.Redirect("Customer.aspx")
End Sub
Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim conn As New OleDbConnection("Provider=Microsoft.ACE.OleDb.12.0;Data Source=" + Server.MapPath("~/bookstore.accdb"))
RadioButton1.Visible = True
RadioButton2.Visible = True
RadioButton3.Visible = True
RadioButton4.Visible = True
RadioButton5.Visible = True
If (RadioButton1.Checked) Then
conn.Open()
Dim sql As String = "SELECT * FROM books WHERE price < $10.00"
Dim dbcomm As New OleDb.OleDbCommand(sql, conn)
Dim dbread = dbcomm.ExecuteReader()
bookSearching.DataSource = dbread
bookSearching.DataBind()
dbread.Close()
conn.Close()
End If
End Sub
Protected Sub RadioButton1_CheckedChanged(sender As Object, e As EventArgs) Handles RadioButton1.CheckedChanged
End Sub
End Class
For the hyperlink I used the vb option to design and dragged a sqlDataSource and a gridview and linked the two together with my bookstore.accdb file. You're help is much appreciated. Thanks a lot.

Vb.net Editing DatagridView

I created a simple program where I can search through a database table. Add data and remove data through a button. Now I want to be able to update the data within the datagrid view however i am getting the classic error: Dynamic SQL generation for the UpdateCommand is not supported against a SelectCommand that does not return any key column information.
I have the primary key in the data grid that is not attached to anything in the vb.net program nor in the database at this time. I am not sure what I am missing.
Public Class Form1
Dim cn As New SqlConnection("Data Source=;Initial Catalog=Inventory;User ID=;Password=")
Dim adap As New SqlDataAdapter("SELECT res_snbr, First_Name, Last_Name, Item FROM Inventory_Details", cn)
Dim builder As New SqlCommandBuilder(adap)
Dim dt As New DataTable
'Dim InventoryDetailsBindingSource As New BindingSource
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load
DataGridView1.AllowUserToAddRows = True
DataGridView1.AllowUserToDeleteRows = True
DataGridView1.[ReadOnly] = False
adap.InsertCommand = builder.GetInsertCommand()
' adap.UpdateCommand = builder.GetUpdateCommand()
adap.UpdateCommand = builder.GetUpdateCommand
adap.Fill(dt)
InventoryDetailsBindingSource.DataSource = dt
DataGridView1.DataSource = InventoryDetailsBindingSource
End Sub
Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged
If TextBox1.TextLength > 0 Then
InventoryDetailsBindingSource.Filter = String.Format("First_Name Like '%{0}%'", TextBox1.Text)
Else
InventoryDetailsBindingSource.Filter = String.Empty
End If
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Try
adap.Update(dt)
MessageBox.Show("Saved successfully")
Catch ex As Exception
MessageBox.Show("Error updating database")
End Try
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
adap.Update(dt)
DataGridView1.[ReadOnly] = True
Button2.Enabled = False
End Sub
End Class
Actually I was incorrect. I did not have a primary key set on my database. I dropped my table - recreated with primary key.

VB.NET Remove Item in ComboBox after placing in ListBox

How can I remove item in comboBox after
I choose it and put into listBox.
Here's my code. Please help me.
Imports System
Imports System.Data
Imports System.Data.SqlClient
Public Class frmAdvancePayment
Private Sub frmAdvancePayment_Load(sender As Object, e As EventArgs) Handles MyBase.Load
lstBillNum.Items.Clear()
Dim connection_string As String = "Data Source=.\sqlexpress;Initial Catalog=CreditAndCollection;Integrated Security=True"
Dim connection As New SqlConnection(connection_string)
connection.Open()
Dim sql As String = "select BillNum from tblBillingSched where Status ='Unpaid'"
Dim da As New SqlDataAdapter(sql, connection_string)
Dim dt As New DataTable
da.Fill(dt)
cmbBillNum.DataSource = dt
cmbBillNum.DisplayMember = "BillNum"
cmbBillNum.ValueMember = "BillNum"
connection.Close()
End Sub
Private Sub btnGet_Click(sender As Object, e As EventArgs) Handles btnGet.Click
lstBillNum.Items.Add(cmbBillNum.SelectedValue)
End Sub
Private Sub btnClear_Click(sender As Object, e As EventArgs) Handles btnClear.Click
lstBillNum.Items.Clear()
End Sub
End Class
it's what you expect ?
Private Sub ComboBox1_SelectedValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedValueChanged
Dim index As Integer = ComboBox1.SelectedIndex
ListBox1.Items.Add(ComboBox1.Items(index))
ComboBox1.Items.RemoveAt(index)
End Sub