How can i display multiple result in a series of textboxs? - mysql

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()

Related

How to put two fields of database to one combobox in vb.net?

Private Sub ForgotPasswordPage1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim table2 As New DataTable
Dim cmd As New MySqlCommand
Dim cmd1 As New MySqlCommand
Dim da As New MySqlDataAdapter
Dim da2 As New MySqlDataAdapter
Dim con = New MySqlConnection
con.ConnectionString = "server=127.0.0.1;userid=root;password=;database=pharma"
Try
'we open Connection
con.Open()
With cmd
.Connection = con
.CommandText = "SELECT `security_question_1` from pharma.account_admin where `u_name`='" & Login.u_name.Text & "';"
End With
'declare dt as new datatable
Dim dt As New DataTable
Dim dt2 As New DataTable
With sq
da.SelectCommand = cmd
'it fills the da values into dt
da.Fill(dt)
'dt provides the data surce of combobox
.DataSource = dt
'specify the what to display
.DisplayMember = "security_question_1"
'and the value
.ValueMember = "security_question_1"
End With
With cmd1
.Connection = con
.CommandText = "SELECT `security_question_2` from pharma.account_admin where `u_name`='" & Login.u_name.Text & "';"
End With
With sq
da.SelectCommand = cmd1
'it fills the da values into dt
da.Fill(dt)
'dt provides the data surce of combobox
.DataSource = dt
'specify the what to display
.DisplayMember = "security_question_2"
'and the value
.ValueMember = "security_question_2"
End With
Catch ex As Exception
Here's my code. I want to have security question 1 and security question 2, which are two different fields in my database to be inserted in a combobox.
The most important thing in my code is the use of Parameters. This change will help protect your database from malicious input. I also used an alias for security question 1 and 2. (the As Question in the select command.) Double check the data type of User Name. I guessed at varchar but it could be something else. Change the MySalDbType accordingly. Using DataTable.Load adds the records from the second query to the data table. Actually, I think the two select could be run as a Union. If you are only getting 2 questions back then I would select both questions in one query, use a datareader and add the values to the combo box manually. The Using/End Using statements ensure that objects that implement IDisosable can release their unmanaged resources as soon as posible.
Using cmd As New MySqlCommand
Using da As New MySqlDataAdapter
Using con = New MySqlConnection("server=127.0.0.1;userid=root;password=;database=pharma")
Try
With cmd
.Connection = con
.CommandType = CommandType.Text
.CommandText = "SELECT `security_question_1` As Question from pharma.account_admin where `u_name`= #UserName;"
.Parameters.Add("#UserName", MySqlDbType.VarChar).Value = Login.u_name.Text
End With
Using dt As New DataTable
da.SelectCommand = cmd
da.Fill(dt)
cmd.CommandText = "SELECT `security_question_2` As Question from pharma.account_admin where `u_name`= #UserName;"
con.Open()
Using dr As MySqlDataReader = cmd.ExecuteReader
dt.Load(dr)
con.Close()
ComboBox1.DataSource = dt
ComboBox1.DisplayMember = "Question"
ComboBox1.ValueMember = "Question"
End Using
End Using
Catch ex As Exception
End Try
End Using
End Using
End Using

Putting values from MySql query onto form in vb

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.

how to retrieve mysql data in vb.net?

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.

Select from mysql put into variable VB.NET

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

Convert MySQL DataSet to String

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()