Your UPDATE-query seems to be structurally fine. But please do use SQL parameters, because if you now include an apostrophe ('
) within your entered textbox values, the query will most likely crash. (Or worse: some malicious user might apply SQL injection to manipulate your database!)
But after the UPDATE-query, you execute a small SELECT query. This SELECT query returns only the NAME
of only the updated record. This single row with a single column is captured in a DataTable object, and that DataTable object is set as the new data source for your DataGridView.
Is that your real intention? In your place, I would probably want to select all relevant fields from the database table and include all records, so that everything will be shown/refreshed in the DataGridView...
You probably already have some SELECT query in your application's startup code to initially fill the DataGridView. I would personally put that code in a separate subroutine (called something like FillDataGridView
) and call that subroutine in the application's (or the form's) startup code and also call it after executing the UPDATE query. So there would not be any need to include a separate SELECT query after your UPDATE query.
You could check out the following code for some inspiration. Note that I haven't tested the code (because I do not have MySQL installed on my machine), so it might quite certainly require some additional tweaking and bugfixing inside your application's code.
Private Sub DB_CMD_FUNC(SENDER As String)
Dim SQL_CMD_TXT As New StringBuilder()
SQL_CMD_TXT.AppendLine("UPDATE `employees`")
SQL_CMD_TXT.AppendLine("SET")
SQL_CMD_TXT.AppendLine(" `NAME` = @name,")
SQL_CMD_TXT.AppendLine(" `FATHER_NAME` = @father_name,")
SQL_CMD_TXT.AppendLine(" `DOB` = @dob,")
SQL_CMD_TXT.AppendLine(" `DOJ` = @doj,")
SQL_CMD_TXT.AppendLine(" `POSITION` = @position,")
SQL_CMD_TXT.AppendLine(" `EMP_STATUS` = @status,")
SQL_CMD_TXT.AppendLine(" `SALARY` = @salary,")
SQL_CMD_TXT.AppendLine(" `EOS` = @eos,")
SQL_CMD_TXT.AppendLine(" `MOBILE` = @mobile'")
SQL_CMD_TXT.AppendLine("WHERE")
SQL_CMD_TXT.AppendLine(" `EMPLOYEE_ID` = @ID;")
Try
Dim ID As String = EMPLOYEE_EDIT_FRM.EDIT_ID_TXT.Text
If String.IsNullOrWhiteSpace(ID) Then
Throw New Exception("The ID is required.")
End If
Dim NAME As String = EMPLOYEE_EDIT_FRM.EDIT_NAME_TXT.Text
If String.IsNullOrWhiteSpace(NAME) Then
NAME = Nothing
End If
Dim FATHER_NAME As String = EMPLOYEE_EDIT_FRM.EDIT_FATHER_NAME_TXT.Text
If String.IsNullOrWhiteSpace(FATHER_NAME) Then
FATHER_NAME = Nothing
End If
Dim DD As Date
Dim DOB As Date?
Dim DOJ As Date?
If Date.TryParse(EMPLOYEE_EDIT_FRM.EDIT_DOB_TXT.Text, DD) Then
DOB = DD
Else
DOB = Nothing
End If
If Not Date.TryParse(EMPLOYEE_EDIT_FRM.EDIT_DOJ_TXT.Text, DD) Then
DOJ = DD
Else
DOJ = Nothing
End If
Dim POSITION As String = EMPLOYEE_EDIT_FRM.EDIT_POSITION_TXT.Text
If String.IsNullOrWhiteSpace(POSITION) Then
POSITION = Nothing
End If
Dim STATUS As String = EMPLOYEE_EDIT_FRM.EDIT_EMP_STATUS_TXT.Text
If String.IsNullOrWhiteSpace(STATUS) Then
STATUS = Nothing
End If
Dim DEC As Decimal
Dim SALARY As Decimal?
If Decimal.TryParse(EMPLOYEE_EDIT_FRM.EDIT_SALARY_TXT.Text, DEC) Then
SALARY = DEC
Else
SALARY = Nothing
End If
Dim EOS As Date?
If Date.TryParse(EMPLOYEE_EDIT_FRM.EDIT_EOS_TXT.Text, DD) Then
EOS = DD
Else
EOS = Nothing
End If
Dim MOBILE As String = EMPLOYEE_EDIT_FRM.EDIT_REMARKS_TXT.Text
If String.IsNullOrWhiteSpace(MOBILE) Then
MOBILE = Nothing
End If
Using con As New MySqlConnection("server=localhost; user=root; password=****; database=****;")
''Do not open the database connection just yet...
'con.Open()
Using cmd As New MySqlCommand(SQL_CMD_TXT.ToString(), con)
cmd.Parameters.Add("@id", MySqlDbType.VarChar).Value = ID
cmd.Parameters.Add("@name", MySqlDbType.VarChar).Value = NAME
cmd.Parameters.Add("@father_name", MySqlDbType.VarChar).Value = FATHER_NAME
cmd.Parameters.Add("@dob", MySqlDbType.Date).Value = DOB
cmd.Parameters.Add("@doj", MySqlDbType.Date).Value = DOJ
cmd.Parameters.Add("@position", MySqlDbType.VarChar).Value = POSITION
cmd.Parameters.Add("@status", MySqlDbType.VarChar).Value = STATUS
cmd.Parameters.Add("@salary", MySqlDbType.Decimal).Value = SALARY
cmd.Parameters.Add("@eos", MySqlDbType.Date).Value = EOS
cmd.Parameters.Add("@mobile", MySqlDbType.VarChar).Value = MOBILE
con.Open() 'Delayed opening the database connection until the very last moment.
cmd.ExecuteNonQuery()
End Using
''Explicitly closing the database connection is not necessary, because the disposal (triggered when the Using-block is ended) takes care of that.
'con.Close()
End Using
Catch ex As Exception
MessageBox.Show($"ERROR FOR SQL CMD EXECUTION SECTION - {ex.Message}",
"SQL CMD EXECUTION",
MessageBoxButtons.OK,
MessageBoxIcon.Error)
Exit Sub
End Try
FillDataGridView()
End Sub
Private Sub FillDataGridView()
Dim SQL_CMD_TXT As New StringBuilder()
SQL_CMD_TXT.AppendLine("SELECT")
SQL_CMD_TXT.AppendLine(" `ID`,")
SQL_CMD_TXT.AppendLine(" `NAME`,")
SQL_CMD_TXT.AppendLine(" `FATHER_NAME`,")
SQL_CMD_TXT.AppendLine(" `DOB`,")
SQL_CMD_TXT.AppendLine(" `DOJ`,")
SQL_CMD_TXT.AppendLine(" `POSITION`,")
SQL_CMD_TXT.AppendLine(" `EMP_STATUS`,")
SQL_CMD_TXT.AppendLine(" `SALARY`,")
SQL_CMD_TXT.AppendLine(" `EOS`,")
SQL_CMD_TXT.AppendLine(" `MOBILE`")
SQL_CMD_TXT.AppendLine("FROM")
SQL_CMD_TXT.AppendLine(" `employees`;")
Try
Dim dt As New DataTable()
Using con As New MySqlConnection("server=localhost; user=root; password=****; database=****;")
''Do not open the database connection just yet...
'con.Open()
Using cmd As New MySqlCommand(SQL_CMD_TXT.ToString(), con)
Using da As New MySqlDataAdapter(cmd)
con.Open() 'Delayed opening the database connection until the very last moment.
da.Fill(dt)
End Using
End Using
''Explicitly closing the database connection is not necessary, because the disposal (triggered when the Using-block is ended) takes care of that.
'con.Close()
End Using
With OBJECT_DATAGRIDVIEW
.DataSource = dt
.FirstDisplayedScrollingRowIndex = .RowCount - 1 'Scroll to the last row.
End With
Catch ex As Exception
MessageBox.Show($"ERROR FOR SQL CMD EXECUTION SECTION - {ex.Message}",
"SQL CMD EXECUTION",
MessageBoxButtons.OK,
MessageBoxIcon.Error)
End Try
End Sub