vb.net(Additional information: Unknown column 'Secret_question' in 'where clause') - mysql

Imports MySql.Data.MySqlClient
Public Class Forgot_password
Private Sub GroupBox1_Enter(sender As Object, e As EventArgs) Handles GroupBox1.Enter
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim con As New MySqlConnection("host=localhost;username=root; password=godzilla408421;database=program")
Dim cmd As New MySqlCommand
Dim dr As MySqlDataReader
con.Open()
cmd.Connection = con
cmd.CommandText = "SELECT * FROM program.login where Username ='" & useridtxt.Text & "' and Secret_question='" & questiontxt.Text & "' and answer='" & answertxt.Text & "'"
dr = cmd.ExecuteReader <<<<(HERE IS MY PROBLEM,IT TELLS ME THAT,"UNKNOW COLUMN'SECRET_QUESTION IN WHERE CLAUSE',PLEASE PLEASE PLEASE HELP"0>>>>
If Not dr Is Nothing Then
dr.Read()
passwordtxt.Text = dr(1)
dr.Close()
Else
MsgBox("Usename or password doesnt match")
End If
End Sub
End Class

First check your table whether you have Secret_question field or not, maybe you put it plural like Secret_questions. Then it's better to modify your code a bit like the following
cmd.CommandText = "SELECT * FROM program.login where Username =?Username and Secret_question=?Secret_question and answer=?answer;"
cmd.Parameters.AddWithValue("?Username", useridtxt.Text.Trim())
cmd.Parameters.AddWithValue("?Secret_question", questiontxt.Text.Trim())
cmd.Parameters.AddWithValue("?answer", answertxt.Text.Trim())
dr = cmd.ExecuteReader

Related

Updating MySQL database in xampp using Visual Studio or Visual Basic

I tried everything that I can do - I'm a beginner at programming. I wanted to update my database in xampp using VB or Visual Studio but can't seem to do it.
This is my code for that form.:
Imports MySql.Data.MySqlClient
Public Class Form4
Public MysqlConn As MySqlConnection
Public cmd As New MySqlCommand
Public da As New MySqlDataAdapter
Public Sub MysqlConnection()
MysqlConn = New MySqlConnection()
'Connection String
MysqlConn.ConnectionString = "server=localhost;" _
& "user id=root;" _
& "password=;" _
& "database=bank"
'OPENING THE MysqlConnNECTION
MysqlConn.Open()
End Sub
Public Sub add()
Dim sql As String
Dim TempTable As New DataTable
sql = "update cbank set Balance = Balance + " & TextBox2.Text & "where AccountID = " & TextBox1.Text & "and PIN = " & TextBox3.Text & ";"
'bind the connection and query
With cmd
.Connection = MysqlConn
.CommandText = sql
End With
da.SelectCommand = cmd
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
MysqlConnection()
add()
End Sub
End Class
There is no error it just that there is no output also
Keep you data objects local so you can be sure they are closed and disposed. A Using...End Using block ensures this even if there is an error. You can make your connection string a form level variable so you can use it anywhere but that is the only form level variable you need.
You can pass your connection string directly to the constructor of the connection.
You can pass your sql command text and the connection directly to the constructor of the command.
Please always use Parameters. Not only wil it save you from misplacing quotes but it will help ensure that correct datatypes are sent to the database. The most important thing is it helps protect you database from sql injection which can destroy your database. I had to guess at the datatypes in your database. Check the database and adjust the code accordingly.
Private ConnString As String = "server=localhost;user id=root;password=;database=bank"
Private Sub add()
Using cn As New MySqlConnection(ConnString)
Using cmd As New MySqlCommand("update cbank set Balance = Balance + #Balance where AccountID = #ID and PIN = #PIN;", cn)
cmd.Parameters.Add("#Balance", MySqlDbType.Decimal).Value = CDec(TextBox2.Text)
cmd.Parameters.Add("#ID", MySqlDbType.Int32).Value = CInt(TextBox1.Text)
cmd.Parameters.Add("#PIN", MySqlDbType.Int32).Value = CInt(TextBox3.Text)
cn.Open()
cmd.ExecuteNonQuery()
End Using
End Using
End Sub
Quotes problem.
Imports MySql.Data.MySqlClient
Public Class Form4
Public MysqlConn As MySqlConnection
Public cmd As New MySqlCommand
Public da As New MySqlDataAdapter
Public Sub MysqlConnection()
MysqlConn = New MySqlConnection()
'Connection String
MysqlConn.ConnectionString = "server=localhost;" _
& "user id=root;" _
& "password=;" _
& "database=bank"
'OPENING THE MysqlConnNECTION
MysqlConn.Open()
End Sub
Public Sub add()
Dim sql As String
Dim TempTable As New DataTable
sql = "update cbank set Balance = Balance + " & TextBox2.Text & "where AccountID = " & TextBox1.Text & "and PIN = " & TextBox3.Text & ";"
bind the connection and query
With cmd
.Connection = MysqlConn
.CommandText = sql
End With
da.SelectCommand = cmd
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Me.Hide()
Form2.Show()
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
MysqlConnection()
add()
End Sub
Private Sub Form4_Load(sender As Object, e As EventArgs) Handles MyBase.Load
End Sub
End Class

Connecting phpmyadmin

Can anyone help me solve this SQL error?
And how do I connect it to my database in phpmyadmin using xampp?
Feel free to correct my code if you think something is wrong with it.
Imports System.Data.SqlClient
Imports MySql.Data.MySqlClient
Public Class Form1
Dim con As SqlConnection
Dim com As SqlCommand
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
con = New SqlConnection
con = New SqlCommand
con.ConnectionString = "initial catalog=student;user id=sa;pwd="
con.Open()
con.Connection = con
End Sub
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
If TextBox1.Text = " " Or TextBox2.Text = " " Or TextBox3.Text = " " Then
MsgBox("Please fill all the details")
Exit Sub
End If
com.CommandText = "insert into stud values('" & TextBox1.Text & "','" & TextBox2.Text & "','" & TextBox3.Text & "')"
com.ExecuteNonQuery()
MsgBox("1 record is stored successfully")
TextBox1.Clear()
TextBox2.Clear()
TextBox3.Clear()
End Sub
End Class
You should use MySqlConnection, not SqlConnection. SqlConnection is used for SQL Server. It can be downloaded from Nuget packages.

Object reference isn't set to an instance of an object

The error is in this image:
How do I fix this?
My code:
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
Dim con As New MySqlConnection("host=localhost; username=root; database= login")
Dim cmd As MySqlCommand
Dim dr As MySqlDataReader
con.Open()
cmd.Connection = con 'this is where I have an error
cmd.CommandText = "select userID, password from login where userID= ' " & userIDtxt.Text & " ' and password = ' " & pwTxt.Text & " '"
dr = cmd.ExecuteReader
If dr.HasRows Then
MsgBox("Login successful!")
Me.Hide()
MainForm.Show()
Else
MsgBox("Username or password does not match")
userIDtxt.Text = ""
pwTxt.Text = " "
End If
End Sub
End Class
your sql connection string missing true credentials, this sample to connect to mysql with vb and c#:
https://dev.mysql.com/doc/connector-net/en/connector-net-programming-connecting-open.html
You haven't initialized a new instance of your cmd variable.
Change:
Dim cmd As MySqlCommand
to:
Dim cmd As New MySqlCommand

how can i fix my database Connection in vb.net?

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

button not working when i insert new data

when I input data are not yet available. button does not work
but when I enter existing data in the database, the button work for find existing records in the database and msgbox.appear
this my coding. (i am using Microsoft Visual Basic 2008 express edition database mysql)
Imports MySql.Data.MySqlClient
Public Class Form2
Public conn As MySqlConnection
Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Application.DoEvents()
Button1.Focus()
conn = New MySqlConnection
'conn.ConnectionString = "server=localhost;database=ilmu;userid=root;password= ''"
Try
conn.Open()
Catch ex As Exception
MessageBox.Show("Error1: " & ex.Message)
End Try
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
conn = New MySqlConnection("server=localhost;database=ilmu;userid=root;password= ''")
Try
conn.Open()
Dim sqlquery As String = "SELECT * FROM visitor WHERE nama = '" & TextBox1.Text & "';"
Dim data As MySqlDataReader
Dim adapter As New MySqlDataAdapter
Dim command As New MySqlCommand
command.CommandText = sqlquery
command.Connection = conn
adapter.SelectCommand = command
data = command.ExecuteReader
While data.Read()
If data.HasRows() = True Then
If data(2).ToString = TextBox2.Text Then
command = New MySqlCommand
command.Connection = conn
tkhupd = Now.ToString("yyyy-MM-dd HH:mm:tt")
command.CommandText = "INSERT INTO visitor(noK,khupd)VALUES ('" & TextBox1.Text & "','" & tkhupd & "')"
command.ExecuteNonQuery()
MessageBox.Show(" Berjaya, Sila Masuk. ", "Tahniah", MessageBoxButtons.OK, MessageBoxIcon.Information)
Else
MsgBox("exist")
End If
Else
MsgBox("Failed Login.")
End If
End While
Catch ex As Exception
End Try
End Sub
End Class
I am not sure what you are trying to do when there is not matching record in the database, but you don't have any code that would be hit in the case of no matching entries.
If there are no matching records, your while condition isn't met and nothing in the loop happens.
Fixing it likely involves rearranging the order of your loop and your if condition.
Check to see if data.hasRows first.
Example:
If data.HasRows() = True Then
While Data.Read
//code here for found rows
End While
Else
//code for no matching entries
End If
And as has been mentioned in Joel's comment, you really should look at using parameterized queries.
example of your insert command altered:
command.CommandText = "INSERT INTO visitor(noK,khupd)VALUES (?noK,?khupd)"
command.Parameters.AddWithValue("?noK",TextBox1.Text)
command.Parameters.AddWithValue("?khupd", tkhupd)
command.ExecuteNonQuery()