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
Related
I'm trying to add data to a MySQL Server database.
Here is the code:
Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim connection As SqlConnection = New SqlConnection("Data Source=192.168.3.173\hidp;Initial Catalog=HIDP;Persist Security Info=True;User ID=intranet;Password=kikapu;APP=Intranet")
Dim sql As String = "Insert into [RAVARIAS].[StockMovimentos] ([CodHospital], [utilizador], [servico], [CodDevice], [CodConsumivel], [TipoMovimento], [DtMovimento], [Qtd]) values (#CodHospital, #utilizador, #servico, #CodDevice, #CodConsumivel, #TipoMovimento, #DtMovimento, #Qtd)"
Dim cmd As SqlCommand = New SqlCommand(sql, connection)
cmd.Parameters.AddWithValue("#CodHospital", Session("Codhospital"))
cmd.Parameters.AddWithValue("#utilizador", TextBox1.Text)
cmd.Parameters.AddWithValue("#servico", TextBox2.Text)
cmd.Parameters.AddWithValue("#CodDevice", DropDownList3.SelectedItem)
cmd.Parameters.AddWithValue("#CodConsumivel", DropDownList4.SelectedItem)
cmd.Parameters.AddWithValue("#TipoMovimento", DropDownList2.SelectedItem)
cmd.Parameters.AddWithValue("#DtMovimento", Calendar1.SelectedDate)
cmd.Parameters.AddWithValue("#Qtd", TextBox4.Text)
connection.Open()
cmd.ExecuteNonQuery()
connection.Close()
TextBox1.Text = ""
TextBox2.Text = ""
TextBox4.Text = ""
DropDownList2.ClearSelection()
DropDownList3.ClearSelection()
DropDownList4.ClearSelection()
Calendar1.SelectedDates.Clear()
End Sub
When I click on the button nothing happens.
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
im trying to retrieve mysql data with specific column and show to textbox in vb.net. what should i do in retrieving it?
Dim connect As New MySqlConnection("server=localhost; user id=root; password= ; database=ticketing_system;")
connect.Open()
Dim sqladapter As New MySqlDataAdapter
Dim sqlcmd As New MySqlCommand
Dim dr As MySqlDataReader
Dim dt As New DataTable
sqlcmd = New MySqlCommand("SELECT * complaint WHERE tran_no='" & lbltranno.Text & "'")
**THEN? WHAT SHOULD I DO TO DISPLAY DATA? PLEASE HELP**
connect.Close()
You are simply missing the Execution method. It depends on what kind of result you want. If you only want the first result from the query (first row and first column) then use sqlcmd.ExecuteScalar().
If you want all the results you'll have to load that into a MySqlDataReader using the method sqlcmd.ExecuteReader()
Using ExecuteReader() :
Dim connect As New MySqlConnection("server=localhost; user id=root; password= ; database=ticketing_system;")
connect.Open()
Dim sqladapter As New MySqlDataAdapter
Dim sqlcmd As New MySqlCommand
Dim dr As MySqlDataReader
Dim dt As New DataTable
sqlcmd = New MySqlCommand("SELECT * complaint WHERE tran_no='" & lbltranno.Text & "'")
dr = sqlcmd.ExecuteReader()
dt.Load(dr)
'Useable datatable in dt variable...
connect.Close()
Using ExecuteScalar() :
Dim connect As New MySqlConnection("server=localhost; user id=root; password= ; database=ticketing_system;")
connect.Open()
Dim sqladapter As New MySqlDataAdapter
Dim sqlcmd As New MySqlCommand
Dim dr As String
Dim dt As New DataTable
sqlcmd = New MySqlCommand("SELECT [COLUMN NAME] complaint WHERE tran_no='" & lbltranno.Text & "'")
dr = sqlcmd.ExecuteScalar()
'dr now contains the value of [COLUMN NAME] for the first returned row.
connect.Close()
You can try this:
Dim connString As String = "server=localhost; user id=root; password=[REPLACE WITH PASSWORD] ; database=ticketing_system;"
Dim sqlQuery As String = "SELECT * from complaint WHERE tran_no='" & lbltranno.Text & "'";
Using sqlConn As New MySqlConnection(connString)
Using sqlComm As New MySqlCommand()
With sqlComm
.Commandtext = sqlQuery
End With
Try
sqlConn.Open()
Dim sqlReader As MySqlDataReader = sqlComm.ExecuteReader()
While sqlReader.Read()
Label1.Text = sqlReader("Name").ToString()
Label2.Text = sqlReader("Points").ToString()
End While
Catch ex As MySQLException
' add your exception here '
End Try
End Using
End Using
Imports System.Data.SqlClient
Public Class Form1
Dim c As New SqlConnection("Data Source=SONY;Initial Catalog=msdb;Integrated Security=True")
Dim cmd As New SqlCommand
Dim da As New SqlDataAdapter
Dim dr As SqlDataReader
Dim ds, ds1 As New DataSet
Private Sub btnsub_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnsub.Click
Try
ds.Clear()
c.Open()
cmd = New SqlCommand("select Sub_Name from Subject_Detail WHERE Course='" + txtcourse.Text + "'", c)
da = New SqlDataAdapter(cmd)
da.Fill(ds, "Subject_Detail")
Label1.Text = ds.Tables(0).Rows(0).Item(0)
Label2.Text = ds.Tables(0).Rows(1).Item(0)
Label3.Text = ds.Tables(0).Rows(2).Item(0)
Label4.Text = ds.Tables(0).Rows(3).Item(0)
Label5.Text = ds.Tables(0).Rows(4).Item(0)
Label6.Text = ds.Tables(0).Rows(5).Item(0)
Catch ex As Exception
MsgBox(ex.Message)
Finally
c.Close()
End Try
End Sub
End Class
'This code display all subject name from the table into labels.This might be helped.
Try this one, i always use this code, that's why i'm pretty sure that this will work...
Imports MySql.Data
Imports MySql.Data.MySqlClient
Dim connect As New MySqlConnection("server=localhost;uid=root;database=ticketing_system;pwd=;")
connect.Open()
Dim sqlcmd As New MySqlCommand("SELECT * complaint WHERE tran_no='" & lbltranno.Text & "'")
Dim sqladapter As New MySqlDataAdapter(sqlcmd)
Dim dt As New DataTable
sqladapter.Fill(dt)
Datagridview1.Datasource = dt
connect.Close()
Imports MySql.Data
Imports MySql.Data.MySqlClient
Dim dbConnection As New MySqlConnection("server=localhost;uid=youruser;database=yourdb;pwd=password")
try
dbConnection.open
catch ex as exception
debug.print(ex.message)
end try
using Adapter as new mysqldataadapter("select * from yourtable where yourcolumn = 'yourvalue'", dbConnection)
using Table as new datatable
Adapter.fill(Table)
Datagridview.datasource = table
end using
end using
dbConnection.close
Imports MySql.Data.MySqlClient
''--- Data Reader Example
private _cn as New MySqlConnection( _
"data source=server; database=databaseName; user id=username; password=password")
private cmd as new MySqlCommand
private dr as MySqlDataReader
With cmd
.Command = "select Name, Address from Clients"
.CommandType = CommandType.Text
.Connection = cn
End With
cn.Open()
dr = cmd.ExecuteReader
While dr.Read
'' use dr(0) for get Name, and dr(1) to get Address for this example
Console.WriteLine(dr(0) & " - " & dr(1))
Console.WriteLine(dr("Name").ToString() & " - " & dr("Address").ToString())
End While
dr.Close
cn.Close
Dim MysqlConn As MySqlConnection
Dim connectionString As String = "Server=-host-ip-;Database=Mysqldb;Uid=user;Pwd=password"
MysqlConn = New MySqlConnection(connectionString)
Try
Dim Mysqlcmd As New MySqlCommand("SELECT * FROM Mysqldb.table WHERE col='" & Label6.Text & "'", MysqlConn)
Dim dt As New DataTable
Dim Mysqladapter As New MySqlDataAdapter()
MysqlConn.Open()
Mysqladapter.SelectCommand = Mysqlcmd
Mysqladapter.Fill(dt)
DataGridView1.DataSource = dt
MessageBox.Show("Connection to Database has been opened.")
MysqlConn.Close()
Catch myerror As MySqlException
MessageBox.Show("Cannot connect to database: " & myerror.Message)
Finally
MysqlConn.Dispose()
End Try
The key is to add the connection at the end of the mysql query as in line 6
I used a variation of the code given in this thread with Visual Basic 2015. I found I needed a 2nd argument in the MySqlCommand statement, namely the MySqlConnection variable ("connect" in some of the examples). See thread: Connection must be valid and open VB.Net
Thanks for the help.
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
How to Inserting Label.text data into mySql table.
i have no problem with textbox.text but i can't figure out how it with Label.text
i try the same code with textbox.text
parameterB_Answer.Value = TextBox1.Text
it work find but when i try with
parameterB_Answer.Value = Label1.Text
mySqlReader seems can't read it.
Update:
1.1.1 is label1.text. My Idea is to insert the text "1.1.1" from Label1 as Primary key and the Textbox(textbox1.text) as the following
my code is:
Try
Dim StrSQL As String = "INSERT INTO boranga" & _
"(IdBorangA,Answers)" & _
"VALUES (#B_IdA,#B_Answer);"
Dim myCommand As MySqlCommand = New MySqlCommand(StrSQL, conn.open)
myCommand.CommandType = CommandType.Text
Dim parameterB_IdA As MySqlParameter = New MySqlParameter("#B_IdA", MySqlDbType.VarChar, 300)
parameterB_IdA.Value = Label1.Text
Dim parameterB_Answer As MySqlParameter = New MySqlParameter("#B_Answer", MySqlDbType.VarChar, 300)
parameterB_Answer.Value = TextBox1.Text
With myCommand.Parameters
.Add(parameterB_IdA)
.Add(parameterB_Answer)
End With
Dim result As MySqlDataReader = myCommand.ExecuteReader(CommandBehavior.CloseConnection)
MsgBox("Tersimpan", vbYes, "boranga")
Catch SqlEx As MySqlException
Throw New Exception(SqlEx.Message.ToString())
End Try
but when I change the value of Label1.text (1.1.1) to 111, it works just fine. probably because I put INT for the column for label1.text to fill while "1.1.1" isn't integer
thank a lot
PS:seems i can't post image because low of reputation
Try using this code format first of all:
Private Sub btnLogin_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLogin.Click
Dim conn As MySqlConnection
'Connect to the database using these credentials
conn = New MySqlConnection
conn.ConnectionString = "server=your server site (generally long url); user id=login id for mysql user; password=self explanatory; database=name of the DB you're trying to reach"
'Try and connect (conn.open)
Try
conn.Open()
Catch myerror As MySqlException 'If it fails do this... (i.e. no internet connection, etc.)
MsgBox("Error connecting to database. Check your internet connection.", MsgBoxStyle.Critical)
End Try
'MySQL query (where to call for information)
Dim myAdapter As New MySqlDataAdapter
'Tell where to find the file with the emails/passes stored
Dim sqlquery = "SELECT * FROM the database you selected above WHERE Email = '" & txtEmail.Text & "' AND Password = '" & txtPassword.Text & "'"
Dim myCommand As New MySqlCommand
myCommand.Connection = conn
myCommand.CommandText = sqlquery
'Start query
myAdapter.SelectCommand = myCommand
Dim myData As MySqlDataReader
myData = myCommand.ExecuteReader
If myData.HasRows = 0 Then
MsgBox("Invalid email address or password.", MsgBoxStyle.Critical)
Else
MsgBox("Logged in as " & txtEmail.Text & ".", MsgBoxStyle.Information)
Me.Close()
End If
End Sub
And to insert label.text try replacing one of the textbox.text fields first and see if it will accept it. If it does, then the answer was all in the formatting.
Also, do not forget to call:
imports mysql.data.mysqlclient
and make sure to add the mysql.data reference.