I have a script to update the value from datagridview to database.
I do use a button insert ToolStripButton but it is not working.
Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
conn = New SqlConnection("")
conn.Open()
Dim query As [String] = "SELECT * FROM dtaTraLoiChoVanBan"
da = New SqlDataAdapter(query, conn)
Dim cb As New SqlCommandBuilder(da)
dt = New DataTable()
da.Fill(dt)
C1FlexGrid1.DataSource = dt
conn.Close()
End Sub
Private Sub ToolStrip1_ItemClicked(ByVal sender As System.Object, ByVal e As System.Windows.Forms.ToolStripItemClickedEventArgs) Handles ToolStrip1.ItemClicked
Select Case e.ClickedItem.Name
Case "ToolStripButton1" '---> button insert
da.Update(dt)
End Select
End Sub
I think your code will reload the grid every time and wipe any changes your user is adding. Try running the grid binding only the first time, by checking for Page.IsPostBack(). You also never used the CommandBuilder to generate an INSERT statement.
Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
conn = New SqlConnection("") 'blank connection string won't really work
conn.Open()
Dim query As [String] = "SELECT * FROM dtaTraLoiChoVanBan"
da = New SqlDataAdapter(query, conn)
Dim cb As New SqlCommandBuilder(da)
cb.GetInsertCommand()
dt = New DataTable()
da.Fill(dt)
conn.Close()
If Not Page.IsPostBack() Then
C1FlexGrid1.DataSource = dt
End If
End Sub
I had same problem. I just added validate to solve it.
Private Sub ToolStripButton5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton5.Click
Me.Validate()
DataGridView1.EndEdit()
da.Update(dt)
MsgBox("Data Updated!!")
Me.BindGrid()
End Sub
Related
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
Dim dt As DataTable
Dim dv As DataView
dt = DirectCast(DataGridView1.DataSource, DataTable)
dv = New DataView(dt, "[Game Name]='DERA'", "[Game Name] desc", DataViewRowState.CurrentRows)
DataGridView1.DataSource = dv
End Sub
this is my code is working , But I need this code filter by combobox1 as like next code
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
Dim dt As DataTable
Dim dv As DataView
dt = DirectCast(DataGridView1.DataSource, DataTable)
dv = New DataView(dt, "[Game Name]" = ComboBox2.SelectedItem, "[Game Name] desc", DataViewRowState.CurrentRows)
DataGridView1.DataSource = dv
End Sub
Imports System.Data.OleDb
Public Class Form1
Dim Dutycon As OleDbConnection
Dim DutyAdo As OleDbDataAdapter
Dim DutyCmd As OleDbCommandBuilder
Dim DutyI As New Integer
Dim DutyTab As New DataTable
Dim DutyRow As DataRow
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Me.Duty_EmployeeTableAdapter.Fill(Me._Duty_DatabaseDataSet1.Duty_Employee)
Dutycon = New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0; Datasource=Duty-Database.accdb; Presist security info=false;")
DutyAdo = New OleDbDataAdapter("Select * from Duty_Employee", Dutycon)
DutyCmd = New OleDbCommandBuilder(DutyAdo)
Try
Dutycon.Open()
MsgBox("Database opened")
Catch ex As Exception
MsgBox("Database Not connected -- -.. " + ex.Message)
End Try
DutyAdo.Fill(DutyTab)
DataGridView1.DataSource = DutyTab
End Sub
Private Sub Duty_EmployeeBindingNavigatorSaveItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Duty_EmployeeBindingNavigatorSaveItem.Click
Me.Validate()
Me.Duty_EmployeeBindingSource.EndEdit()
Me.TableAdapterManager.UpdateAll(Me._Duty_DatabaseDataSet1)
End Sub
Private Sub Cmd_Add_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Cmd_Add.Click
Dutycon.Open()
DutyRow = DutyTab.NewRow
DutyRow("Emp_No") = TextBox1.Text
DutyRow("Emp_Name") = TextBox2.Text
DutyRow("Emp_salary") = TextBox3.Text
DutyTab.Rows.Add(DutyRow)
DutyAdo.Update(DutyTab)
MsgBox("New Record added", MsgBoxStyle.Information, "Record Stored")
Dutycon.Close()
End Sub
End Class
Connection () can't open
Could not find installeble ISAM
I have Ms-Office 2007 (Access) and connect to VB.net through "ACE.OLEDB.12.0"
I am creating an inventory application which runs off of an Access DB in visual studio, using visual basic. I can populate my data grid view just fine, but when I try to add new information into the data grid view, it does not number correctly and it does not append my database.
I have tried binding and updating using the table adapter.
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
CustomersBindingSource.AddNew()
Me.Validate()
Me.CustomersTableAdapter.Update(Me.Database1DataSet.Customers)
Me.CustomersBindingSource.EndEdit()
End Sub
Here is my code:
Public Class Form1
Private Sub enterbtn_Click(sender As Object, e As EventArgs) Handles enterbtn.Click
If username.Text = "Tanner" And password.Text = "bmis365" Then
GroupBox1.Visible = False
Else
MsgBox("Incorrect Username or Password, please try again.")
username.Clear()
password.Clear()
username.Focus()
End If
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
DataGridView1.CurrentCell = Nothing
'This line of code loads data into the 'Database1DataSet.Customers' table. You can move, or remove it, as needed.
Me.CustomersTableAdapter.Fill(Me.Database1DataSet.Customers)
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
'This is where my new info is to be appended into the database, once the button is clicked.
CustomersBindingSource.AddNew()
Me.Validate()
Me.CustomersTableAdapter.Update(Me.Database1DataSet.Customers)
Me.CustomersBindingSource.EndEdit()
End Sub
Private Sub searchbtn_Click(sender As Object, e As EventArgs) Handles searchbtn.Click
'The Following Code is from https://social.msdn.microsoft.com/Forums/vstudio/en-US/36c54726-4f49-4e15-9597-7b201ec13ae7/search-in-datagrid-using-textbox-vbnet-without-data-connectivity?forum=vbgeneral
For Each row As DataGridViewRow In DataGridView2.Rows
For Each cell As DataGridViewCell In row.Cells
If Not IsNothing(cell.Value) Then
If cell.Value.ToString.StartsWith(searchbar.Text, StringComparison.InvariantCultureIgnoreCase) Then
cell.Selected = True
DataGridView2.CurrentCell = DataGridView2.SelectedCells(0)
End If
End If
Next
Next
End Sub
End Class
My output initially has 3 rows(numbered 1, 2, and 3) but any that are added through the application have the numbers -1, -2, -3 and so on. Also, when I close the program and restart it, my original rows (1, 2, and 3) are still there from when I entered them in the DB file, but any that were added through my application are gone.
Here is one way to do an Update, as well as a few other common SQL manipulations/operations.
Imports System.Data.SqlClient
Public Class Form1
Dim sCommand As SqlCommand
Dim sAdapter As SqlDataAdapter
Dim sBuilder As SqlCommandBuilder
Dim sDs As DataSet
Dim sTable As DataTable
Private Sub load_btn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles load_btn.Click
Dim connectionString As String = "Data Source=.;Initial Catalog=pubs;Integrated Security=True"
Dim sql As String = "SELECT * FROM Stores"
Dim connection As New SqlConnection(connectionString)
connection.Open()
sCommand = New SqlCommand(sql, connection)
sAdapter = New SqlDataAdapter(sCommand)
sBuilder = New SqlCommandBuilder(sAdapter)
sDs = New DataSet()
sAdapter.Fill(sDs, "Stores")
sTable = sDs.Tables("Stores")
connection.Close()
DataGridView1.DataSource = sDs.Tables("Stores")
DataGridView1.ReadOnly = True
save_btn.Enabled = False
DataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect
End Sub
Private Sub new_btn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles new_btn.Click
DataGridView1.[ReadOnly] = False
save_btn.Enabled = True
new_btn.Enabled = False
delete_btn.Enabled = False
End Sub
Private Sub delete_btn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles delete_btn.Click
If MessageBox.Show("Do you want to delete this row ?", "Delete", MessageBoxButtons.YesNo) = DialogResult.Yes Then
DataGridView1.Rows.RemoveAt(DataGridView1.SelectedRows(0).Index)
sAdapter.Update(sTable)
End If
End Sub
Private Sub save_btn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles save_btn.Click
sAdapter.Update(sTable)
DataGridView1.[ReadOnly] = True
save_btn.Enabled = False
new_btn.Enabled = True
delete_btn.Enabled = True
End Sub
End Class
When I try to click the Button1 ,2 and 3. The error that is stated below happens. This error always occur when I have two tables on my database.
No value given for one or more required parameters
This is my code
Imports System.Data.OleDb
Module MSAccessConnection
Public Function OpenConnection() As String
Dim connString As String = "Provider=Microsoft.ACE.OLEDB.12.0; Data Source=G:\MerdoresNew\OrderData.accdb"
Return connString
End Function
End Module
Public Class Order
Public total As Integer
Public ordered As Integer
Public ordered2 As Integer
Public ordered3 As Integer
Public price As Integer
Dim myConnection As OleDbConnection = New OleDbConnection
Private Sub Order_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'TODO: This line of code loads data into the 'OrderDatabaseDataSet.Items' table. You can move, or remove it, as needed.
End Sub
Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
myConnection.ConnectionString = OpenConnection()
myConnection.Open()
'the query:
Dim cmd As OleDbCommand = New OleDbCommand("SELECT [Price] FROM [Items] where Item='Cheese Burger'", myConnection)
Dim dr As OleDbDataReader = cmd.ExecuteReader
' the following variable is hold true if user is found, and false if user is not found
Dim Found As Boolean = False
While dr.Read
Found = True
price = dr("Price")
End While
If Found = True Then
ordered = TextBox1.Text * price
Else
MsgBox("Saved!")
Me.Close()
End If
myConnection.Close()
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
myConnection.ConnectionString = OpenConnection()
myConnection.Open()
'the query:
Dim cmd As OleDbCommand = New OleDbCommand("SELECT [Price] FROM [Items] where Item='Ham Burger'", myConnection)
Dim dr As OleDbDataReader = cmd.ExecuteReader
' the following variable is hold true if user is found, and false if user is not found
Dim Found As Boolean = False
While dr.Read
Found = True
price = dr("Price")
End While
If Found = True Then
ordered2 = TextBox2.Text * price
Else
MsgBox("Saved!")
End If
myConnection.Close()
End Sub
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
total = ordered + ordered2 + ordered3
Form1.TextBox1.Text = total
End Sub
Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
myConnection.ConnectionString = OpenConnection()
myConnection.Open()
'the query:
Dim cmd As OleDbCommand = New OleDbCommand("SELECT [Price] FROM [Items] where Item='Chicken Sandwich'", myConnection)
Dim dr As OleDbDataReader = cmd.ExecuteReader
' the following variable is hold true if user is found, and false if user is not found
Dim Found As Boolean = False
While dr.Read
Found = True
price = dr("Price")
End While
If Found = True Then
ordered3 = TextBox3.Text * price
Else
MsgBox("Saved!")
End If
myConnection.Close()
End Sub
End Class
I don't see where you are passing your connection string to the button click handler. You DIM the connection string as connString, but you never call it in the handler.
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
myConnection.ConnectionString = OpenConnection() <----- Change ConnectionString to connString
myConnection.Open()
I think that's the variable that you're missing. I would have commented but I don't have enough rep to comment yet.
I'm unable to get the listbox display values from my database.This is the code. ANy thoughts on how to crack it?
Imports System.Data.OleDb
Public Class Form1
Dim dbConnection As OleDbConnection
Dim dbCommand As OleDbCommand
Dim strInsert As String
Dim dbDataAdapter As OleDbDataAdapter
Dim ConnectString As String = "Provider = Microsoft.Jet.OLEDB.4.0;" & "Data Source =atg.mdb"
Dim dtATG As DataTable
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim dtTest As New DataTable
dtTest.Columns.Add("Col1", GetType(Integer))
For i As Integer = 1 To 10
dtTest.Rows.Add(i, "Row " & i.ToString)
Next
ListBox1.DisplayMember = "Col1"
ListBox1.ValueMember = "Col1"
ListBox1.DataSource = dtTest.DefaultView
AddHandler ListBox1.SelectedIndexChanged, AddressOf ListBox1_SelectedIndexChanged
End Sub
Private Sub ListBox1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs)
MessageBox.Show(ListBox1.SelectedValue.ToString)
End Sub
End Class
Here is a modified version of your code that works.
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
Dim dtTest As New DataTable
dtTest.Columns.Add("Col1", GetType(Integer))
For i As Integer = 1 To 10
dtTest.Rows.Add(i)
Next
ListBox1.DisplayMember = "Col1"
ListBox1.ValueMember = "Col1"
ListBox1.DataSource = dtTest
End Sub
Use the System.IO.File.ReadAllLines:
ListBox1.Items.AddRange(System.IO.File.ReadAllLines("C:\folder\Your File.txt"))