The following is my code for forgot password,
I want to store the value of mysql query in a variable so that i can use the variable value for placing it in a email.
Private Sub Button3_Click(sender As Object, e As EventArgs)
Handles Button3.Click
Dim p_text As String
p_text = TextBox1.Text
MsgBox(p_text)
Dim i As Integer
Try
Dim conn As MySqlConnection
conn = New MySqlConnection
conn.ConnectionString ="server=192.168.1.111;userid=betterhomes;
password=123456;database=betterhomes"
conn.Open()
Dim password As String = ""
Dim sqlquery2 As String = "SELECT bh_password FROM bh_login
where job_id = '" & p_text & "';"
Dim data2 As MySqlDataReader
Dim adapter2 As New MySqlDataAdapter
Dim command2 As New MySqlCommand
command2.CommandText = sqlquery2
command2.Connection = conn
adapter2.SelectCommand = command2
data2 = command2.ExecuteReader
While data2.HasRows
password = data2.GetString("bh_password")
MsgBox(password)
data2.Close()
End While
Catch exError As MySqlException
' MsgBox("Error connecting to database! Try again later")
End Try
End Sub
End Class
Try Like this
If data2.Read
password = data2.GetString("bh_password")
MsgBox(password)
End if
data2.Close()
Send Email
Dim mail As New MailMessage()
'set the addresses
mail.From = New MailAddress("xx#xx")
mail.[To].Add("xx#xx")
' //set the content
mail.Subject = "This is an email"
mail.Body = password
' // set the server
Dim smtp As New SmtpClient("localhost")
' // send the message
Try
smtp.Send(mail)
Response.Write("Your Email has been sent sucessfully - Thank You")
Catch exc As Exception
Response.Write("Send failure: " & exc.ToString())
End Try
Related
I have a textbox, button, and datagridview in my form. When i click the button, system will grab a table depending on my textbox from the database and show on datagridview.
I getting this error when i click the button. Where am I wrong?
here is my dbconn
Module mod_dbconn
Public conn As MySqlConnection
Public Sub openDB()
Dim dbname As String = scr_sales.btn_dbswitch.Text
Dim server As String = "localhost"
Dim user As String = "root"
Dim password As String = ""
Try
conn = New MySqlConnection
conn.ConnectionString = String.Format("server={0}; user id={1}; password={2}; database={3}; pooling=false", server, user, password, dbname)
If conn.State = ConnectionState.Closed Then
conn.Open()
End If
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
This is my form
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Dim query As String = "SELECT * FROM '" + TextBox1.Text + "'"
Dim cmd As New MySqlCommand(query, conn)
Dim da As New MySqlDataAdapter(cmd)
Dim dt = New DataTable
Dim cb As MySqlCommandBuilder
cb = New MySqlCommandBuilder(da)
DataGridView1.Refresh()
Try
conn.Open()
da.Fill(dt)
Dim bsource As New BindingSource
bsource.DataSource = dt
Me.DataGridView1.DataSource = bsource
da.Update(dt)
conn.Close()
Catch ex As Exception
MessageBox.Show(ex.Message)
Finally
conn.Dispose()
End Try
End Sub
You are trying to build a dynamic table select so for the table name you don't need the quotes around the tablename
"SELECT * FROM " + TextBox1.Text + " ;"
I have a registration form and I want to encrypt the password using whatever encryption is available, I'm using vb.net 2008 and MySQL as database, I searched through online and found some encrypting code but I have no idea how to connect it to my registration form. here is my registration code and the encryption code i found online (at the top part)
Imports MySql.Data.MySqlClient
Imports System.Security
Imports System.Security.Cryptography
Public Class user
Public Function AES_Encrypt(ByVal input As String, ByVal pass As String) As String
Dim AES As New System.Security.Cryptography.RijndaelManaged
Dim Hash_AES As New System.Security.Cryptography.MD5CryptoServiceProvider
Dim encrypted As String = ""
Try
Dim hash(31) As Byte
Dim temp As Byte() = Hash_AES.ComputeHash(System.Text.ASCIIEncoding.ASCII.GetBytes(pass))
Array.Copy(temp, 0, hash, 0, 16)
Array.Copy(temp, 0, hash, 15, 16)
AES.Key = hash
AES.Mode = Security.Cryptography.CipherMode.ECB
Dim DESEncrypter As System.Security.Cryptography.ICryptoTransform = AES.CreateEncryptor
Dim Buffer As Byte() = System.Text.ASCIIEncoding.ASCII.GetBytes(input)
encrypted = Convert.ToBase64String(DESEncrypter.TransformFinalBlock(Buffer, 0, Buffer.Length))
Return encrypted
Catch ex As Exception
End Try
End Function
Private Sub BCreateAcount_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BCreateAcount.Click
Dim conn As MySqlConnection
conn = New MySqlConnection
conn.ConnectionString = "server = localhost;username= root;password= a;database= database"
Try
conn.Open()
Catch mali As MySqlException
MsgBox("connot establish connection")
End Try
Dim myCommand As New MySqlCommand
Dim myReader As MySqlDataReader
myCommand.Connection = conn
myCommand.CommandText = "insert into user values('" + txtUserName.Text + "','" + txtNewPassword.Text + "')"
Call calldaw()
If txtUserName.Text = "" Or txtNewPassword.Text = "" Or txtConfirmPassword.Text = "" Then
MsgBox("Please enter username and password", MsgBoxStyle.Information, "Inventory System")
ElseIf txtConfirmPassword.Text = txtNewPassword.Text Then
MsgBox("Account Created", MsgBoxStyle.Information, "Inventory System")
myReader = myCommand.ExecuteReader()
txtUserName.Text = ""
txtNewPassword.Text = ""
txtConfirmPassword.Text = ""
Else
MsgBox("Password did not match", MsgBoxStyle.Critical, "Inventory System")
txtConfirmPassword.Text = ""
txtNewPassword.Text = ""
txtUserName.Text = ""
End If
End Sub
Private Sub calldaw()
Dim conn As MySqlConnection
conn = New MySqlConnection
conn.ConnectionString = "server = localhost;username= root;password= a;database= database"
Try
conn.Open()
Catch mali As MySqlException
MsgBox("connot establish connection")
End Try
Dim myData As MySqlDataAdapter
Dim reason As String = " Create Account "
Dim tao As String = "admin"
myData = New MySqlDataAdapter
Dim sqlsql = "insert into daily_log values('" + tao + "','" + Date1.Text + "','" + reason + "','" + Time1.Text + "')"
Dim ssql = "Select * from user"
Dim myCommand As New MySqlCommand
myCommand.Connection = conn
myCommand.CommandText = sqlsql
Dim myReader As MySqlDataReader
myReader = myCommand.ExecuteReader
End Sub
Private Sub BBack_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BBack.Click
Me.Close()
End Sub
Private Sub user_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Timer1.Enabled = True
End Sub
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
Date1.Text = Date.Today.Date
Dim Date2 As Date = Date1.Text
Date1.Text = Format(Date2, "yyyy-MM-dd")
Time1.Text = TimeOfDay
End Sub
End Class
any help will do, thanks.
You have to call the AES_Encrypt function before executing the INSERT statement in order to pass the encrypted password to database.
Dim myCommand As New MySqlCommand
Dim myReader As MySqlDataReader
myCommand.Connection = conn
myCommand.CommandText = "insert into user values('" + txtUserName.Text + "','" + AES_Encrypt(txtNewPassword.Text,txtNewPassword.Text) + "')"
Call calldaw()
I'm working on simple project to figure out how databases work.
I created mysql base on my host and used this code to connect to it
Private mysql_host = "myhost"
Private mysql_user = "myuser"
Private mysql_pass = "mypw"
Private mysql_db = "mydb"
Private SQLConnect As String = "Server=" + mysql_host + ";" + "User Id=" + mysql_user + ";" + "Password=" + mysql_pass + ";" + "Database=" + mysql_db
Private SQLConnection As New MySqlConnection
Private Sub Form1_Load(sender As Object, e As System.EventArgs) Handles Me.Load
sqlConnection.ConnectionString = SQLConnect
Try
If sqlConnection.State = ConnectionState.Closed Then
sqlConnection.Open()
MsgBox("Connected")
Else
sqlConnection.Close()
MsgBox("Not Connected")
End If
Catch ex As Exception
MsgBox(ex.ToString)
End Try
End Sub
After that i used this code to add record into database.
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim SQLStatement As String = "INSERT into tCodes(Code) VALUES ('" & TextBox1.Text & "')"
Dim cmd As New MySqlCommand
With cmd
.CommandText = SQLStatement
.CommandType = CommandType.Text
.Connection = SQLConnection
.ExecuteNonQuery()
End With
MsgBox("Added")
End Sub
On the other form i want to check if record exist in database. How to do that .
I tried with code
Dim SQLStatement As String = "SELECT * From tCodes WHERE Code '" & TextBox1.Text & "'")
For read . Me using this
Private host As String = "host" 'Host DB
Private user As String = "user" 'User DB
Private pass As String = "pass" 'Pass DB
Private base As String = "base" 'Base
Private conn As String = "Database=" & base & ";Data Source=" & host & ";User Id=" & _
user & ";Password=" & pass 'Connection
Private Connection As New MySqlConnection(conn) 'Connection
Private readData As MySqlDataReader 'Data Reader
Private adaptsData As New MySqlDataAdapter 'Data Adapter
Private command As New MySqlCommand 'command
Private ds As New DataSet 'DataSet
After
Public Function __select(Optinal table as String = "tCodes") As String
Try
Connection.Open()
Dim query As String = "SELECT * FROM " & table
command.CommandText = query
command.Connection = conexiune
adaptsData.SelectCommand = comanda
adaptsData.Fill(ds, tabla)
Dim newvalue As String = ds.Tables(tabla).Rows(0).Item(item)
ds.Dispose()
ds.Clear()
Connection.Close()
Return newvalue
Catch ex As Exception
ds.Dispose()
ds.Clear()
conexiune.Close()
msgbox(ex.message)
End Try
End Function
Dim conn As MySqlConnection
Dim sqlquery = "SELECT * FROM tCodes WHERE Code = '" + txtCode.Text + "'"
Dim myCommand As New MySqlCommand()
myCommand.Connection = conn
myCommand.CommandText = sqlquery
'start query
myAdapter.SelectCommand = myCommand
Dim myData As MySqlDataReader
myData = myCommand.ExecuteReader()
'see if user exists
If myData.HasRows = 0 Then
MsgBox("Kod je nevazeci")
conn.Close()
Else
MsgBox("Kod je vazeci")
End If
That was the solution of this problem
I'm trying to add titles (picture) from all rows to a listbox and I'm getting this error:
An unhandled exception of type 'System.IndexOutOfRangeException' occurred in System.Data.dll. Not sure what to do at this point I've tried adding functions, making it a variable called item..
Public Function updatenews()
Dim MySqlConnection As New MySqlConnection()
Dim newsmydatatable As New DataTable
Dim rowcount As Integer = 0
Dim amount As Integer
MySqlConnection.ConnectionString = "server=" + host + "; user id=" + user + "; password=" + password + "; database=website;"
Try
MySqlConnection.Open()
Catch myerror As MySqlException
MessageBox.Show("Cannot connect news server: " & myerror.Message & "Please check your internet connection settings and try again. If problem persists contact support.")
Label3.Text = "Error!"
End Try
Dim myadapter As New MySqlDataAdapter
Dim newsmydatatable As New DataTable
Dim sqlquary = "SELECT * FROM news;"
Dim command As New MySqlCommand
command.Connection = MySqlConnection
command.CommandText = sqlquary
myadapter.SelectCommand = command
myadapter.Fill(newsmydatatable)
Dim mydata As MySqlDataReader
mydata = command.ExecuteReader()
If mydata.HasRows = 0 Then
Else
amount = newsmydatatable.Rows.Count
MsgBox(amount)
For value As Integer = 0 To amount
For value As Integer = 0 To amount
ListBox1.Items.Add(newsmydatatable.Rows(rowcount).Item("title"))
rowcount += 1
Next
End If
End Function
where you have:
ListBox1.Items.Add(newsmydatatable.Rows(rowcount).Item("title"))
rowcount += 1
change it to:
ListBox1.Items.Add(newsmydatatable.Rows(value).Item("title"))
I have a DataBound DGV it has 3 Columns namely ID(not pk), Name, and Status. I have 2 Buttons Add and Post. Add, adds Data to the DGV and DataBase(MySql) but in Status, it adds "No". What I want to do is for example I have 3 Rows in my DGV and highlight those 3 and click Post, the value in Status will be changed to "Yes". Here is what I have done so far, but I'm having trouble with the syntax for the UPDATE QUERY. THIS CODE WORKS NOW
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'TODO: This line of code loads data into the TestDataSet.testing table. You can move, or remove it, as needed.
Me.TestingTableAdapter.Fill(Me.TestDataSet.testing)
Using _conn As New MySqlConnection("Server = localhost; Username= root; Password =; Database = test")
Using cmd
With cmd
MsgBox("Connection Established")
.Connection = _conn
.Parameters.Clear()
.CommandText = "Select Max(TransactionID) from testing"
_conn.Open()
Dim dr As MySqlDataReader
dr = cmd.ExecuteReader()
If dr.Read() Then
If IsDBNull(dr.Item(0)) Then
txtNumber.Text = "1"
Else
txtName.Text = dr(0).ToString() + 1
End If
End If
End With
End Using
End Using
FillGrid()
End Sub
Private Sub FillGrid()
Using _conn As New MySqlConnection("Server = localhost; Username= root; Password =; Database = test")
Using _comm As New MySqlCommand
With _comm
.CommandText = " SELECT `ID`, `TransactionID`, `Name`, `Posted` FROM `testing`"
.Connection = _conn
End With
Using _adapter As New MySqlDataAdapter(_comm)
Try
_conn.Open()
Dim _ds As New DataSet
_adapter.Fill(_ds)
GVTransaction.DataSource = _ds.Tables(0)
Catch ex As Exception
End Try
End Using
End Using
End Using
End Sub
Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click
Using conn As New MySqlConnection("Server = localhost; Username= root; Password =; Database = test")
Using cmd
With cmd
MsgBox("Connection Established")
.Connection = conn
.Parameters.Clear()
.CommandText = "INSERT INTO testing(TransactionID, Name, Posted) VALUES (#ID, #iName, #iPosted)"
.Parameters.Add(New MySqlParameter("#ID", txtNumber.Text))
.Parameters.Add(New MySqlParameter("#iName", txtName.Text))
.Parameters.Add(New MySqlParameter("#iPosted", "No"))
End With
Try
conn.Open()
cmd.ExecuteNonQuery()
Catch ex As MySqlException
MsgBox(ex.Message.ToString())
End Try
End Using
End Using
MsgBox("Data Added to the Database")
Me.TestingTableAdapter.Dispose()
Me.TestingTableAdapter.Fill(Me.TestDataSet.testing)
End Sub
Private Sub btnPost_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPost.Click
Dim _ID As New List(Of String)
Dim _Name As New List(Of String)
For Each _x As DataGridViewRow In TestingDataGridView.SelectedRows
_ID.Add(_x.Cells("GVTransactionID").Value.ToString())
_Name.Add("'" & _x.Cells("GVName").Value.ToString() & "'")
Next
Dim inClause As String = String.Join(",", _ID.ToArray())
Dim inClause1 As String = String.Join(",", _Name.ToArray())
Dim _sqlUpdate As String = String.Format("UPDATE testing SET Posted = #Posted WHERE TransactionID IN ({0}) AND Name IN ({1})", inClause, inClause1)
Using _conn As New MySqlConnection("Server = localhost; Username= root; Password =; Database = test")
Using _commm As New MySqlCommand()
With _commm
.CommandText = _sqlUpdate
.Connection = _conn
.CommandType = CommandType.Text
.Parameters.AddWithValue("#Posted", "YES")
End With
Try
_conn.Open()
_commm.ExecuteNonQuery()
FillGrid()
Catch ex As MySqlException
MsgBox(ex.StackTrace.ToString)
End Try
End Using
End Using
End Sub
End Class
Any Helps or tips is greatly appreciated. Please and Thank you
Try this one,
Dim _ID As New List(Of String)
Dim _Name As New List(Of String)
For Each _x As DataGridViewRow In TestingDataGridView.SelectedRows
_ID.Add(_x.Cells("TransactionID").Value.ToString())
_Name.Add("'" & _x.Cells("Name").Value.ToString() & "'")
Next
Dim inClause As String = String.Join(",", _ID.ToArray())
Dim inClause1 As String = String.Join(",", _Name.ToArray())
Dim _sqlUpdate As String = String.Format("UPDATE testing SET Posted = #Posted WHERE TransactionID IN ({0}) AND Name IN ({1})", inClause, inClause1)
Using _conn As New MySqlConnection("Server = localhost; Username= root; Password =; Database = test")
Using _comm As New MySqlCommand()
With _comm
.CommandText = _sqlUpdate
.Connection = _conn
End With
Try
_conn.Open()
_comm.ExecuteNonQuery()
Catch ex As MySqlException
MsgBox(ex.Message.ToString())
End Try
End Using
End Using
I believe you can simply use this
For Each Row As DataGridViewRow In TestingDataGridView.SelectedRows
.CommandText = "UPDATE testing SET Posted = #iChange WHERE ID = " & Row.Cells(0) & ", Name = " & Row.Cells(1)
.CommandType = CommandType.Text
.Parameters.AddWithValue("#iChange", "Yes")
End For
Hope this helps :)