combobox is blank - vb.net

Seems to me that the combobox will only display string values. I have a table column that is integer that I need to be displayed in the combobox for the user to pick.
Dim cmd As New SqlCommand("Select ID From ATable", con)
Dim ds As New DataSet()
Dim adap As New SqlDataAdapter(cmd)
If con.State = ConnectionState.Closed Then con.Open()
adap.Fill(ds, "ATable")
con.Close()
comboBox1.DisplayMember = "ID"
comboBox1.DataSource = ds.Tables("ATable")
comboBox1.SelectedIndex = -1
combobox is just blank and I'm thinking it's because my fields are integers and not string. How can I make it display my integer values. EDIT: I needed to put the function in my public sub form initializecomponent()

put the function in public sub form after initializecomponent()

Related

How to use DataGridView checkbox in Combobox's Select Change event?

I want to create a mobile phone project in VB.net 2019. Where the model of the mobile phone in the Combobox came from the database. I have attached a datagridview where the IMEI number of the mobile phone is displayed. An attached checkbox in Datagridview. The problem is that when I go to change the model number of the mobile phone in the change event of Combobox, a new checkbox is added to every change in Datagridview.
I created a searchbox to easily find IEMI numbers in a textbox. When I search the IEMI number in the search box in the datagridview and check in the datagridview checkbox and search for the next number, the previous one is unchecked
I am praying for the help of the experienced.
Sub display_data()
con.Open()
Dim cmd As New SqlCommand("select Product_Name, IMEI_No,qty from tbl_Stock where Company_Name=#d1 and Product_Name =#d2", con)
cmd.Parameters.Add("#d1", SqlDbType.NVarChar).Value = cmb_cmp.Text
cmd.Parameters.Add("#d2", SqlDbType.NVarChar).Value = cmb_pro.Text
Dim da As New SqlDataAdapter(cmd)
Dim dt As New DataTable
dt.Clear()
da.Fill(dt)
DataGridView1.DataSource = dt
con.Close()
Dim chakbox As New DataGridViewCheckBoxColumn
chakbox.Width = 60
chakbox.Name = "checkbox"
DataGridView1.Columns.Insert(0, chakbox)
End Sub
Private Sub ComboBox2_SelectedIndexChanged(sender As Object, e As EventArgs) Handles cmb_pro.SelectedIndexChanged
con.Open()
Dim strsql As New SqlCommand("Select * from tbl_pro where Product_name=#d1", con)
strsql.Parameters.Add("#d1", SqlDbType.NVarChar).Value = cmb_pro.Text
Dim myreader As SqlClient.SqlDataReader = strsql.ExecuteReader
While myreader.Read
txt_sal_rt.Text = myreader.GetDecimal("sale_rate")
txt_pro_cod.Text = myreader.GetString("product_code")
txt_pur_rt.Text = myreader.GetDecimal("Purchase_rate")
End While
con.Close()
display_data()
End Sub
Private Sub txt_serch_KeyUp(sender As Object, e As KeyEventArgs) Handles txt_serch.KeyUp
DataGridView1.DataSource = searech()
End Sub
Private Function searech() As DataTable
Dim cmd As New SqlCommand("select Product_Name, IME_No,qty from tbl_Stock where Company_Name=#d1 and IME_NO Like '%' +#d2 + '%'", con)
cmd.Parameters.Add("#d1", SqlDbType.NVarChar).Value = cmb_cmp.Text
cmd.Parameters.Add("#d2", SqlDbType.VarChar).Value = txt_serch.Text
Dim da As New SqlDataAdapter(cmd)
Dim dt As New DataTable
da.Fill(dt)
Return dt
End Function
Just put a bool in your query, throw that "add a checkbox to the grid" code away
Sub display_data()
con.Open()
Dim cmd As New SqlCommand("select CAST(0 as BIT) as checkbox, Product_Name, IMEI_No,qty from tbl_Stock where Company_Name=#d1 and Product_Name =#d2", con)
cmd.Parameters.Add("#d1", SqlDbType.NVarChar).Value = cmb_cmp.Text
cmd.Parameters.Add("#d2", SqlDbType.NVarChar).Value = cmb_pro.Text
Dim da As New SqlDataAdapter(cmd)
Dim dt As New DataTable
dt.Clear()
da.Fill(dt)
DataGridView1.DataSource = dt
con.Close()
End Sub

Refresh DataGridView vb.net

I have DataGridView which doesn't refresh. I was hoping to be able to refresh it by clicking on the 'Refresh' button.
My database is called 'ProjectDatabase' and the table I want to use is called 'SWOT'.
I'v tried a few ways but none have been working, and if it does work then I get new issues like every time I click on refresh it duplicates the data. Im using a MS Acess Database.
I tried the below coding, it refreshes, but duplicates the data x2 every time I click Refresh:
Private Sub btnRefresh_Click(sender As Object, e As EventArgs) Handles btnRefresh.Click
Refreshdata()
End Sub
Dim myConnection As New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Users\ahmed\OneDrive\Desktop\ProjectDatabase2003.mdb")
Dim DS As DataSet = New DataSet
Dim DA As OleDbDataAdapter
Dim tables As DataTableCollection = DS.Tables
Dim source1 As New BindingSource()
Private Sub Refreshdata()
DA = New OleDbDataAdapter("Select * from SWOT", myConnection)
DA.Fill(DS, "SWOT_Report")
Dim view1 As New DataView(tables(0))
source1.DataSource = view1
DataGridView1.DataSource = view1
DataGridView1.Refresh()
End Sub
I think that you are overcomplicating your code with the use of the BindingSource, DataSet, DataAdapter, and DataTable member (class scope) variables.
For the member variables, strip them down to these two:
Dim ConnectionString As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Users\ahmed\OneDrive\Desktop\ProjectDatabase2003.mdb"
Dim swotBindingSource As BindingSource
Please note, the myConnection object has been removed and replaced with a Connection String variable instead
In the Forms New() method add:
swotBindingSource = New BindingSource()
DataGridView1.DataSource = swotBindingSource
This will setup your DataGridView to have the swotBindingSource as its DataSource. The BindingSource can be of use later on when it comes to filtering data.
And, finally change the RefreshData sub to be:
Private Sub Refreshdata()
Dim swotDataTable As New DataTable
Using conn As New OleDbConnection(ConnectionString)
Using DA As New OleDbDataAdapter("SELECT * FROM SWOT", conn)
Dim ds As New DataSet
DA.Fill(ds, "SWOT_Report")
swotBindingSource.DataSource = ds.Tables(0)
End Using
End Using
End Sub
When the swotBindingSource gets updated, you should automatically see the updates in the DataGridView.
It is likely you will also have to make other changes to the rest of your code due to the lack of myConnection object, you will be better off creating a new Connection object (similar to how it is done in the RefreshData sub) each time you need to use the database rather than creating one when the application starts and re-using the same connection.
What you need to do is clear your DataTable before filling it again. .NET doesn't do that automatically.
Private Sub Refreshdata()
'-- clear table before filling
If DS.Tables("SWOT_Report") IsNot Nothing Then DS.Tables("SWOT_Report").Rows.Clear
DA = New OleDbDataAdapter("Select * from SWOT", myConnection)
DA.Fill(DS, "SWOT_Report")
Dim view1 As New DataView(tables(0))
source1.DataSource = view1
DataGridView1.DataSource = view1
DataGridView1.Refresh()
End Sub

textbox AutoComplete not working after a search query

Hello there I am quite new to windows form programming, and my project requires me to do a search query on my database. I would like the option for the names that can be currently searched to be displayed when typing, however, after pressing the search button the autocomplete no longer displays when I try to look for another attribute. https://i.stack.imgur.com/Wytdy.png , https://i.stack.imgur.com/Qjy5q.png
Dim con As New SQLiteConnection(ConnectionString)
Dim cmd As New SQLiteCommand(mSQL, con)
con.Open()
Dim da As New SQLiteDataAdapter(cmd)
da.Fill(ds, "customers")
dt = ds.Tables(0)
MaxRows = ds.Tables("customers").Rows.Count
con.Close()
Dim msSQL As String = "SELECT * FROM customers;"
dgvCustomerInfo.DataSource = display(msSQL, "customers")
Try
dt = New DataTable
con.Open()
With cmd
.Connection = con
.CommandText = "SELECT DISTINCT fname FROM customers"
End With
da.SelectCommand = cmd
da.Fill(dt)
Dim r As DataRow
txtSearchName.AutoCompleteCustomSource.Clear()
For Each r In dt.Rows
txtSearchName.AutoCompleteCustomSource.Add(r.Item(0).ToString)
Next
''''''''''''''''''''''''
Catch ex As Exception
MsgBox(ex.Message)
End Try
con.Close()
da.Dispose()
I believe your problems stem from the AutoCompleteMode. Suggest and SuggestAppend seem to fill in the text box as expected. Append seem to do nothing. I don't think this is working as intended.
I tested with a little database I have. It is Sql Server but should work the same for Sqlite. I used a bit of Linq magic to get the AutoCompleteCustomSource. A few other changes... Using...End Using blocks ensure that database objects are closed and disposed. You don't need a DataAdapter, just a DataTable and Command.
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim dt = GetDataForTextBox()
Dim strArray = dt.AsEnumerable().[Select](Function(x) x.Field(Of String)("Name")).ToArray()
TextBox5.AutoCompleteSource = AutoCompleteSource.CustomSource
Dim MySource As New AutoCompleteStringCollection()
MySource.AddRange(strArray)
TextBox5.AutoCompleteCustomSource = MySource
TextBox5.AutoCompleteMode = AutoCompleteMode.SuggestAppend
End Sub
Private Function GetDataForTextBox() As DataTable
Dim dt As New DataTable
Using cn As New SqlConnection(My.Settings.CoffeeConnection),
cmd As New SqlCommand("Select Distinct Name From Coffees", cn)
cn.Open()
dt.Load(cmd.ExecuteReader)
End Using
Return dt
End Function
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim dt = GetSearchResults(TextBox5.Text)
DataGridView1.DataSource = dt
End Sub
Private Function GetSearchResults(name As String) As DataTable
Dim dt As New DataTable
Using cn As New SqlConnection(My.Settings.CoffeeConnection),
cmd As New SqlCommand("Select * From Coffees Where Name = #Name", cn)
cmd.Parameters.Add("#Name", SqlDbType.VarChar, 100).Value = name
cn.Open()
dt.Load(cmd.ExecuteReader)
End Using
Return dt
End Function

How to add a blank field in a DataGridViewComboBoxColumn

I have a DataGridViewComboBoxColumn that works and populates well. All I want to know is how to add a blank field. This way if the user inadvertently selects something they can select the blank row rather than pressing Ctrl-0.
In my code I tried adding Items.Add(DBNull.Value) but to no avail, as shown in my code. All it does is give me an error which is why it is rem'd out.
Any ideas would be greatly appreciated.
Private Sub FillCombobox(strHeaderText As String, strPropertyName As String, strTableName As String)
Using cboBox As New DataGridViewComboBoxColumn
With cboBox
.HeaderText = strHeaderText
.DataPropertyName = strPropertyName
.DataSource = GetData("Select * From " & strTableName)
.ValueMember = .DataPropertyName
'.Items.Add(DBNull.Value)
End With
dgrMain.Columns.Add(cboBox)
End Using
End Sub
EDIT
Private Function GetData(ByVal sqlCommand As String) As DataTable
Dim da = New OleDbDataAdapter(sqlCommand, strConn)
Dim con As New OleDbConnection(strConn)
con.Open()
Dim command As OleDbCommand = New OleDbCommand(sqlCommand, con)
da.SelectCommand = command
Dim dt As DataTable = New DataTable()
da.Fill(dt)
con.Close()
Return dt
End Function

Load Access from Datatable to Datset to DataGridView and Update Changes in DataGridView to Datatable

I need to load a DataTable from Microsoft Access into a DataSet using OleDb. I need to load that DataSet into a DataGridView. I then need to make changes to the DataGridView and update those changes in the original DataTable in Microsoft Access.
Here is my code so far:
Public tblName As String = "Criteria"
Dim ds As New DataSet()
Dim da As OleDbDataAdapter
Dim cmdBuilder As OleDbCommandBuilder
Dim Bsource As New BindingSource
Public Sub Show_Panel_Manage_Calculations()
Panel_Manage_Calculations.Show()
Nordeen_Investing_3.con.Open()
da = New OleDbDataAdapter("SELECT Calculation, [Interval], Formula FROM " & tblName & "", Nordeen_Investing_3.con)
cmdBuilder = New OleDbCommandBuilder(da)
da.Fill(ds, "Criteria")
Bsource.DataSource = ds
DataGridView_Manage_Calculations.DataSource = Bsource
Nordeen_Investing_3.con.Close()
End Sub
Private Sub Button_Update_Click(sender As Object, e As EventArgs) Handles Button_Update.Click
Nordeen_Investing_3.con.Open()
da.Update(ds, "Criteria")
Nordeen_Investing_3.con.Close()
End Sub
Right now my data from the DataTable is not being displayed in my DataGridView.
The DataSource expects a Table, not the whole DataSet. Also you don't need the BindingSource part. Sample code:
DataGridView_Manage_Calculations.DataSource = ds.Tables(0) 'By assuming that you want the first table