Linq to SQL - SubmitChanges not working - linq-to-sql

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.

Related

pass data to multiple textbox with 1 form vb.net

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.

deleting data via datagrid view mySQL Vb.net

When I click the delete button, the data in the mysqldatabase is deleted but the data in the datagridview is still there. How can I delete the data in the datagridview?
This is my code
cmd.CommandText = "delete from pawn where category = '" & txtCategory.Text & "'"
dr = cmd.ExecuteReader
You can add two buttons, button Delete and button Update to your form.After that you can add the event handler as follows.
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)
'yourdatadaptername'.Update('yourdatatablename')
End If
End Sub
Private Sub save_btn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles save_btn.Click
'yourdatadaptername'.Update('yourdatatablename')
End Sub
End Class

MySQL Query Execution with Visual Basic

I am attempting to make a connection to MySQL server, take input from a text box, the use the information to run query and store it in a listbox.
To go even further I would like to and make the listbox update as I type in the text box.
Whenever I run run the vb nothing populates. I even tried changing the query to something basic, select * from securePasswordList.users; And I can't get anything to populate.
Any suggestions or criticism? THanks! Still learning quite a bit about VB...and a long ways to go.
Imports MySql.Data.MySqlClient
Public Class Reveal
Public Property MyDataClass As SecurePasswordList
Dim lastNameInput As String
Dim firstNameInput As String
Dim connStr As String = "server=0.0.0.0;user=johndoe;database=securePasswordList;port=3306;password=password;"
Dim conn As New MySqlConnection(connStr)
Dim SQL As String = "select password from securePasswordList.users where LAST_NAME like ' " & lastNameInput & "%'"
Dim cmd As MySqlCommand = New MySqlCommand(SQL, conn)
Private Sub Reveal_Load(sender As Object, e As EventArgs) Handles MyBase.Load
End Sub
Private Sub firstName_TextChanged(sender As Object, e As EventArgs) Handles lastNameTextBox.TextChanged
lastNameInput = lastNameTextBox.Text
listBox.Items.Add(cmd)
End Sub
Private Sub lastName_TextChanged(sender As Object, e As EventArgs) Handles firstNameTextBox.TextChanged
firstNameInput = firstNameTextBox.Text
End Sub
Private Sub listBox_SelectedIndexChanged(sender As Object, e As EventArgs) Handles listBox.SelectedIndexChanged
End Sub
End Class
One of your issues is that you are not actually executing the MySqlCommand anywhere. Also, I would like to point you to this link, since you have a potential security issue in your current code in the way you build your select-string.
Here is an example of how you could retrieve a password:
Private Function RetrievePassword(ByVal lastName As String) As String
Using conn As New MySqlConnection(connStr)
Using comm As New MySqlCommand(query, conn)
comm.Parameters.AddWithValue("#LastName", lastName & "%'")
Dim result As Object = comm.ExecuteScalar()
If result Is Nothing Then
handle as you like...
End If
Return result.ToString
End Using
End Using
End Function
query would contain a placeholder for the parameter like so:
Private Const query As String =
"select password from securePasswordList.users where LAST_NAME like #LastName"

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