I have no idea how to use parameterized and would like someone to point me into the right direction.
Here's what I'm currently using.
Public Class main
Dim dbCon As New MySqlConnection("Server=localhost;Database=payid;Uid=root")
Dim strQuery As String = ""
Dim SQLCmd As MySqlCommand
Dim DR As MySqlDataReader
Private Sub Use()
Try
strQuery = "UPDATE payid " & _
"SET used='" & amen.Text & "' " & _
"WHERE payid='" & TextBox1.Text & "'"
SQLCmd = New MySqlCommand(strQuery, dbCon)
dbCon.Open()
SQLCmd.ExecuteNonQuery()
dbCon.Close()
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
If someone could change that for me I'd be able to do the rest of my code.
strQuery = "UPDATE payid SET used=#used WHERE payid=#payid "
SQLCmd = New MySqlCommand(strQuery, dbCon)
SQLCmd.Parameters.AddWithValue("#used", amen.Text)
SQLCmd.Parameters.AddWithValue("#payid", TextBox1.Text )
Related
Seriously need help. Been figuring this one out for days and I can't seem to get it. Tried all the codes I searched but still no luck.
So here's the problem, for every added existing violation in a student in tablestudentviolation, The numberofviolation will increase from 1st to 2nd to 3rd. How can I do that? I'll post my codes (current in progress codes) and screenshot of the mysql table below for reference
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Mysqlconn = New MySqlConnection
Mysqlconn.ConnectionString =
"server=localhost;uid=root;password=;database=cdm"
Try
Mysqlconn.Open()
Dim query As String
query = "select * from cdm.tablestudentviolations where NameOfViolation = '" & ComboBox1.Text & "' and NumberOfViolation = LAST_INSERT_ID()"
command = New MySqlCommand(query, Mysqlconn)
Dim reader As MySqlDataReader = command.ExecuteReader
If reader.HasRows Then
command = New MySqlCommand(query, Mysqlconn)
reader = command.ExecuteReader
Else
Dim query1 As String
Dim reader1 As MySqlDataReader
Dim command1 As MySqlCommand
query1 = "insert into cdm.tablestudentviolations (StudentNumber,NameOfViolation,Remarks,NumberOfViolation,EmployeeUsername,EmployeeID) values ('" & Studnum.Text & "','" & ComboBox1.Text & "','" & RichTextBox1.Text & "','" & Label2.Text & "', '" & Form5.Label4.Text & "','2')"
command1 = New MySqlCommand(query1, Mysqlconn)
reader1 = command1.ExecuteReader
End If
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub
What I want to achieve is:
I will choose what column should I update, this is why I am experimenting on my code but did not work and now I think I need some help.
Dim btnup="col1"
Dim btnval="1"
Try
Dim mysqlconn As New MySqlConnection
Dim conStr As String
conStr = "Server=localhost; user id=root; password=; database=dnc_floor"
mysqlconn = New MySqlConnection(conStr)
mysqlconn.Open()
Dim update As String
Dim cmd As MySqlCommand
update = "Update floor set ='" & btnup & "'=btnval where ID='" & Day & "'"
cmd = New MySqlCommand(update, mysqlconn)
cmd.ExecuteNonQuery()
Dim check As String = cmd.ExecuteNonQuery()
MsgBox("Saved")
mysqlconn.Close()
Catch ex As MySqlException
MessageBox.Show("Check your connection with your database")
Finally
mysqlconn.Dispose()
End Try
The update string is probably wrong. Maybe u think of that:
update = "Update floor set " & btnup & " ='" & btnval & "' where ID= '" & Day & "'"
I get an error when I try to connect to my database.
What I want to do is to check if I have on the same row from my DataBase a name and a surname
ex. Id_ 1 Michael Dawn
I have 2 textboxes and If they include:
Textbox1 - Michael
Textbox2 - Dawn
Then it's a positive match
I get an error :
Need some help with this one guys, thanks
Here is my code
Imports MySql.Data
Imports MySql.Data.MySqlClient
Public Class Form2
Dim dbCon As MySqlConnection
Dim strQuery As String = ""
Dim SQLCmd As MySqlCommand
Dim DR As MySqlDataReader
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
'just a message
MsgBox(" You are searching for the users: " & vbCrLf & "Name: " & TextBox1.Text & vbCrLf & "Surname: " & TextBox2.Text)
' connecting to the database
dbCon = New MySqlConnection("Server = localhost, Database = users, Uid = root, Pwd = password")
strQuery = "SELECT users.name, users.surname" & _
"WHERE users.name = '" + TextBox1.Text + "'AND password = '" + TextBox2.Text + "'"
SQLCmd = New MySqlCommand(strQuery, dbCon)
'Database open
Try
dbCon.Open()
DR = SQLCmd.ExecuteReader
If DR.HasRows = 0 Then
MsgBox("Not a match", MsgBoxStyle.Critical)
Else
MsgBox("You guessed the correct name: " & TextBox1.Text & "and the surname: " & TextBox2.Text)
End If
'Close
DR.Close()
dbCon.Close()
Catch ex As Exception
MsgBox("Failure to communicate " & vbCrLf & vbCrLf & ex.Message)
End Try
End Sub
End Class
Imports MySql.Data.MySqlClient
Imports MySql.Data
Public Class Form1
Dim dbcon As New MySqlConnection("Data Source=VINCENT-PC\SQLEXPRESS;Initial Catalog=Group1;Integrated Security=True")
Dim sqlcmd As String
Dim mysqlcmd As MySqlCommand
Dim dr As MySqlDataReader
Private Sub btnSearch_Click(sender As System.Object, e As System.EventArgs) Handles btnSearch.Click
Try
sqlcmd = "Select * from Sample1 where num=" + txtname.Text
mysqlcmd = New MySqlCommand(sqlcmd, dbcon)
dbcon.Open()
dr = mysqlcmd.ExecuteReader
While dr.Read
txtfname = dr.Item("fname")
txtlname = dr.Item("lname")
End While
dr.Close()
dbcon.Close()
Catch ex As Exception
MsgBox("Error: " & vbCrLf & vbCrLf & vbCrLf & ex.Message)
End Try
End Sub
End Class
this is the code that i wrote but it seemed like it doesn't connect to my database please help me and thank you in advance
Try the Following code :
Imports System.Data.SqlClient
Dim Conn As SqlConnection
Dim sqlcmd As String
Dim mysqlcmd As SqlCommand
Dim dr As SqlDataReader
Private Sub btnSearch_Click(sender As System.Object, e As System.EventArgs) Handles btnSearch.Click
dim m_CurrConstr as String
dim m_svrname as String
dim m_Currentdbf as String
m_Srvname="VINCENT-PC\SQLEXPRESS"
m_Currentdbf ="[YourDBName]"
Try
m_CurrConStr = "Server=" & m_Srvname & ";Initial Catalog=" & m_Currentdbf & ";User ID=sa;Password=as;Trusted_Connection=False;"
Conn = new Sqlconnection(m_currConstr)
sqlcmd = "Select * from Sample1 where num=" + txtname.Text
mysqlcmd = New SqlCommand(sqlcmd, Connn)
Conn.Open()
dr = mysqlcmd.ExecuteReader
While dr.Read
txtfname = dr.Item("fname")
txtlname = dr.Item("lname")
End While
dr.Close()
Conn.Close()
Catch ex As Exception
Messagebox.Show("Error: " & vbCrLf & vbCrLf & vbCrLf & ex.Message)
End Try
End Sub
Mark as Answer if I answered your query
When I run this function
For RepeatBooking = 1 To 51
dateConvertedDateToBook = dateDateToBook.Date
dateDateToBook = dateDateToBook.AddDays(7)
strDateToBook = dateConvertedDateToBook.ToString("yyyy-MM-dd")
Try
Dim command As MySqlCommand = New MySqlCommand
Dim sqlQuery As String = "INSERT INTO bookings SET Date=" & "'" & strDateToBook & "',RoomID='" & strComputerRoomToBook & "',Length='" & intNewBookingLength & "',Period='" & intNewStartPeriod & "',UserID='" & intid & "'"
Dim reader As MySqlDataReader
SQLConnection.Open()
command.CommandText = sqlQuery
command.Connection = SQLConnection
reader = command.ExecuteReader
SQLConnection.Close()
Catch excep As Exception
MsgBox(excep.ToString)
End Try
Next
in my program I get an error saying "The connection property has not been set or is null"
How can I get rid of this?
It goes to the exception when it gets to SQLconnection.Open()
I created the ServerString and MySQL connection at the top of the module like so:
Dim ServerString As String = "Server=localhost;User Id=root;Password=**********;Database=rooms"
Dim SQLConnection As MySqlConnection = New MySqlConnection
You are opening a connection without its property
It should be,
Dim SQLConnection As New MySqlConnection(ServerString)
SQLConnection.Open
Also, you may want to use the USING function so that your connection is properly closed.
It seems you are just inserting a bunch of values to your database and not retrieving anything so why do you use a DataReader?
Your code should be something like this:
Using SQLConnection = New MySqlConnection(ServerString)
SQLConnection.Open 'You should open a connection only once
For RepeatBooking = 1 To 51
dateConvertedDateToBook = dateDateToBook.Date
dateDateToBook = dateDateToBook.AddDays(7)
strDateToBook = dateConvertedDateToBook.ToString("yyyy-MM-dd")
Try
Dim sqlQuery As String = "INSERT INTO bookings SET " & _
"Date='" & strDateToBook & "'," & _
"RoomID='" & strComputerRoomToBook & "', " & _
"Length='" & intNewBookingLength & "', " & _
"Period='" & intNewStartPeriod & "', " & _
"UserID='" & intid & "'"
Dim command = New MySqlCommand(sqlQuery, SQLConnection)
command.ExecuteNonQuery
Catch excep As Exception
MsgBox(excep.Message)
End Try
Next
End Using
Also, you may want to change how to pass your values into a parameter. This will prevent SQL Injection.