As a Vb noob im working on this school project. I need to insert my values into my mysql database but for a reason it isn't inserting tried everything but i can't find why it isn't inserting.
Thx in advance
Dim sqlCommand As New MySqlCommand
Dim SQLConnection As MySqlConnection = New MySqlConnection
Dim strStockSQL As String
Dim server As String = "localhost"
Dim DatabaseName As String = "Gip"
Dim userName As String = "root"
Dim password As String = ""
SQLConnection = New MySqlConnection()
If Not conn Is Nothing Then conn.Close()
conn.ConnectionString = String.Format("server={0}; user id={1}; password={2}; database={3}; pooling=false", server, userName, password, DatabaseName)
Try
strStockSQL = "insert into stock (Barcode,Naam_Product,Verkoopprijs) values (#Barcode,#Naam_product,#Verkoopprijs)"
sqlCommand.Connection = SQLConnection
sqlCommand.CommandText = strStockSQL
sqlCommand.Parameters.AddWithValue("#Barcode", Convert.ToString(txtBarcode.Text))
sqlCommand.Parameters.AddWithValue("#Naam_product", Convert.ToString(txtNaam.Text))
sqlCommand.Parameters.AddWithValue("#Verkoopprijs", Convert.ToInt32(txtVP.Text))
sqlCommand.ExecuteNonQuery()
Catch ex As Exception
MsgBox("Error occured: Could not insert record")
When executing an sqlCommand you must have it's related connection object in open state.
SQLConnection.Open()
sqlCommand.ExecuteNonQuery()
SQLConnection.Close()
Also, read about Using statement and use it for SqlConnection.
Another thing: this code line is meaningless: If Not conn Is Nothing Then conn.Close() remove it.
Related
I want to get all the rows of table of a MySQL database and store them in a listbox. I am using vb.net. Thanks in advance.
Dim connection As New SqlConnection
Dim cmd As New SqlCommand
Dim dr As SqlDataReader
Dim Column0 As String
connection.ConnectionString = "Data Source=localhost; Initial Catalog=catalogname; Integrated Security=True; Connection Timeout=3"
Dim query As String = "SELECT * FROM MyDB"
connection.Open()
cmd = New SqlCommand(query, connection)
dr = cmd.ExecuteReader
If dr.Read() Then
Column0 = dr(0)
End If
connection.Close()
When I try and execute the following code to input a value into a text box it comes up with an error saying : Authentication to host '' for user '' using method 'mysql_native_password' failed with message: Access denied for user ''#'Adam-PC' (using password: NO)
Public Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles Datetxt.TextChanged
Dim ConnectionString As String
Dim con As New MySqlConnection
ConnectionString = "server=localhost;user id=root;database=river_data;allowuservariables=True"";Integrated Security=True;MultipleActiveResultSets=True;"
Dim cmd As MySqlCommand
Dim query As String = "SELECT date FROM river-derwent-keswick-portinscale WHERE(`date` = '01.01.2013') "
cmd = New MySqlCommand(query, con)
con.Open()
Dim myreader As MySqlDataReader = cmd.ExecuteReader()
If myreader.Read() Then
Datetxt.Text = myreader.GetValue(0)
Else
Me.Close()
HomeForm.Show()
End If
myreader.Close()
ConnectionString = "server=localhost;user id=root;database=river_data;allowuservariables=True"";Integrated Security=True;MultipleActiveResultSets=True;"
it looks like the user part is wrong. Try:
ConnectionString = "server=localhost;user=root;database=river_data;allowuservariables=True"";Integrated Security=True;MultipleActiveResultSets=True;"
I get a fatal error when I execute this code, but the info still shows up in the database. Can anyone see an error here?
sql.RunQuery("INSERT INTO test(test1,test2,test3) values(?FName,?LName,?DOB)")
sql.SQLcmd.Parameters.AddWithValue("?FName", Gender.SelectedItem)
sql.SQLcmd.Parameters.AddWithValue("?LName", Age)
sql.SQLcmd.Parameters.AddWithValue("?DOB", RDIAge)
sql.SQLcmd.ExecuteNonQuery()
Your code should looks like:
Dim connString As String = "Database=yourDB;Data Source=localhost;";
connString +="User Id=yourUserDb;Password=dbPass"
Dim conn As New MySqlConnection(connString)
Dim cmd As New MySqlCommand()
Try
conn.Open()
cmd.Connection = conn
cmd.CommandText = "INSERT INTO test(test1,test2,test3) values(#FName,#LName,#DOB)"
cmd.Prepare()
cmd.Parameters.AddWithValue("#FName", Gender.SelectedItem)
cmd.Parameters.AddWithValue("#LName", Age)
cmd.Parameters.AddWithValue("#DOB", RDIAge)
cmd.ExecuteNonQuery()
conn.Close()
Catch ex As MySqlException
Console.WriteLine("Error: " & ex.ToString())
End Try
I Have Login Form in VB.net
but i got problem if i access my localhost , previous the code is working, but after i leave the VS and go back again, i got problem like this http://i.imgur.com/SfMghZj.png
this is my source code
MySqlConn = New MySqlConnection
MySqlConn.ConnectionString = "server=localhost;userid=root;password=***;database=exodium"
Dim Reader As MySqlDataReader
Try
MySqlConn.Open()
Dim Query As String
Query = "select * from exodium.member where Username='" & UsernameTxt.Text & "' and Password='" & PasswordTxt.Text & "'"
Command = New MySqlCommand(Query, MySqlConn)
Reader = Command.ExecuteReader
Dim count As Integer
count = 0
While Reader.Read
count = count + 1
End While
If count = 1 Then
Loading.Show()
ElseIf count > 1 Then
MessageBox.Show("Duplicate !")
Else
MessageBox.Show("Not Correct !")
End If
MySqlConn.Close()
Catch ex As MySqlException
MessageBox.Show(ex.Message)
Finally
MySqlConn.Dispose()
End Try
and this is my Localhost http://i.imgur.com/CfeOHuh.png
anyone can help? thanks T_T
It may be possible of that you don't have permission on mysql database.
Look into mysql.user table that you have entry for localhost and that password[Will be in encrypted form].
if not please insert one and use
CREATE USER 'root'#'localhost' IDENTIFIED BY '14253690';
GRANT ALL PRIVILEGES ON * . * TO 'root'#'localhost' IDENTIFIED BY '14253690';
FLUSH PRIVILEGES;
Try
Dim MySqlConn As MySqlConnection
Dim COMMAND As MySqlCommand
MySqlConn = New MySqlConnection
MySqlConn.ConnectionString = "server=localhost;user id=root;password=;database=exodium"
Dim READER As MySqlDataReader
MySqlConn.Open()
Dim Query As String
Query = "SELECT Username,Password FROM member"
COMMAND = New MySqlCommand(Query, MySqlConn)
READER = COMMAND.ExecuteReader
While READER.Read
Dim userNameDB = READER.GetString("Username")
Dim PasswordDB = READER.GetString("Password")
Dim userName As String = UsernameTxt.Text
Dim Password As String = PasswordTxt.Text
If userNameDB = userName And PasswordDB = Password Then
MessageBox.Show("Duplicate !")
Else
MessageBox.Show("Not Correct !")
End If
End While
MySqlConn.Close()
Catch myerror As Exception
MessageBox.Show(myerror.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
I have a function in VB.NET that authenticates a user towards a MySQL database before launching the main application. Here's the code of the function:
Public Function authConnect() As Boolean
Dim dbserver As String
Dim dbuser As String
Dim dbpass As String
dbserver = My.Settings.dbserver.ToString
dbuser = My.Settings.dbuser.ToString
dbpass = My.Settings.dbpass.ToString
conn = New MySqlConnection
myConnString = "server=" & dbserver & ";" & "user id=" & dbuser & ";" & "password=" & dbpass & ";" & "database=rtadmin"
Dim myCommand As New MySqlCommand
Dim myAdapter As New MySqlDataAdapter
Dim myData As New DataTable
Dim myDataReader As New MySqlDataReader
Dim query As String
myCommand.Parameters.Add(New MySqlParameter("?Username", login_usr_txt.Text))
myCommand.Parameters.Add(New MySqlParameter("?Password", login_pass_txt.Text))
query = "select * from users where user = ?Username and passwd = ?Password"
conn.ConnectionString = myConnString
Try
conn.Open()
Try
myCommand.Connection = conn
myCommand.CommandText = query
myAdapter.SelectCommand = myCommand
myDataReader = myCommand.ExecuteReader
If myDataReader.HasRows() Then
MessageBox.Show("You've been logged in.", "RT Live! Information", MessageBoxButtons.OK, MessageBoxIcon.Information)
End If
Catch ex As Exception
End Try
Catch ex As Exception
End Try
End Function
The function is not yet complete, there are a few other things that need to be done before launching the application, since I'm using a MessageBox to display the result of the login attempt.
The error that I'm getting is the following:
Error 1 'MySql.Data.MySqlClient.MySqlDataReader.Friend Sub New(cmd As MySql.Data.MySqlClient.MySqlCommand, statement As MySql.Data.MySqlClient.PreparableStatement, behavior As System.Data.CommandBehavior)' is not accessible in this context because it is 'Friend'. C:\Users\Mario\documents\visual studio 2010\Projects\Remote Techs Live!\Remote Techs Live!\Login.vb 43 13 Remote Techs Live!
Any ideas?
It makes no sense to try to create a MySqlDataReader and then throw it away!
First you do this to attempt to create a reader:
Dim myDataReader As New MySqlDataReader
Then later you attempt to throw that away when you do this:
myDataReader = myCommand.ExecuteReader
Just remove the New from your initial declaration. I suspect that the constructor for the MySqlDataReader is not publicly accessible.
You could try the .AddWithValue instead of .Add.
For example:
cmd.Parameters.AddWithValue("?Username", login_usr_txt.Text)