I tried the equation separately. It worked. But after I joined, i got the error of conversion. My aim is to get MySql data to the vb.net So I can check some values and develop the project. I have to finish the project within one week and I don't know how to finish this. If this seems easy please forgive me.
Imports MySql.Data.MySqlClient
Public Class Form3
Dim conn As MySqlConnection
Dim command As MySqlCommand
Dim cmd As MySqlCommand
Dim Da As New MySqlDataAdapter
Dim ds As New DataSet
Private Sub Form3_Load(sender As Object, e As EventArgs) Handles MyBase.Load
'To check whether the date is same
TextBox1.Text = System.DateTime.Now.ToString(("MM/dd/yyyy"))
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
conn = New MySqlConnection
conn.ConnectionString = "server=localhost;user=root;password=1234;database=attendance"
Dim reader As MySqlDataReader
Try
ds.Clear()
conn.Open()
'Checking Subject Now
cmd = New MySqlCommand("select Subject_Name from dateverification", conn)
Da = New MySqlDataAdapter(cmd)
Da.Fill(ds, "dateverification")
TextBox2.Text = ds.Tables(0).Rows(0).Item(0)
'Checking Todays Date
cmd = New MySqlCommand("select Today_Date from dateverification", conn)
Da = New MySqlDataAdapter(cmd)
Da.Fill(ds, "dateverification")
Label1.Text = ds.Tables(0).Rows(0).Item(0)
'Checking Count1
cmd = New MySqlCommand("select Count1 from dateverification", conn)
Da = New MySqlDataAdapter(cmd)
Da.Fill(ds, "dateverification")
Label2.Text = ds.Tables(0).Rows(0).Item(0)
'Checking Count2
cmd = New MySqlCommand("select Count2 from dateverification", conn)
Da = New MySqlDataAdapter(cmd)
Da.Fill(ds, "dateverification")
Label3.Text = ds.Tables(0).Rows(0).Item(0)
'If the days are Different, Total days will be counted and Date will be updated
If Label1.Text <> TextBox1.Text Then
Label1.Text = System.DateTime.Now.ToString(("yyyy-MM-dd"))
Label2.Text = Label2.Text + 1
Dim query1 As String
query1 = "UPDATE attendance.dateverification SET Today_Date = '" & Label1.Text & "' , Count1 = '" & Label2.Text & "' WHERE Subject_Name = '" & TextBox3.Text & "'; "
command = New MySqlCommand(query1, conn)
reader = command.ExecuteReader
MessageBox.Show("Welcome to New Day")
Else
'If the date are equal, then the number of counts which register wasopen in same day will be increased
Label3.Text = Label3.Text + 1
Dim query1 As String
query1 = "UPDATE attendance.dateverification SET Count2 = '" & Label3.Text & "' WHERE Subject_Name = '" & TextBox3.Text & "'; "
command = New MySqlCommand(query1, conn)
reader = command.ExecuteReader
MessageBox.Show("You are still on the same day")
End If
Dim query As String
query = "UPDATE attendance.dateverification SET Subject_selected = '" & TextBox3.Text & "' WHERE Subject_Name = '" & TextBox3.Text & "'; "
command = New MySqlCommand(query, conn)
reader = command.ExecuteReader
conn.Close()
Catch ex As Exception
MessageBox.Show(ex.Message)
Finally
conn.Dispose()
End Try
Probably, the error is because this code is not always legal:
Label3.Text = Label3.Text + 1
Label3.Text
is a string. Depending on compiler options, you can't just expect the compiler to convert it to a number for you. Those options are there to help you, and you should use them.
Moving on.
Not 100% sure on MySql, but in Sql Server for sure you could do all this as one connection command job in the database. This will perform better, and helped me greatly simplify the code.
Pay special attention to how I used parameters. It's NEVER okay to use string concatenation to put parameter values into a query. This is too important even for learning or proof of concept code.
Option Strict On
Option Infer On
Imports System
Imports MySql.Data.MySqlClient
Public Class Form3
'Don't try to keep the connection/command objects at the form level!
'Do keep the connection string here:
Private ConnectionString As String = "server=localhost;user=root;password=1234;database=attendance"
Private Sub Form3_Load(sender As Object, e As EventArgs) Handles MyBase.Load
TextBox1.Text = Today.ToString("MM/dd/yyyy")
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim conn As SqlConnection
'I put two statements in one command string,
' and the CASE expressions let me get all of the updates into one statement
Dim sql As String =
"UPDATE attendance.dateverification
SET Count1 = CASE WHEN Today_Date <> current_date THEN Count1 + 1 ELSE Count1 END,
Count2 = CASE WHEN Today_Date = current_date THEN Count2 + 1 ELSE Count2 END,
Today_Date = current_date,
Subject_selected = @subject
WHERE Subject_Name = @subject;
SELECT Subject_Name, Today_Date, Count1, Count2
FROM dateverification"
' Also note: this string could be a constant if we wanted to!
Try
conn = New MySqlConnection(ConnectionString)
Dim cmd As New MySqlCommand(sql, conn)
'Use actual type and length here
cmd.Parameters.Add("@subject", MySqlDbType.VarString, 50).Value = TextBox3.Text
conn.Open()
Dim rdr As MySqlDataReader = cmd.ExecuteReader()
rdr.Read()
TextBox2.Text = rdr("Subject_Name").ToString()
Label1.Text = rdr("Today_Date").ToString()
Label2.Text = rdr("Count1").ToString()
Label3.Text = rdr("Count2").ToString()
Catch ex As Exception
MessageBox.Show(ex.Message)
Finally
conn.Dispose()
End Try
End Sub
End Class
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.