SQL, VB.Net | What query can I use grab this value? - mysql

In the above I want it to give me the value of "ID" of the "Username" associated with it.
Here is the command I tried
Dim Query2 = String.Format("SELECT ID FROM account WHERE character = " & UsernameTextBox.Text & " ")
Command = New MySqlCommand(Query2, mydbcon)
Dim idtest = reader.Read.ToString
MsgBox(idtest)
however it is returning "false" what am I doing wrong?
Here is the entire code that I am working on:
Try
mydbcon.Open()
Dim Query As String
Query = String.Format("SELECT * FROM account WHERE username = '{0}' AND password = '{1}'", Me.UsernameTextBox.Text.Trim(), Me.PasswordTextBox.Text.Trim())
Dim Command = New MySqlCommand(Query, mydbcon)
reader = Command.ExecuteReader
Dim count As Integer
count = 0
While reader.Read
count = count + 1
End While
If count = 1 Then
MessageBox.Show("Username and password are correct")
Query = String.Format("SELECT username AS ID FROM account WHERE character = " & UsernameTextBox.Text & " ")
Command = New MySqlCommand(Query, mydbcon)
My.Settings.LoginID = reader.Read
MsgBox(My.Settings.LoginID)
ElseIf count > 1 Then
MessageBox.Show("Username and password are duplicate")
Else
MessageBox.Show("Username and password are wrong")
End If
mydbcon.Close()
Catch ex As MySqlException
MessageBox.Show(ex.Message)
Finally
mydbcon.Dispose()
End Try

Use ID as alias (so select username AS anyNameYouWantToAssignToIt):
Dim Query2 = String.Format("SELECT username AS ID FROM account WHERE character = " & UsernameTextBox.Text & " ")

Related

VB Log In button linked to mysq

Im new to visual basic and im currently working on a log in form linked to mysql...i have a code for the log in button but it only gives me an error.This is the code
Private Sub LogIn_btn_Click(sender As Object, e As EventArgs) Handles LogIn_btn.Click
dbconn = New MySqlConnection()
dbconn = New MySqlConnection("Data Source=localhost;user id=root;password=root;database=oeas") With {
.ConnectionString = "Data Source=localhost;user id=root;password=root;database=oeas"
}
sqlcmd = New MySqlCommand()
Try
dbconn.Open()
Const V As String = "select * From oeas.users where User_Name = '" & Pass_txtbx.Text&"' and Password ='" & UserN_txtbx.Text& "' "
query = V
sqlcmd = New MySqlCommand(query, dbconn)
dbrd = sqlcmd.ExecuteReader
Dim count As Integer
count = 0
While dbrd.Read
count = count + 1
End While
If count = 1 Then
MessageBox.Show("username and password are correct")
Profile_form.Show()
Me.Hide()
ElseIf count > 1 Then
MessageBox.Show("username and password are duplicate")
Else
MessageBox.Show("username and password are not correct")
End If
Catch ex As MySqlException
MsgBox("Connection Error: " & ex.Message.ToString)
End Try
End Sub
what might be wrong?

Mysql Replace function issue

I want to update my table with replace function, but the result is not my expected result, how can I solve this problem?
Lets say I have a LONGTEXT columns(using_id) with value like this 1,2,3,4,5,6,7,8,9,10,11,
Now I want to remove '1,' so I write in this way:-
Dim query As String = "UPDATE curr_id SET using_id = REPLACE(using_id,'" & $"{g_currTicketID}," & "','')"
But the result become 2,3,4,5,6,7,8,9,10,1 How can I keep my '11,'?
Expected result: 2,3,4,5,6,7,8,9,10,11,
Public Sub TicketStatusUpdate(p_str As String)
'Login => GET NEXT ID > UPDATE USING ID > UPDATE NEXT ID
'Sales => RELEASE > UPDATE USED ID > GET NEXT ID > UPDATE USING ID > UPDATE NEXT ID
Select Case p_str
Case "UPDATE USED ID"
TicketStatusUpdate("RELEASE")
Dim query As String = "UPDATE curr_id SET used_id = CONCAT(used_id, '" & $"{g_currTicketID}," & "')"
Using cmd = New MySqlCommand(query, conn)
cmd.ExecuteNonQuery()
End Using
TicketStatusUpdate("GET NEXT ID")
Case "GET NEXT ID"
Dim query_select = "SELECT * FROM curr_id WHERE DATE(curr_date) = '" & Format(DateTime.Today.Date, "yyyy-MM-dd") & "' "
Dim reader As MySqlDataReader
Dim IsNewDay As Boolean = False
Using cmd = New MySqlCommand(query_select, conn)
reader = cmd.ExecuteReader
reader.Read()
If reader.HasRows Then
g_currTicketID = reader.GetInt32("next_id")
Else
g_currTicketID = 1
IsNewDay = True
End If
reader.Close()
End Using
If IsNewDay Then TicketStatusUpdate("RESET")
TicketStatusUpdate("UPDATE USING ID")
TicketStatusUpdate("UPDATE NEXT ID")
Case "UPDATE USING ID"
Dim query As String = "UPDATE curr_id SET using_id = CONCAT(using_id, '" & $"{g_currTicketID}," & "')"
Using cmd = New MySqlCommand(query, conn)
cmd.ExecuteNonQuery()
End Using
Case "UPDATE NEXT ID"
Dim query1 As String = "SELECT * FROM curr_id"
Dim str_usingID As String = ""
Dim str_usedID As String = ""
Dim reader As MySqlDataReader
Using cmd = New MySqlCommand(query1, conn)
reader = cmd.ExecuteReader
reader.Read()
str_usingID = reader.GetString("using_id").ToString
str_usedID = reader.GetString("used_id").ToString
reader.Close()
End Using
Dim str_allID As String = (str_usingID + str_usedID).TrimEnd(",")
Dim strArray As String() = str_allID.Split(",")
Dim intArray As Integer() = Array.ConvertAll(strArray, Function(s) Int32.Parse(s))
Array.Sort(Of Integer)(intArray)
Dim nextID As Integer = FirstMissing(intArray)
Dim query2 As String = "UPDATE curr_id SET next_id = '" & nextID & "'"
Using cmd = New MySqlCommand(query2, conn)
cmd.ExecuteNonQuery()
End Using
Case "RELEASE"
Dim query As String = "UPDATE curr_id SET using_id = REPLACE(using_id,'" & $"{g_currTicketID}," & "','')"
Using cmd = New MySqlCommand(query, conn)
cmd.ExecuteNonQuery()
End Using
Case "RESET"
Dim query As String = "UPDATE curr_id SET next_id='',used_id='',using_id=''"
Using cmd = New MySqlCommand(query, conn)
cmd.ExecuteNonQuery()
End Using
End Select
End Sub
Private Function FirstMissing(sequence() As Integer) As Integer
Dim seq = sequence
Dim firstMissingNumer = Int32.MinValue
For i = 1 To Math.Min(seq.Last, seq.Count)
If seq(i - 1) <> i Then
firstMissingNumer = i
Exit For
End If
Next
If firstMissingNumer = Int32.MinValue Then
Return seq.Max + 1
Else
Return firstMissingNumer
End If
End Function
When I logout the application it will call TicketStatusUpdate("RELEASE") to remove the g_currTicketID from using_id column.
Examples:
g_currTicketID = 1
using_id = 1,11,21,31,
When TicketStatusUpdate("RELEASE") called it will remove all '1,' from using_id, so the result will become 1,2,3 which is not the result that I want, I only want to remove '1,' and keep '11,21,31,'
Instead of replace just value, you could try to replace both value and boundary character
(in your case, comma ',' is a boundary character).
Example:
With g_currTicketID = 1, using_id = 1,11,21,31, then
replaceValue = CONCAT(',',g_currTicketID,',') = ',1,'
stringToReplace = CONCAT(',',using_id,',') = ',1,11,21,31,'
using_id = TRIM(BOTH ',' FROM REPLACE(stringToReplace, replaceValue, ','))
Your updat statement could be like this
Dim query As String = "UPDATE curr_id SET using_id = TRIM(BOTH ',' FROM REPLACE(CONCAT(',' ,using_id, ','), '," & $"{g_currTicketID}," & "', ','))"

Vb.net SELECT command denied to user 'myuser'#'myip' for table 'mytable'

Hello how to connect to mysql database ?
Mysql database on my website but not connecting. I gived all permission
Connection succesful but not selecting.
ERROR: SELECT command denied to user 'myuser'#'myip' for table 'mytable'
Dim DatabaseName As String = "mydatabase"
Dim server As String = "myip"
Dim userName As String = "myusername"
Dim password As String = "mypass"
Dim okuyucu As MySqlDataReader
Dim baglanti As New MySqlConnection
If Not baglanti Is Nothing Then baglanti.Close()
baglanti.ConnectionString = String.Format("server={0}; user id={1}; password={2}; database={3}; pooling=false", server, userName, password, DatabaseName)
Try
baglanti.Open()
Dim query As String
query = "select * from database.chat where name='" & name.Text & "' and password='" & passwordbox.Text & "' "
komut = New MySqlCommand(query, baglanti)
okuyucu = komut.ExecuteReader
Dim count As Integer
count = 0
While okuyucu.Read
count = count + 1
End While
If count = 1 Then
MessageBox.Show("Giriş bilgileri doğru !")
ElseIf count > 1 Then
MessageBox.Show("Giriş bilgileri fazladan var !")
Else
MessageBox.Show("Giriş bilgileri doğru değil !")
End If
baglanti.Close()
Catch ex As MySqlException
MessageBox.Show(ex.Message)
Finally
baglanti.Dispose()
End Try
My connect code this is.

Vb.net Login Session and Mysql Query

I have a login page that i want to collect a users id and use a session to store it so i can use it on the redirect page.
Login Page :
Dim Query As String
Query = "select * from mdxmain.taffiliate where affID = '" & username.Text & "' and affPassword = '" & password.Text & "'"
COMMAND = New MySqlCommand(Query, MysqlConn)
Session("affID") = username.Text
HttpContext.Current.Session.Add("affID", userid)
READER = COMMAND.ExecuteReader
Dim count As Integer
count = 0
While READER.Read
count = count + 1
End While
If count = 1 Then
Response.Redirect("dashboard.aspx")
Else
Literal1.Text = "Invalid credentials"
End If
MysqlConn.Close()
Finally
End Try
MysqlConn.Dispose()
and the dashboard.aspx page that has a query in it :
Dim c As New MySqlConnection("Server=localhost;Database=test;UID=test;PWD=test;")
c.Open()
Dim com As New MySqlCommand("SELECT COUNT(*) as c FROM toutcome WHERE AffID = 'MW0011' AND CompletedDate >= CURDATE();", c)
Dim myReader As MySqlDataReader = com.ExecuteReader(CommandBehavior.CloseConnection)
myReader.Read()
Label1.Text = myReader.Item(0).ToString()
Now what i want to do is the customer logs in using his ID and i want to copy that ID into a session and then use it in the above query (replace MW0011 with the session id) , please help me.
Try to replace the query WHERE AffID = 'MW0011' with WHERE AffID = '" & CType(Session.Item("affID"), String) & "'

Connecting to database and reading data in asp.net

What am I doing wrong here? I get an error:
incorrect syntax near =
on this line of code:
Dim SqlDataReader As SqlClient.SqlDataReader = SQLCommad.ExecuteReader()
My code:
Dim Username = TUserName.Text
Dim Password = TPassword.Text
Dim SqlConnection = New SqlClient.SqlConnection(My.Settings.DBConnection.ToString)
Dim SQLCommand = New SqlClient.SqlCommand("select * from " + My.Settings.TableName.ToString + "Where EmpID = " + Username, SqlConnection)
SqlConnection.Open()
Dim SqlDataReader As SqlClient.SqlDataReader = SQLCommand.ExecuteReader
While (SqlDataReader.Read)
If Username = SqlDataReader(1).ToString And Password = SqlDataReader(20).ToString Then
If eEncrypt(Username, Password) Then
MsgBox("You are NOT logged in")
Else
MsgBox("You are logged in")
End If
End If
End While
SqlConnection.Close()
This should work, see how a parammeter is added
Dim Username = TUserName.Text
Dim Password = TPassword.Text
Dim SqlConnection = New SqlClient.SqlConnection(My.Settings.DBConnection.ToString)
Dim SQLCommand = New SqlClient.SqlCommand("select * from " + My.Settings.TableName.ToString + "Where EmpID = #Username", SqlConnection)
SQLCommand.Parameters.AddWithValue("#Username", Username)
SqlConnection.Open()
Dim SqlDataReader As SqlClient.SqlDataReader = SQLCommand.ExecuteReader()
While (SqlDataReader.Read)
If Username = SqlDataReader(1).ToString And Password = SqlDataReader(20).ToString Then
If eEncrypt(Username, Password) Then
MsgBox("You are NOT logged in")
Else
MsgBox("You are logged in")
End If
End If
End While
SqlConnection.Close()
you miss parenthesis
Dim SqlDataReader As SqlClient.SqlDataReader = SQLCommad.ExecuteReader()
This line of code
Dim SQLCommand = New SqlClient.SqlCommand("select * from " + My.Settings.TableName.ToString + "Where EmpID = " + Username, SqlConnection)
should be
Dim SQLCommand = New SqlClient.SqlCommand("select * from " + My.Settings.TableName.ToString + "Where EmpID = '" + Username + "'", SqlConnection)
posted from mobile.