in vb.net datagrideview filter by Combobox - vb.net

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

Related

System.Data.OleDb.OleDbException: 'Data type mismatch in criteria expression.' VB. Net

Imports System.Data.OleDb
Public Class Form1
Dim con As New OleDbConnection
Dim sql As String
Dim cmd As OleDbCommand
Dim odr As OleDbDataReader
Dim dt As New DataTable
Dim ds As New DataSet
Dim da As OleDbDataAdapter
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
con.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Users\e_alq\OneDrive\Desktop\a3\test.mdb"
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
con.Open()
da = New OleDbDataAdapter("Select * from test where basicsalary > 1 ", con)
ds.Tables.Add(dt)
DataGridView1.DataSource = dt.DefaultView
da.Fill(dt)
ds.Tables.Clear()
con.Close()
End Sub
Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
End Sub
End Class

Why is does this error happen? "No value given for one or more required parameters"

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.

update row from datagridview to database

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

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

Populate Listbox with Access Database?

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"))