I want to check if a user is an administrator or user and I want to change the dataset to a string so I can use it in Label1.
I've tried Using 'GetXML' but it comes in a funny format something like 'Administrator', well it's a bit different to that. I want to be able to use the value of the label1.text for other features in my program.
Here is my code:
Dim SQLConnection As MySqlConnection = New MySqlConnection
Dim ServerString As String = "server=localhost;user id=root;password=;database=business elements"
SQLConnection.ConnectionString = ServerString
SQLConnection.Open()
Dim ds As New DataSet()
Dim cmd As New MySqlCommand("SELECT Level FROM users WHERE username = '" & UsernameTextBox.Text & "'", SQLConnection)
Dim da As New MySqlDataAdapter(cmd)
da.Fill(ds)
Dim str As String = ds.GetXml
ds.GetXml()
Label1.Text = str
Try this
Dim str As String = ds.Tables(0).Rows(0).Item(0).ToString()
Related
i am actually having a problem about this, can you please help me out?
i'm still a beginner and i don't know how to fix this...
Private Sub textSearch_TextChanged(sender As Object, e As EventArgs) Handles textSearch.TextChanged
MysqlConn = New MySqlConnection
MysqlConn.ConnectionString =
"server=localhost;userid=root;password=root;database=student_info"
Dim SDA As MySqlDataAdapter
Dim dbDataSet As New DataTable
Dim DV As New DataView(dbDataSet)
Dim bsource As New BindingSource
Dim Query As String
Dim COMMAND As MySqlCommand
Dim ds As New DataSet
Try
Query = "select * from std_info where firstname like ' % " & textSearch.Text & " % '"
SDA.Fill(ds)
COMMAND = New MySqlCommand(Query, MysqlConn)
bsource.DataSource = dbDataSet
DataGridView1.DataSource = bsource
SDA.Update(dbDataSet)
MysqlConn.Close()
Catch ex As Exception
MessageBox.Show(ex.Message)
Finally
MysqlConn.Dispose()
End Try
End Sub
Well the error occurs because you have declared it
Dim SDA As MySqlDataAdapter
but not instantiated (i.e. set it to an instance of an object) it before you call
SDA.Fill(ds)
I have no idea of what exactly your code is doing as I've never used MySQL etc. but at least try something like
Query = "select * from std_info where firstname like ' % " & textSearch.Text & " % '"
COMMAND = New MySqlCommand(Query, MysqlConn)
SDA = new MySqlDataAdapter(COMMAND)
SDA.Fill(ds)
The fact that ds is an empty new DataSet worries me a little but that could just be my ignorance of MySQL classes.
I already run the program, the only thing is that I still cannot see the data in the table and show it to the datagridview, and also when I try to search no results are shown... help? (i am just a beginner don't be angry at me pls.)
Private Sub textSearch_TextChanged(sender As Object, e As EventArgs) Handles textSearch.TextChanged
MysqlConn = New MySqlConnection
MysqlConn.ConnectionString =
"server=localhost;userid=root;password=root;database=student_info"
Dim Query As String
Dim COMMAND As MySqlCommand
Dim SDA As New MySqlDataAdapter
Dim dbDataSet As New DataTable
Dim DV As New DataView(dbDataSet)
Dim bsource As New BindingSource
Try
Query = "select * from std_info where firstname like ' % " & textSearch.Text & " % '"
COMMAND = New MySqlCommand(Query, MysqlConn)
SDA = New MySqlDataAdapter(COMMAND)
SDA.Fill(dbDataSet)
bsource.DataSource = dbDataSet
DataGridView1.DataSource = bsource
SDA.Update(dbDataSet)
MysqlConn.Close()
Catch ex As Exception
MessageBox.Show(ex.Message)
Finally
MysqlConn.Dispose()
End Try
End Sub
I'm trying to extract values from my MySql table and put them onto a form in VB. I want the user to have a list of all the movies he/she is currently renting. I know the MySql statement works so its just a problem with actually getting at the values. Any help would be great! I have the following code:
Private Sub Form9_Load(sender As Object, e As EventArgs) Handles MyBase.Load
NoMovies.Visible = False
Dim query As String
Dim ans As String
Dim ds As New DataSet
Dim da As New MySqlDataAdapter
MysqlConn = New MySqlConnection
Dim Reader As MySqlDataReader
'another query and a two table join'
query = "SELECT movie_name from movie2 Where movie_id=(SELECT movie_id from rental where client_username='" & currentUser & "' AND start_date<='" & DateTime.Now.ToString("yyyy-MM-dd") & "' AND return_date>='" & DateTime.Now.ToString("yyyy-MM-dd") & "')"
MysqlConn.ConnectionString =
"server=mysql.scss.tcd.ie;userid=murpha83;password=gh8ht6789;database=murpha83_db"
MysqlConn.Open()
Command = New MySqlCommand(query, MysqlConn)
da = New MySqlDataAdapter(Command)
da.Fill(ds, "Subject_Detail")
Reader = Command.ExecuteReader
If Reader.HasRows = True Then
'Think I Should extract the values and put them on the form here '
Else
NoMovies.Visible = True
End If
MysqlConn.Close()
Reader.Close()
End Sub
Thanks,
Alan.
I have a problem populating multiple data to multiple textboxes. I've attached a link to the project so that you guys can get the picture.
Thank you so much in advance! I'm no wiz programmer. :)
EDIT: heres the code
` Dim con As MySqlConnection = New MySqlConnection("data source=localhost; user id=root; password=dctvmainadmin; database=tmsdctv")
Dim query As String = "SELECT * FROM recordedattendance WHERE empno = '00111' and dateattended = '2014-02-03 Mon'"
Dim reader As MySqlDataReader
Dim result As New System.Data.DataTable
con.Open()
Dim Cmd_Read_Name As New MySqlCommand(query)
Cmd_Read_Name.Connection = con
result.Load(Cmd_Read_Name.ExecuteReader)
With Me
For Each dtrow As DataRow In result.Rows
.lblEmpno.Text = dtrow(0)
.lblDate1.Text = dtrow(1)
.txtTimeIn1.Text = dtrow(2)
.txtTimeOut1.Text = dtrow(3)
Next
con.Close()
End With`
Try Like This:
Specifing textbox lable name like:
lblEmpno1,lblEmpno2,lblEmpno3.., lblDate1,lblDate2,lblDate3....,
txtTimeIn1,txtTimeIn2,txtTimeIn3..., txtTimeOut1,txtTimeOut2,txtTimeOut3....
Dim con As MySqlConnection = New MySqlConnection("data source=localhost;
user id=root; password=dctvmainadmin; database=tmsdctv")
Dim query As String = "SELECT * FROM recordedattendance WHERE empno
= '00111' and dateattended = '2014-02-03 Mon'"
Dim reader As MySqlDataReader
Dim result As New System.Data.DataTable
con.Open()
Dim Cmd_Read_Name As New MySqlCommand(query)
Cmd_Read_Name.Connection = con
result.Load(Cmd_Read_Name.ExecuteReader)
For xi as integer=0 to result.Rows.Count-1
Controls("lblEmpno" & xi+1 &).Text .Text = result.Rows(xi).Item(0)
Controls("lblDate" & xi+1 &).Text = result.Rows(xi).Item(1)
Controls("txtTimeIn" & xi+1 &).Text = result.Rows(xi).Item(2)
Controls("txtTimeOut" & xi+1 &).Text = result.Rows(xi).Item(3)
Next
con.Close()
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.
I am trying to Select data from MySQL database using VB.NET
Dim conn As New MySqlConnection
Dim cmd As New MySqlCommand
conn.ConnectionString = "Server=localhost; user id=root; password=; database=aplikasi_store_testing;"
cmd.Connection = conn
conn.Open()
Dim Number As Integer
cmd.CommandText = "SELCECT nama_student FROM student where Id_student ='" & id & "'"
but i dont know how to put selected query into variable,
anybody can help me ?
Dim StrVar as String
Dim rd As MySqlDataReader
Dim cmd as New MySqlcommand
cmd.commandtext = "Select student_name from student_table where student_id = #ID"
cmd.connection = conn
rd = cmd.ExecuteReader
if rd.read then
StrVar = rd.GetString(1)
end if
rd.close
Using the Data Reader it will let you assign the result of the query to your variable StrVar and this will come in handy. I use GetString because I assume it is a string type and GetValue for integer. The value "1" represent the column you want to pass to your variable.
Let me know if this works. Cheers..Happy Coding..
you can use ExecuteScalar method as below
object nama_studentObj = cmd.ExecuteScalar()
if nama_studentObj != null then
string nama_student= nama_studentObj .ToString()
Full example code
Dim cs As String = "Database=testdb;Data Source=localhost;" _
& "User Id=testuser;Password=test623"
Dim stm As String = "SELECT VERSION()"
Dim version As String
Dim conn As MySqlConnection
Try
conn = New MySqlConnection(cs)
conn.Open()
Dim cmd As MySqlCommand = New MySqlCommand(stm, conn)
version = Convert.ToString(cmd.ExecuteScalar())
Console.WriteLine("MySQL version: {0}", version)
Catch ex As MySqlException
Console.WriteLine("Error: " & ex.ToString())
Finally
conn.Close()
End Try
Note :
Better to use parameters when you call database, like below
cmd.CommandText = "SELCECT nama_student FROM student where Id_student = #Id_student"
then you have to add the parameter as
cmd.Parameters.AddWithValue("Id_student", id )
How do I create a parameterized SQL query? Why Should I?
You can put it into DataSet
Dim conn As New MySqlConnection
Dim cmd As New MySqlCommand
conn.ConnectionString = "Server=localhost; user id=root; password=; database=aplikasi_store_testing;"
cmd.Connection = conn
conn.Open()
Dim id As Integer
cmd.CommandText = "SELECT nama_student FROM student where Id_student ='" & id & "'"
Dim da As New MySqlDataAdapter 'DataAdapter can be used to fill DataSet
Dim ds As New DataSet
da.SelectCommand = cmd
da.Fill(ds, "student") 'you can change student with the table name
From above command, your data will be stored in a DataSet.
Sample to use:
ds.Tables("student").Rows.Count 'Get the number of rows in the DataTable
ds.Tables("student").Rows(0).Item("nama_student").ToString 'Get first row of the nama_student field
You can check MSDN for further information:
DataSet: http://msdn.microsoft.com/en-us/library/system.data.dataset.aspx
DataTable: http://msdn.microsoft.com/en-sg/library/system.data.datatable.aspx
DataRow: http://msdn.microsoft.com/en-sg/library/system.data.datarow.aspx
Note:
As mentioned by #Joel Coehoorn, try to look at Command Parameter http://dev.mysql.com/doc/refman/5.0/es/connector-net-examples-mysqlparameter.html