Retrieve DataRow after get sql - mysql

I`m new in VB.net and i need to create common function to retrieve data row. I tried this way. But this is not successful.
Function GetRecord(ByVal sql As String) As DataRow
Dim conn As MySqlConnection = connect()
Dim objDataSet As New DataSet
Dim dt As DataTable
Dim dr As DataRow
Try
Dim cmd As New MySqlCommand()
conn.Open()
cmd.Connection = conn
cmd.CommandText = sql
Dim da As New MySqlDataAdapter
da.SelectCommand = cmd
Dim ds As New DataSet
da.Fill(ds, "T")
dt = ds.Tables("T")
dr = dt.Rows.Item(0)
Catch e As Exception
Throw e
Finally
conn.Close()
End Try
GetRecord = dr
End Function
remark: Connect() function returns mysql connection object

Not exactly sure what are you trying to do but change your code to be like
da.Fill(dt, "T")
Catch e As Exception
Throw e
Finally
conn.Close()
End Try
GetRecord = dt.Rows.Item(0)

Related

Reference required to assembly 'System.Data.Common', containing the type 'DBConnection'

I'm making a program in VB with MySQL. For some reason, I got an error where all of my references had a yellow triangle, but I have since fixed that by reinstalling them on NuGet. Now I have 39 errors which I didn't have before, and I haven't edited the code since then.
Here are the errors:
errors
That isn't all of the errors, but the rest are a lot of duplicates. Now here's my form1 code.
Imports MaterialSkin
Imports MySql.Data.MySqlClient
Imports System.Net
Imports System.Text
Imports System.Data
Public Class Form1
Private Sub LoginBtn_Click(sender As Object, e As EventArgs) Handles LoginBtn.Click
Dim address As String
address = "http://ipv4bot.whatismyipaddress.com"
Dim client As WebClient = New WebClient()
Dim reply As String = client.DownloadString(address)
Dim connString As String = "Database=aquarius;Data Source=localhost;User Id=root;Password=asd"
Dim conn As New MySqlConnection(connString)
Dim Query As String
Dim Query2 As String
Dim reader As MySqlDataReader
Try
conn.Open()
Query = "SELECT * FROM login WHERE username = ?UserName AND password = ?Password AND buyer = ?Buyer"
Dim cmd As New MySqlCommand(Query, conn)
cmd.Parameters.Add(New MySqlParameter("?UserName", MaterialSingleLineTextField1.Text))
cmd.Parameters.Add(New MySqlParameter("?Password", MaterialSingleLineTextField2.Text))
cmd.Parameters.Add(New MySqlParameter("?Buyer", "true"))
cmd.Connection = conn
reader = cmd.ExecuteReader
If reader.HasRows() Then
conn.Close()
MessageBox.Show("Thanks for buying Aquarius", "Welcome")
conn.Open()
Query2 = "INSERT INTO logs (ip, user, time) VALUES (?Ip, ?User, ?Time)"
Dim cmd2 As New MySqlCommand(Query2, conn)
cmd2.Parameters.Add(New MySqlParameter("?Ip", reply.ToString))
cmd2.Parameters.Add(New MySqlParameter("?User", Environment.UserName))
cmd2.Parameters.Add(New MySqlParameter("?Time", System.DateTime.Now.ToString()))
cmd2.ExecuteNonQuery()
conn.Close()
Form2.Show()
Me.Close()
Else
MessageBox.Show("Your password is incorrect or you are not whitelisted.", "Sad")
End If
Catch ex As Exception
MessageBox.Show(ex.ToString, "Error")
End Try
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim SkinManager As MaterialSkinManager = MaterialSkinManager.Instance
SkinManager.AddFormToManage(Me)
SkinManager.Theme = MaterialSkinManager.Themes.DARK
SkinManager.ColorScheme = New ColorScheme(Primary.LightBlue500, Primary.LightBlue600, Primary.LightBlue700, Accent.LightBlue200, TextShade.WHITE)
End Sub
Private Sub MaterialRaisedButton1_Click(sender As Object, e As EventArgs) Handles MaterialRaisedButton1.Click
Dim connString As String = "Database=aquarius;Data Source=localhost;User Id=root;Password=asd"
Dim conn As New MySqlConnection(connString)
Dim Query As String
Try
conn.Open()
Query = "INSERT INTO login (username, password, buyer) VALUES (?UserName, ?Password, ?Buyer)"
Dim cmd As New MySqlCommand(Query, conn)
cmd.Parameters.Add(New MySqlParameter("?UserName", MaterialSingleLineTextField1.Text))
cmd.Parameters.Add(New MySqlParameter("?Password", MaterialSingleLineTextField2.Text))
cmd.Parameters.Add(New MySqlParameter("?Buyer", "false"))
cmd.Connection = conn
cmd.ExecuteNonQuery()
conn.Close()
MessageBox.Show("Done!", ":)")
Catch ex As Exception
MessageBox.Show(ex.ToString, "Error")
End Try
End Sub
End Class

Displaying a value obtained from a mysql query into a VB.net textbox

I am trying to display a value I obatain from a COUNT(*) query in a textbox in vb.net but instead of displaying the obtained value it displays the actual query.
Here is my code:
Imports MySql.Data.MySqlClient
Public Class Statistics
Dim conn As MySqlConnection
Dim command As MySqlCommand
Dim query As String
Dim dadapter As New MySqlDataAdapter
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
conn = New MySqlConnection
conn.ConnectionString =
"server=localhost;userid=root;database=librarydatabase"
Dim reader As MySqlDataReader
Try
conn.Open()
query = "SELECT COUNT(*) FROM login where users='" & Username.Text & "'"
command = New MySqlCommand(query, conn)
reader = command.ExecuteReader
dadapter.SelectCommand = command
If reader.HasRows Then
reader.Read()
takenout.Text = (query)
End If
conn.Close()
Catch ex As MySqlException
MessageBox.Show(ex.Message)
Finally
conn.Dispose()
End Try
End Sub
Here is a screenshot of my program:
When trying to fix the code I change it to replace takenout.Text = (query) with takenout.Text = (reader.read()) and deleting the reader.read()) above and dadapter.SelectCommand = command
Here is the changed code:
Imports MySql.Data.MySqlClient
Public Class Statistics
Dim conn As MySqlConnection
Dim command As MySqlCommand
Dim query As String
Dim dadapter As New MySqlDataAdapter
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
conn = New MySqlConnection
conn.ConnectionString =
"server=localhost;userid=root;database=librarydatabase"
Dim reader As MySqlDataReader
Try
conn.Open()
query = "SELECT COUNT(*) FROM login where users='" & Username.Text & "'"
command = New MySqlCommand(query, conn)
reader = command.ExecuteReader
If reader.HasRows() Then
takenout.Text = (reader.Read())
End If
conn.Close()
Catch ex As MySqlException
MessageBox.Show(ex.Message)
Finally
conn.Dispose()
End Try
End Sub
This code doesn't fix the problem but instead displays true in the textbox when the button is pressed no matter whats in the username textbox.
Here is an image of that:
Use command.ExecuteScalar() instead.
Using command.executescalar() fixes the issue.
Here is a copy of the fixed code if anyone else has a similar issue and wants to see a working example.
Public Class Statistics
Dim conn As MySqlConnection
Dim command As MySqlCommand
Dim query As String
Dim dadapter As New MySqlDataAdapter
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
conn = New MySqlConnection
conn.ConnectionString =
"server=localhost;userid=root;database=librarydatabase"
Dim numtakenout As String
Try
conn.Open()
query = "SELECT COUNT(*) FROM login where users='" & Username.Text & "'"
command = New MySqlCommand(query, conn)
numtakenout = Convert.ToString(command.ExecuteScalar())
takenout.Text = numtakenout
conn.Close()
Catch ex As MySqlException
MessageBox.Show(ex.Message)
Finally
conn.Dispose()
End Try
End Sub

check the manual that corresponds to your mariadb server version for the right syntax to use near "user" at line 1

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 + " ;"

Too many arguments to 'Public Sub New() VB .NET

I use MySqlBackup.NET_2.0.9.2 to backup a MySQL database.
I use this code:
Private Sub cmdExportBazaDate_Click(sender As System.Object, e As System.EventArgs) Handles cmdExportBazaDate.Click
Dim constring As String = Parametriiconexiune
Dim zi As String = data
Dim file As String = cale_salvari + data + "_baza" + ".sql"
Dim conn As MySqlConnection
conn = New MySqlConnection(constring)
Dim cmd As MySqlCommand
cmd = New MySqlCommand
cmd.Connection = conn
conn.Open()
Dim mb As MySqlBackup
mb = New MySqlBackup(cmd)
mb.ExportToFile(file)
conn.Close()
End Sub
I get the error:
Too many arguments to 'Public Sub New()'
For this line:
mb = New MySqlBackup(cmd)
How to fix please to bypass the error.

What is wrong in this code Vb MySql data to TextBox

What is wrong in this code
I run the software click the button and nothing happens if you have a better code that would help thanks in advance
Dim sql As String
Dim con As MySqlConnection = New
MySqlConnection("Server=localhost;Database=******;Uid=*****;password =******")
Dim ds As DataSet = New DataSet
Dim dataadapter As MySqlDataAdapter = New MySqlDataAdapter
Dim cmd As MySqlCommand = New MySqlCommand()
Dim datareader As MySqlDataReader
Try
sql = "SELECT * FROM users"
con.Open()
cmd.CommandText = sql
cmd.Connection = con
dataadapter.SelectCommand = cmd
datareader = cmd.ExecuteReader
While datareader.Read
datareader.Read()
Email.Text = datareader("Email")
LastName.Text = datareader("LastName")
Address.Text = datareader("Address")
End While
Catch ex As Exception
End Try
con.Close()
End Sub
Narrow the scope of your select. The way its written I think it might set the textbox 100 times if 100 results are returned. Here is how I would write this:
Imports MySql.Data.MySqlClient
Public Sub getData()
Dim objConn As MySqlConnection = New MySqlConnection("Data Source=localhost;" _
& "Database=TestDB;" _
& "User ID=Root;" _
& "Password=myPassword;")
Dim strSQL As String = "SELECT TOP 1 Email, LastName, Address FROM users"
Dim da As MySql.Data.MySqlClient.MySqlDataAdapter
Dim dt As New DataTable
Try
objConn.Open()
da = New MySql.Data.MySqlClient.MySqlDataAdapter(strSQL, objConn)
da.Fill(dt)
If dt.Rows.Count > 0 Then
Email.Text = dt.Rows(0)("Email").ToString
LastName.Text = dt.Rows(0)("LastName").ToString
Address.Text = dt.Rows(0)("Address").ToString
End If
da = Nothing
objConn.Close()
objConn = Nothing
Catch ex As Exception
objConn.Close()
objConn = Nothing
End Try
End Sub