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

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

Related

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.

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

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

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

Can't display results of mysql query

I'm new to Visual Basic, I am quite proficient in php which is why I moved on to VB. I am having trouble getting data and displaying it. Here's my full code which is supposed to look in my database "game", find the row with username "kenazz" and display the username "kenazz".
Imports MySql.Data.MySqlClient
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim cs As String = "Database=game;Data Source=localhost;" _
& "User Id=root;Password="
Dim conn As New MySqlConnection(cs)
Try
conn.Open()
Dim stm As String = "SELECT * FROM users WHERE users.username = 'Kenazz'"
Dim cmd As MySqlCommand = New MySqlCommand(stm, conn)
Dim reader As MySqlDataReader = cmd.ExecuteReader()
While reader.Read()
Console.WriteLine(reader.GetInt32("id") & ": " _
& reader.GetString("username"))
End While
reader.Close()
Catch ex As MySqlException
Console.WriteLine("Error: " & ex.ToString())
Finally
conn.Close()
End Try
End Sub
End Class
Where am I going wrong?

SQL parameterized? I'm lost

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 )