textbox not showing autocomplete, the textbox just blinks - vb.net

Every single comment i have been reading said that this code works..
But when I try to type in text to the textbox no autocomplete appears and it just blinks.. please help,, i m new to winforms
Private Sub txsCusID_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txsCusID.TextChanged
Dim cmd As New SqlClient.SqlCommand("SELECT DISTINCT fname + ' ' + lname AS clist FROM tblclient WHERE id LIKE '%" + txsCusID.Text.ToString + "%'", sqlconstr)
Dim ds As New DataSet
Dim da As New SqlClient.SqlDataAdapter(cmd)
da.Fill(ds, "list")
Dim col As New AutoCompleteStringCollection
Dim i As Integer
For i = 0 To ds.Tables(0).Rows.Count - 1
col.Add(ds.Tables(0).Rows(i)("clist").ToString())
Next
txsCusID.AutoCompleteSource = AutoCompleteSource.CustomSource
txsCusID.AutoCompleteCustomSource = col
txsCusID.AutoCompleteMode = AutoCompleteMode.Suggest
End Sub

Here give this a try... I would suggest setting your table up and then loop through while adding to your source...
Dim dtCustomerNames As DataTable
dtCustomerNames = ds.Tables(0)
For Each row As DataRow In dtCustomerNames.Rows
'You have to continue to add your data here...'
txsCusID.AutoCompleteCustomSource.Add((row.Item("clist").ToString))
Next
txsCusID.AutoCompleteMode = AutoCompleteMode.Suggest
txsCusID.AutoCompleteSource = AutoCompleteSource.CustomSource
Let me know how it works out for you...
Thanks!

Related

How can I write a loop to replace this if statement for different buttons

I have an Access datatable named A. It has 90 rows and each row has 2 columns as follows:
I have a vb form that has 90 green buttons and code:
Private Sub _1st_Floor_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim myConnection As New OleDbConnection(myConnString)
Dim myCommand As New OleDbCommand("SELECT ID FROM A WHERE Busy=True", myConnection)
myConnection.Open()
Dim reader As OleDbDataReader
Dim dt As New DataTable
dt.Load(myCommand.ExecuteReader)
If dt.Rows(0).Item(0).ToString = 1 Then
Button1.BackColor = Color.Red
Button1.FlatAppearance.BorderColor = Color.Red
End If
If dt.Rows(1).Item(0).ToString = 2 Then
Button2.BackColor = Color.Red
Button2.FlatAppearance.BorderColor = Color.Red
End If
End Sub
This works fine, but I don't want to repeat the same If block over and over again for 90 buttons. How can I write a loop with just one set of code for all 90 buttons?
Loop through your records and find the corresponding buttons:
For Each row As DataRow In dt.Rows
Dim buttonName as String = "button" & row(0).ToString()
Dim cntrls() As Control = Me.Controls.Find(buttonName, True)
If cntrls IsNot Nothing Then
Dim btn As Button = TryCast(cntrls(0), Button)
If btn IsNot Nothing Then
btn.BackColor = Color.Red
btn.FlatAppearance.BorderColor = Color.Red
End If
End If
Next
This is dependent on the buttons' naming scheme being consistent.
Using your own "words" :) maybe not the most efficient way but I just wanted to write this by reusing your code as much as possible...
Private Sub _1st_Floor_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim myConnection As New OleDbConnection(myConnString)
Dim myCommand As New OleDbCommand("SELECT ID FROM A WHERE Busy=True", myConnection)
myConnection.Open()
Dim reader As OleDbDataReader
Dim dt As New DataTable
dt.Load(myCommand.ExecuteReader)
For index As Integer = 1 To 90
If dt.Rows(index - 1).Item(0).ToString = index.ToString Then
Dim button As Button = CType(Me.Controls("Button" + index.ToString), Button)
button.BackColor = Color.Red
button.FlatAppearance.BorderColor = Color.Red
End If
Next
End Sub
I used this post to get controls by name String

ListBox index changed shows wrong value from SQLite database

I am coding a VB.NET program, which uses SQLite as a database.
My problem is when the form is loading it is displaying all values from a particular column (questions in my case) from the database to the ListBox. Then if the user clicks on any item (ListBox index change), the corresponding value from the database (answer) displays in a RichTextBox. This works as expected.
When the user types something in a TextBox and clicks the search Button, it is expected to show the questions from the database according to TextBox.Text as a tag column in the database to the same ListBox. This also works fine.
The problem begins when the ListBox index changes now. If the search result is only one item in the ListBox, the index will be zero and the RichTextBox shows the first item of the database insted of the corresponding value of the ListBox item.
My code is here:
Sub showData()'''this shows questions when form loads
connect()
Dim da As New SQLiteDataAdapter("select * from elect", connection)
'Dim dt As New DataTable
Dim ds As New DataSet
da.Fill(ds, "elect")
Dim mySelectQuery As String = "select * from elect"
Dim sqConnection As New SQLiteConnection(connection)
Dim sqCommand As New SQLiteCommand(mySelectQuery, sqConnection)
'sqConnection.Open()
Try
Dim sqReader As SQLiteDataReader = sqCommand.ExecuteReader()
' Always call Read before accessing data.
Do While sqReader.Read()
Dim sName = sqReader.Item("question")
ListBox1.Items.Add(sName)
Loop
' always call Close when done reading.
sqReader.Close()
' Close the connection when done with it.
Finally
connection.Close()
End Try
End Sub
Public Sub NavigateRecords()
page1.Clear()''page is rich text box
connect()
Dim da As New SQLiteDataAdapter("select * from elect", connection)
'Dim dt As New DataTable
Dim ds As New DataSet
da.Fill(ds, "elect")
Dim mySelectQuery As String
mySelectQuery = "select * from elect"
Dim sqConnection As New SQLiteConnection(connection)
Dim sqCommand As New SQLiteCommand(mySelectQuery, sqConnection)
Dim num As Integer = Me.inc = Conversions.ToInteger(ListBox1.SelectedIndices.ToString)
MsgBox(num)
Me.inc = Conversions.ToInteger(ListBox1.SelectedIndex.ToString)
If (Me.inc > -1) Then
Dim ans As String = Conversions.ToString(ds.Tables.Item("elect").Rows.Item(Me.inc).Item(2))
page1.Text = (ans)
End If
End Sub
Private Sub search()
connect()
Dim da As New SQLiteDataAdapter("select * from elect", connection)
'Dim dt As New DataTable
Dim ds As New DataSet
da.Fill(ds, "elect")
Dim mySelectQuery As String = ("select * from elect WHERE tag like'%" & txtSearch.Text & "%' ")
Dim sqConnection As New SQLiteConnection(connection)
Dim sqCommand As New SQLiteCommand(mySelectQuery, sqConnection)
Try
Dim sqReader As SQLiteDataReader = sqCommand.ExecuteReader()
' Always call Read before accessing data.
Do While sqReader.Read()
Dim sName = sqReader.Item("question")
ListBox1.Items.Add(sName)
Loop
If ListBox1.Items.Count = 0 Then
MsgBox("Nothing Found ")
showData()
End If
' always call Close when done reading.
sqReader.Close()
' Close the connection when done with it.
Finally
connection.Close()
End Try
End Sub
Private Sub ListBox1_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles ListBox1.SelectedIndexChanged
page1.Visible = True
ListBox1.Visible = False
Label1.Visible = False
NavigateRecords()
End Sub

Select Combobox Value Based on Textbox.Text

I have a 'Drop Down' style combobox named EmployedCombobox1. I am trying to have EmployedComobox1 automatically select a value depending on the text that is written in a textbox located on another form.
The code below is how my EmployedCombobox is populated:
Private Sub getinstitutionname(ByVal p_institution1_id As Integer)
If sConnection.State = ConnectionState.Closed Then
sConnection.ConnectionString = Search.sqlConnect
sConnection.Open()
End If
Dim sqlAdapter As New MySqlDataAdapter
Dim sqlCommand As New MySqlCommand
Dim sqlTable As New DataTable
Dim InstitutionName As String
Dim sqlText As String = "select * from institution order by institution_name"
Dim InstitutionID As Integer
With sqlCommand
.CommandText = sqlText
.Connection = sConnection
End With
With sqlAdapter
.SelectCommand = sqlCommand
.Fill(sqlTable)
End With
EmployedComboBox1.Items.Clear()
EmployedComboBox1.SelectedIndex = -1
For i = 0 To sqlTable.Rows.Count - 1
InstitutionName = sqlTable.Rows(i)("institution_name")
InstitutionID = sqlTable.Rows(i)("institution_id")
EmployedComboBox1.Items.Add(InstitutionName)
If p_institution1_id = InstitutionID Then
EmployedComboBox1.SelectedIndex = i
End If
Next
sqlTable.Dispose()
sqlCommand.Dispose()
sqlAdapter.Dispose()
Next, the code below is me attempting to auto select the value based off a textbox.text:
Dim sqlAdapter1 As New MySqlDataAdapter
Dim sqlCommand1 As New MySqlCommand
Dim sqlTable1 As New DataTable
Dim sqlText1 As String = "select institution_name from institution where institution_name='" & Institution.InstitutionNameTextBox.Text & "'"
If Search.debugging = True Then
MsgBox(sqlText1)
End If
With sqlCommand1
.CommandText = sqlText1
.Connection = sConnection
End With
With sqlAdapter1
.SelectCommand = sqlCommand1
.Fill(sqlTable1)
End With
For i = 0 To sqlTable1.Rows.Count - 1
Me.EmployedComboBox1.SelectedItem = (sqlTable1.Rows(i)("institution_name"))
Next
sqlTable1.Dispose()
sqlCommand1.Dispose()
sqlAdapter1.Dispose()
When I run that second code, nothing gets selected in the combobox when there is text written in the Textbox.
like you say the values are the same so i would make a AutoCompleteStringCollection for the text box.
And on page load put the items that are in combo box also in the AutoCompleteStringCollection."or after you load you're combo box"
Then add it to the text box
Then for the selecting i would use AutoCompleteMode.Suggest otherwise it will select a item in the combo box if you type 1 letter in the text box.
Dim names As New AutoCompleteStringCollection()
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
ComboBox1.Items.AddRange({"yes", "no", "mmm"})
For Each item As String In ComboBox1.Items
names.Add(item)
Next
With TextBox1
.AutoCompleteMode = AutoCompleteMode.Suggest
.AutoCompleteCustomSource = names
.AutoCompleteSource = AutoCompleteSource.CustomSource
End With
End Sub
For the selecting of the combo box item , first check if the text box text exists in the combo box and then select it."like the code shows below"
i used TextBox1_TextChanged event for it.
Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged
If ComboBox1.Items.Contains(TextBox1.Text) = True Then
ComboBox1.SelectedIndex = (ComboBox1.FindString(TextBox1.Text))
End If
End Sub

How to save changes from DataGridView to the database?

I would like to save the changes made to a DataGridView into the database MS SQL CE,
but i can't, the changes are not saved to the database....
This the code (VB.net):
Private Sub Form1_Load(ByVal sender As System.Object,
ByVal e As System.EventArgs) handles MyBase.Load
Dim con As SqlCeConnection = New SqlCeConnection(#"Data Source=C:\Users\utente\Documents\test.sdf")
Dim cmd As SqlCeCommand = New SqlCeCommand("SELECT * FROM mytable", con)
con.Open()
myDA = New SqlCeDataAdapter(cmd)
Dim builder As SqlCeCommandBuilder = New SqlCeCommandBuilder(myDA)
myDataSet = New DataSet()
myDA.Fill(myDataSet, "MyTable")
DataGridView1.DataSource = myDataSet.Tables("MyTable").DefaultView
con.Close()
con = Nothing
End Sub
Private Sub edit_rec()
Dim txt1, txt2 As String
Dim indice As Integer = DataGridView1.CurrentRow.Index
txt1 = DataGridView1(0, indice).Value.ToString '(0 is the first column of datagridview)
txt2 = DataGridView1(1, indice).Value.ToString '(1 is the second) MsgBox(txt1 + " " + txt2)
'
DataGridView1(0, indice).Value = "Pippo"
DataGridView1(1, indice).Value = "Pluto"
'
Me.Validate()
Me.myDA.Update(Me.myDataSet.Tables("MyTable"))
Me.myDataSet.AcceptChanges()
'
End Sub
Thank you for any suggestion.
You need to use myDA.Update, that will commit the changes. This is because any changes that you are making are made in the local instance of that dataset. And therefore disposed of just like any other variable.
... I can see that in your edit_rec sub, but what is calling that - there is nothing in the code that you have posted.
A little late perhaps.
In the Form load event, you open and close the connection.
And furthermore, all are local variables who loose any value or data when leaving the sub
Public Class Form1
Dim con As SqlCeConnection
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
con = New SqlCeConnection(#"Data Source=C:\Users\utente\Documents\test.sdf")
Dim cmd As SqlCeCommand = New SqlCeCommand("SELECT * FROM mytable", con)
con.Open()
myDA = New SqlCeDataAdapter(cmd)
Dim builder As SqlCeCommandBuilder = New SqlCeCommandBuilder(myDA)
myDataSet = New DataSet()
myDA.Fill(myDataSet, "MyTable")
DataGridView1.DataSource = myDataSet.Tables("MyTable").DefaultView
cmd.Dispose()
End Sub
***Put the closing of connection here instead or somewhere else suitable when you don't use it anymore***
Private Sub Form1_FormClosed(sender As Object, e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
con.Close()
con = Nothing
End Sub
Private Sub edit_rec()
Dim txt1, txt2 As String
Dim indice As Integer = DataGridView1.CurrentRow.Index
txt1 = DataGridView1(0, indice).Value.ToString '(0 is the first column of datagridview)
txt2 = DataGridView1(1, indice).Value.ToString '(1 is the second) MsgBox(txt1 + " " + txt2)
'
DataGridView1(0, indice).Value = "Pippo"
DataGridView1(1, indice).Value = "Pluto"
'You code to update goes here and not in my scope of answer
End Sub
End Class
I think you want to add # with your connection string
SqlConnection con;
con = New SqlConnection(#"Data Source=C:\Users\utente\Documents\test.sdf");

"This row already belongs to this table"

I've read existing code in stackoverflow but I'm still lost as how do I fix this.
This row already belongs to this table.
Any suggestions always is appreciated.
It errors out on my command button save event.
I've listed code for the entire form just to give you an idea on what I'm doing. Hope this helps
Private Sub btnSave_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSave.Click
DataGridView1.EndEdit()
Dim con As SqlConnection = CompDB.GetConnection
Dim cmd As New System.Data.SqlClient.SqlCommand
Dim SPgetStandards As String = "dbo.SPgetStandards"
Dim SPgetRecordDetail As String = "dbo.SPgetRecordDetail"
Dim dsStandard As New DataSet
Dim dsRecords As New DataSet
Dim currentrow As DataRow
Dim currentcolumn As DataColumn
MsgBox("Record id is " & RecordID)
Using con
con.Open()
Dim Standardsdataadapter As New SqlClient.SqlDataAdapter(SPgetStandards, con)
Dim Detaildataadapter As New SqlClient.SqlDataAdapter(SPgetRecordDetail, con)
Detaildataadapter.Fill(dsRecords, "Record")
Standardsdataadapter.Fill(dsStandard, "Standard")
'5/31/13
'Figured out how to run a loop through a table and write to the console
For Each currentrow In dsStandard.Tables("Standard").Rows
For Each currentcolumn In dsStandard.Tables("Standard").Columns
'Debug.WriteLine(currentrow(currentcolumn) & ControlChars.Tab & ControlChars.Tab)
'MsgBox(currentcolumn.ToString)
'Debug.WriteLine(currentrow.Item(0))
Next currentcolumn
Next currentrow
' con.Close()
'con.Dispose()
Dim newrow As DataRow = dsRecords.Tables("Record").NewRow
For Each oRow As DataGridViewRow In DataGridView1.Rows
If Convert.ToInt16(oRow.Cells("chkbox").Value) = 1 Then
' Debug.WriteLine("It was clicked ", DataGridView1.Columns.Item(1))
Debug.WriteLine(oRow.Cells("ID").Value)
'dsStandard.Tables.Add.Rows("RecordID") = 1234
'dsRecords.Tables.Add.Rows("Standard_Name_ID") = 1234
' newrow("Record_Detail_ID") = RecordID
' newrow("Record_ID") = RecordID
newrow("Standard_Name_ID") = (oRow.Cells("ID").Value)
'/////Errors out here!!!!!dsRecords.Tables("Record").Rows.Add(newrow) '/////Errors out here!!!!!
dsRecords.AcceptChanges()
End If
Next
End Using
End Sub
Move this line:
Dim newrow As DataRow = dsRecords.Tables("Record").NewRow
To be the first line inside of your If block.