pass data to multiple textbox with 1 form vb.net - mysql

I have issue with passing data in VB.NET and I'm new in vb.net + mysql. I don't know is it possible or not. I have some TextBoxes and I want fill those with userID or something. I want to make this like a modal on php.
When I clik TextBox, 1 form shows up with ComboBox and submit button to pass data to TextBox on previous Form. I've done it with 1 TextBox, but I can't pass it to other TextBox. Here is my code:
Form1.vb
Private Sub TextBox4_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox4.Click
'This is method **in my logic** can throw back data to TextBox4 from Form2.vb'
Form2.Label1.Text = TextBox4.Name
Form2.Show()
End Sub
Form2.vb
Private Sub AddTeam_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Call getUSers()
End Sub
Sub getUSers()
Call connection()
Dim str As String
str = "SELECT id FROM users WHERE status='Active'"
cmd = New MySqlCommand(str, conn)
rd = cmd.ExecuteReader
If rd.HasRows Then
Do While rd.Read
ComboBox1.Items.Add(rd("id"))
Loop
Else
End If
ComboBox1.AutoCompleteMode = AutoCompleteMode.SuggestAppend
ComboBox1.AutoCompleteSource = AutoCompleteSource.ListItems
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim nik = ComboBox1.SelectedItem
'Here is the issue, if i pass to 1 TextBox (like TextBox4 as sample) it`s easy, but if pass to another TextBox i have no idea to do that'
Form1.TextBox4.Text = nik
Me.Refresh()
Me.Close()
End Sub
note: I know there are many ways to do so, like change TextBox to ComboBox and fill with data, but I want this method
I'm sorry for my poor logic in vb.net. I was better with PHP before.

Related

Passing (form1) Datagridview "ID" cell content to (form2) in textbox with the click of button

I have a question regarding to my problem on how to pass the value from datagridview or selected ID by the user then with a click of the button the Selected ID in Datagridview should pass into the other form in textbox and show. This is what I want to happen. Because I want to use the ID from form 1 to form 2 for my child database.
Actually my content in datagridview is from mysql as my database and load data automatically when it runs. If anyone could help me then
Public Class frmCurriculumCourses
Dim con As New MySqlConnection
Dim cmd As New MySqlCommand
Dim dt As New DataTable
Dim da As New MySqlDataAdapter
Dim ds As New DataSet
Dim ConnectionString As String = "server=127.0.0.1;user id=root;database=dbgradeinquiry;password=12345;"
Sub LoadData()
con.ConnectionString = ConnectionString
con.Open()
DataGridView1.AutoGenerateColumns = False
With cmd
.Connection = con
.CommandText = "SELECT * from tblcurriculumcourses"
End With
dt = New DataTable
da.SelectCommand = cmd
da.Fill(dt)
DataGridView1.DataSource = dt
con.Dispose()
con.Close()
da.Dispose()
End Sub
Private Sub frmCurriculum_Load(sender As Object, e As EventArgs) Handles MyBase.Load
LoadData()
End Sub
Private Sub btnViewCourses_Click(sender As Object, e As EventArgs) Handles btnViewCourses.Click
'This is the button that im going to use for passing the selected ID from datagridview(form1) to textbox(form2)
End Sub
End Class
If your going to pass the column 1 values of the Selected Row in Datagridview, then use following code,
Private Sub btnViewCourses_Click(sender As Object, e As EventArgs) Handles btnViewCourses.Click
Form2.show()
Form2.TextBox1.Text=DataGridView1.SelectedRows(0).Cells(0).Value.ToString
End Sub
or use this following command,
DataGridView1.Item(0, DataGridView1.CurrentRow.Index).Value.ToString
Create a module and a global variable:
Public Module GlobalVariables
Public cellID As String
End Module
Add this code to the btnViewCourses button (form1):
Form2.TextBox1.Text = cellID
Add this piece of code to your Form1 class:
Private Sub DataGridView1_CellClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellClick
cellID = DataGridView1.Rows(e.RowIndex).Cells(e.ColumnIndex).Value
End Sub
or click on datagridview > events > CellClick and add (same as above):
cellID = DataGridView1.Rows(e.RowIndex).Cells(e.ColumnIndex).Value
This code will pass a SELECTED SINGLE value.
How to use: Show form2 (don't forget), select a cell in datagridview with elft click, press the button.
Make sure you have a textbox named textbox2 in your form2. If you want to change the name do it and change the name in Form2.TextBox1.Text = cellID
If you want to pass the whole column (to a multiline textbox or a new datagridview) then store values one by one into the public variable or make your public variable an array. I would go with one by one.
Change your public variable from String to Integer if you are using integer numbers for IDs (you should).

BackgroundWorker not Running when it's needed

I am writing a program which lets users upload a large file, compare it to another large file uploaded before and return a list of new entries and discontinued entries.
This requires the program to run a few queries, so it takes a while for the program to complete the task.
Of course, this means that until the program is done with the task, the user cannot do anything else. To prevent that from happening I have included a BackgroundWorker to the project.
The problem is, the BackgroundWorker doesn't start, giving me the same problem.
Can you please help me with this problem? Thanks!
Code:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim opendialog As New OpenFileDialog
Dim filepath As String = ""
Dim cellvalue(20) As String
opendialog.Title = "Elija el archivo que quiere importar"
opendialog.Filter = "CSV (*.csv)|*.csv"
If opendialog.ShowDialog() = DialogResult.Cancel Then
Exit Sub
End If
filepath = Replace(opendialog.FileName, "\", "\\")
Label1.Visible = True 'This is supposed to appear first, but it doesn't appear until the end of the method.
'Reading CSV file content
Cmd.CommandText = "SELECT COUNT(*) AS cuenta FROM libros WHERE 1"
rs = Cmd.Execute
If rs("cuenta").Value = 0 Then
BackgroundWorker1.RunWorkerAsync()
'MySQL queries, some of which takes a long time due to the large file being processed
Beep()
MsgBox("Archivo exportado con éxito",, "Exito")
BackgroundWorker1.CancelAsync()
Else
BackgroundWorker1.RunWorkerAsync()
'MySQL queries, some of which takes a long time due to the large file being processed
Beep()
MsgBox("Archivo exportado con éxito",, "Exito")
BackgroundWorker1.CancelAsync()
End If
End Sub
Private Sub BackgroundWoker1_ProgressChanged(ByVal sender As Object, ByVal e As System.ComponentModel.ProgressChangedEventArgs) Handles BackgroundWorker1.ProgressChanged
' Update the progress bar
Me.ProgressBar1.Value = e.ProgressPercentage
End Sub
Private Sub BackgroundWorker1_RunWorkerCompleted(ByVal sender As Object, ByVal e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles BackgroundWorker1.RunWorkerCompleted
If e.Cancelled Then
Label1.Text = "Cancelled"
Else
Label2.Text = "Completed"
End If
End Sub
Private Sub BackgroundWorker1_DoWork(ByVal sender As System.Object,
ByVal e As System.ComponentModel.DoWorkEventArgs) _
Handles BackgroundWorker1.DoWork
' Do some time-consuming work on this thread.
System.Threading.Thread.Sleep(5)
End Sub
RunWorkerAsync is, as the name implies, an async method call. This means that the only work done here is to start the DoWork event handler and return immediately. Thus your code now has two paths of execution, the one in the DoWork event handler (that has just started its work and it has probably done nothing) and the code that follows the call to RunWorkerAsync (the code in the ButtonClick).
This last code shows a messagebox and then call CancelAsync, and you could imagine what this call do to your DoWork thread of execution.
So you just need to wait before displaying anything and the Completed event is exactly what you need to use to wait the completition of your DoWork event handler
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
.....
BackgroundWorker1.RunWorkerAsync()
' Remove these lines....
' Beep()
' MsgBox("Archivo exportado con éxito",, "Exito")
' BackgroundWorker1.CancelAsync()
End Sub
Private Sub BackgroundWorker1_RunWorkerCompleted(ByVal sender As Object, ByVal e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles BackgroundWorker1.RunWorkerCompleted
If e.Cancelled Then
Label1.Text = "Cancelled"
Else
Label2.Text = "Completed"
' Add the message here....'
Beep()
MsgBox("Archivo exportado con éxito",, "Exito")
' Of course at this point you don't need to cancel anything....'
End If
End Sub
Private Sub BackgroundWoker1_ProgressChanged(ByVal sender As Object, ByVal e As System.ComponentModel.ProgressChangedEventArgs) Handles BackgroundWorker1.ProgressChanged
Me.ProgressBar1.Value = e.ProgressPercentage
End Sub
Private Sub BackgroundWorker1_DoWork(ByVal sender As System.Object,
ByVal e As System.ComponentModel.DoWorkEventArgs) _
Handles BackgroundWorker1.DoWork
' Now suppose that your work here could be divided in 5 methods'
ExecuteMethod1()
backgroundWorker1.ReportProgress(20)
ExecuteMethod2)
backgroundWorker1.ReportProgress(40
ExecuteMethod3
backgroundWorker1.ReportProgress(60)
ExecuteMethod4
backgroundWorker1.ReportProgress(80)
ExecuteMethod5
backgroundWorker1.ReportProgress(100)
End Sub

Adding a checkbox in header of checkbox column in data grid view using windows form vb.net

I have a functioning data grid view which displays all data of my databases with corresponding checkboxes..
Private Sub Recipients_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
mycom.Connection = cn
mycom.CommandText = <SQL> SELECT Idno,Name,YearSec,Course,Organization FROM tbl_students </SQL>.Value
Dim myadap As New MySqlDataAdapter(mycom)
Dim mydt As New DataTable
mydt.Columns.Add("", GetType(Boolean))
myadap.Fill(mydt)
grdRecipients.DataSource = mydt
myadap.Dispose()
End Sub
I have a problem of creating a checkbox in the header of my checkbox column, can anyone help me, i didnt started any code yet.
Dim chkbx As New DataGridViewCheckBoxColumn()
DataGridView1.Columns.Add(chkbx)
chkbx.HeaderText = "Check Box"
chkbx.Name = "checkBox"
or manually
AddColumn... -> Type = DataGridViewCheckBoxColumn
valter

backgroundworker retrieve data from database. timeout expired

This is the first time I use background worker. I want to retrieve data around 20.000 rows, save it on dataadapter and showing in on a datagridview
it seems running but then message Timeout expired. The timeout period elapsed prior to completion of the operation or the server is not responding comes up.
What I have done is put : Connect Timeout=90000 on the connection string and da.SelectCommand.CommandTimeout = 2000000, which doesn't seems to have a better end result by make adding few extra zeroes. :(
Here is what I have done. What should I do to make it works. thank you
Dim da As MySqlDataAdapter
Dim resultDataTable As DataTable
Dim queryString As String
Private Sub BTNrefresh_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BTNrefresh.Click
queryString =" LONG QUERY "
bw.RunWorkerAsync()
End Sub
Private Sub bw_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles bw.DoWork
MethodGlobal.mySqlCon.Open()
da = New MySqlDataAdapter(queryString, MethodGlobal.mySqlCon)
myDataTable = New DataTable
da.Fill(myDataTable)
da.SelectCommand.CommandTimeout = 2000000
Me.DGVtest.DataSource = resultDataTable
End Sub
Private Sub backgroundworker_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
MethodGlobal.NewMysqlConnection("connection")
End Sub

Linq to SQL - SubmitChanges not working

So - I have a MySQL db with one employee table, using Devart Linq to SQL (for MySQL)
It has a primary key in the DataContext,
I can load the gridview,
select a row,
set an instance of Staff = to the ID and Name or the row selected in the gridview
set a Textbox = to Name of Staff
change the textbox and set the object Staff.Name to the textbox.....
but I cant submit the change back to the db!!!! Probably a stupid thing I'm doing as a newbie.
Public Class Form1
Dim db As New MydbContext.MydbDataContext
Dim bs = New BindingSource
Dim Staff As New MydbContext.Employee
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
' load employee data from MySQL db in datagrid
Dim StaffQuery = From s In db.Employees Select s
bs.DataSource = StaffQuery
DataGridView1.DataSource = bs
' this works fine
End Sub
Private Sub DataGridView1_CellClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellClick
'create an instance of the object Staff with the ID and Name from the datagrid
'is there a better way to do this?
Staff.EmployeeID = DataGridView1.Rows(e.RowIndex).Cells("EmployeeID").Value
Staff.Name = DataGridView1.Rows(e.RowIndex).Cells("Name").Value
MsgBox(Staff.EmployeeID & " " & Staff.Name)
TextBox1.Text = Staff.Name
'this works fine
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Staff.Name = TextBox1.Text 'edit the Name
MsgBox(Staff.EmployeeID & " " & Staff.Name) ' this works - shows new Staff.Name from textbox
db.SubmitChanges() ' this fails to make db changes
End Sub
End Class
I believe you need to add db.Staffs.InsertOnSubmit(Staff)
Where Staffs is the name of the collection of Staff objects.
However, rather than keep the context and object around all the time, why not just create the new Staff object when the user presses the save button?
Finally, you can call GetChangeSet() on your db context to see what changes are pending. This can help you understand when something strange happens.