VB.NET ComboBox SelectedIndex from MySQL - mysql

so I have done with show data form my database MySQL to ComboBox.. I'm using this code:
Private Sub Get_Product()
Connection()
Dim command As New MySqlCommand
Dim reader As MySqlDataReader
Dim query As String = "SELECT * FROM product, writer, publisher WHERE product.writer = writer.writer AND product.publisher = publisher.publisher AND code = " & throwCode & " ORDER BY code"
Dim queryWriter As String = "SELECT DISTINCT writer, writer_name FROM writer ORDER BY writer_name"
Dim dataAdapter As New MySqlDataAdapter(queryWriter, conn)
Dim dataSet As New DataSet
dataAdapter.Fill(dataSet, "writer")
Try
command = New MySqlCommand(query, conn)
reader = command.ExecuteReader
While reader.Read
TextBoxISBN.Text = reader("isbn")
TextBoxTitle.Text = reader("title")
TextBoxPage.Text = reader("page")
With (ComboBoxWriter)
.Items.Add("Select")
.DataSource = dataSet.Tables("writer")
.DisplayMember = "writer_name"
.ValueMember = "writer"
.SelectedIndex = 0 **// Give atention to this code**
End With
TextBoxYear.Text = reader("year")
TextBoxCategory.Text = reader("category")
TextBoxCallNumber.Text = reader("call_number")
TextBoxWeight.Text = reader("weight")
TextBoxPurchasePrice.Text = reader("purchase_price")
TextBoxSellingPrice.Text = reader("selling_price")
TextBoxDiscount.Text = reader("discount")
TextBoxDescription.Text = reader("description")
TextBoxTag.Text = reader("tag")
TextBoxPusatPenerbit.Text = reader("pusat_penerbit")
TextBoxMrican.Text = reader("mrican")
TextBoxPaingan.Text = reader("paingan")
If (Not IsDBNull(reader("picture"))) Then
Dim byteImage() As Byte = reader("picture")
Dim tempImage As New System.IO.MemoryStream(byteImage)
PictureBoxPicture.Image = Image.FromStream(tempImage)
End If
End While
conn.Close()
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub
In my code, I give some comment and bold to show you what I want to do.
So I have a writer code in product an writer code in writer (writer code named as writer), and I join the table to show writer name from writer code. But in ComboBox I want to show all of writer for editing but the default value is index of the writer that same like in product table.
1
2
If you see my first image that have a writer name Sutarjo Adisusilo but when I view it and want to use it as a default value in combobox that show A. Kardiyat Wiharyanto as indexvalue number 0..
I need help how to change the default value to be same like the picture number 1
Thanks

I've updated this answer to an easier method. Double check the column names though.
ComboBoxWriter.SelectedIndex = ComboBoxWriter.FindStringExact(reader("writer_name"))

Related

Is it possible to sort controls inside flowpanel without using .add() and .clear() by clicking id or name etc

Application Screen Shot
I tried searching all over from the internet if this kind of thing is possible but I didn't find any that solve my problem..
Here is my code for ... but this code keep on adding another usercontrols to my panel...
Sub sorbyID()
pnList.Controls.Clear()
connect()
Dim dr As MySqlDataReader
Dim str As String = "SELECT * FROM clientinfo order by id desc"
Dim cmd As New MySqlCommand(str, con)
dr = cmd.ExecuteReader
For i As Integer = 0 To 10
Dim c As Control1 = New Control1
If dr.Read Then
c.Dock = DockStyle.Top
c.lblID.Text = dr("ID")
c.lblName.Text = dr("lname") & ", " & dr("fName")
c.lblPlan.Text = dr("plan")
c.lblIPadd.Text = dr("ipAdd")
c.lblMac.Text = dr("Mac")
c.lblBrgy.Text = dr("brgy")
pnList.Controls.Add(c)
End If
Next
End Sub

How to read a value from mysql database?

I want to be able to read a value (in this case an Group ID). All the topics and tutorials I've watched/read take the data and put it into a textbox.
I don't want to put it in a textbox in this case; I want to grab the Group ID and then say:
If Group ID = 4 then login
Here is an image of the database.
Basically, but none of the tutorials I watch or the multiple forums. None of them take a a value and say if value = 4 then login or do something else.
If text = "1" Then
MysqlConn = New MySqlConnection
MysqlConn.ConnectionString =
"server='ip of server'.; username=; password=; database="
Dim READER As MySqlDataReader
Dim member_group_id As String
Try
MysqlConn.Open()
Dim Query As String
Query = "SELECT * FROM `core_members` where name='" & TextBox2.Text & "'"
Query = "SELECT * FROM `nexus_licensekeys` where lkey_key='" & TextBox1.Text & "'"
COMMAND = New MySqlCommand(Query, MysqlConn)
READER = COMMAND.ExecuteReader
Dim count As Integer
count = 0
While READER.Read
count = count + 1
End While
Here is what I have so far. I'm kind of new implementing mysql data with visual basic and only recently started to get into it. I'm not sure what comes next or how to even start with reading the group id etc.
As I said any help from here on out would be highly appreciated of how to read the group id and say if this group id = this number then do this or that. I'm sure you get the idea.
I divided the code into UI Sub, and Data Access Function that can return data to the UI. Your Event procedure code should be rather brief and the functions should have a single purpose.
Keep your database objects local to the method. This way you can have better control. The Using...End Using blocks ensure that your database objects are closed and disposed even if there is an error.
I leave it to you to add validation code. Checking for empty TextBox or no return of records.
I hope this serves as a quick introduction to using ADO.net. The take away is:
Use Parameters
Make sure connections are closed. (Using blocks)
Private ConnString As String = "server=ip of server; username=; password=; database="
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim GroupID As String = GetGroupID(TextBox1.Text)
If GroupID = "4" Then
'your code here
End If
Dim LocalTable As DataTable = GetLicenseKeysData(TextBox1.Text)
'Get the count
Dim RowCount As Integer = LocalTable.Rows.Count
'Display the data
DataGridView1.DataSource = LocalTable
End Sub
Private Function GetGroupID(InputName As String) As String
'Are you sure member_group_id is a String? Sure looks like it should be an Integer
Dim member_group_id As String = ""
'You can pass the connection string directly to the constructor of the connection
Using MysqlConn As New MySqlConnection(ConnString)
'If you only need the value of one field Select just the field not *
'ALWAYS use parameters. See comment by #djv concerning drop table
Using cmd As New MySqlCommand("SELECT g_id FROM core_members where name= #Name")
'The parameters are interperted by the server as a value and not executable code
'so even if a malicious user entered "drop table" it would not be executed.
cmd.Parameters.Add("#Name", MySqlDbType.VarChar).Value = InputName
MysqlConn.Open()
'ExecuteScalar returns the first column of the first row of the result set
member_group_id = cmd.ExecuteScalar.ToString
End Using
End Using
Return member_group_id
End Function
Private Function GetLicenseKeysData(InputName As String) As DataTable
Dim dt As New DataTable
Using cn As New MySqlConnection(ConnString)
Using cmd As New MySqlCommand("SELECT * FROM `nexus_licensekeys` where lkey_key= #Name;", cn)
cmd.Parameters.Add("#Name", MySqlDbType.VarChar).Value = InputName
cn.Open()
dt.Load(cmd.ExecuteReader())
End Using
End Using
Return dt
End Function

Hide part of String.Format within a ComboBox.Item

Is it possible to hide part of String.Format?
This my code:
'Select Product'
Try
MysqlConn.Close()
MysqlConn.Open()
Dim Query As String
Query = "select id, name,id_maker, id_types from product ORDER BY name ASC"
COMMAND = New MySqlCommand(Query, MysqlConn)
READER = COMMAND.ExecuteReader
While READER.Read
Dim sName = READER.GetString("name")
Dim sMaker = READER.GetString("id_maker")
Dim sTypes = READER.GetString("id_types")
Dim sId = READER.GetString("id")
'ComboBox1.Items.Add(sName)'
ComboBox1.Items.Add(String.Format("{0}|{1}|{2}|{3}", sName, sMaker, sTypes, sId))
End While
Catch ex As Exception
MessageBox.Show(ex.Message)
Finally
MysqlConn.Dispose()
End Try
'Select Product'
I want to hide {3} which is sId in the ComboBox, because later I need to use a query where the ComboBox1.Text is used and the id is necessary.
Maybe you could change the way you are assigning the data to the ComboBox.
First thing to do is change the query and use CONCAT:
SELECT id, CONCAT(name,'|',id_maker,'|',id_types) AS value FROM product ORDER BY name ASC
I would also implement Using:
Managed resources are disposed of by the .NET Framework garbage collector (GC) without any extra coding on your part. You do not need a Using block for managed resources. However, you can still use a Using block to force the disposal of a managed resource instead of waiting for the garbage collector.
You also don't need the READER. Instead load the data into a DataTable and assign that to the .DataSource property on the ComboBox.
Your code would look something like this:
Using con As New MySqlConnection(connectionString)
cmd As New MySqlCommand("SELECT id, CONCAT(name,'|',id_maker,'|',id_types) AS value FROM product ORDER BY name ASC", con)
con.Open()
Dim dt As New DataTable
dt.Load(cmd.ExecuteReader())
ComboBox1.DataSource = dt
ComboBox1.DisplayMember = "value"
ComboBox1.ValueMember = "id"
End Using
You can now get the id with this bit of code:
ComboBox1.SelectedValue.ToString()
And you can get the text with this bit of code:
ComboBox1.Text
Ok i use now that and works
Dim connetionString As String = Nothing
Dim connection As MySqlConnection
Dim command As MySqlCommand
Dim adapter As New MySqlDataAdapter()
Dim ds As New DataSet()
Dim i As Integer = 0
Dim sql As String = Nothing
'connetionString = "Data Source=ServerName;Initial Catalog=databasename;User ID=userid;Password=yourpassword"
'sql = "select id,name from product"
sql = "SELECT id, CONCAT(name,' | ',id_maker,' | ',id_types) AS value FROM product ORDER BY name ASC"
'connection = New MySqlConnection(connetionString)
connection = New MySqlConnection(ConfigurationManager.ConnectionStrings("xCollectibles.My.MySettings.xcollectiblesConnectionString").ToString)
Try
connection.Open()
command = New MySqlCommand(sql, connection)
adapter.SelectCommand = command
adapter.Fill(ds)
adapter.Dispose()
command.Dispose()
connection.Close()
ComboBox1.DataSource = ds.Tables(0)
ComboBox1.ValueMember = "id"
ComboBox1.DisplayMember = "value"
Catch ex As Exception
MessageBox.Show("Can not open connection ! ")
End Try
Thanks you..

Strange issue when retrieving data from my database

When reading data from a table, one of my numerical values returns as a -1, rather than a value given by the user.
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Try
Dim conn As New MySqlConnection("server=localhost;user=root;database=new_project;port=3306;password=********;")
conn.Open()
Dim command As New MySqlCommand("SELECT * FROM students;", conn)
Dim dataSet As New DataSet()
Dim dataAdapter As New MySqlDataAdapter()
dataAdapter.SelectCommand = command
dataAdapter.Fill(dataSet, "students")
Dim dataTable As DataTable = dataSet.Tables("students")
For x As Integer = 0 To dataTable.Rows.Count - 1
Dim newStudent As New Student
newStudent.intIDNum = dataTable.Rows(x).Item("idNumber")
newStudent.strFirstName = dataTable.Rows(x).Item("firstName")
newStudent.strLastName = dataTable.Rows(x).Item("lastName")
newStudent.chrGender = dataTable.Rows(x).Item("gender")
newStudent.dateDOB = dataTable.Rows(x).Item("dateOfBirth")
newStudent.intAge = CInt(Today.Year - newStudent.dateDOB.Year)
newStudent.intYearGroup = dataTable.Rows(x).Item("yearGroup")
newStudent.intSkillLevel = dataTable.Rows(x).Item("skillLevel")
studentList.Add(newStudent)
lstStudents.Items.Add(newStudent.nameConcat(newStudent.strFirstName, newStudent.strLastName))
Next
conn.Close()
Catch ex As Exception
MsgBox("Error: " & ex.ToString())
End Try
End Sub
When reading the value "skillLevel", it should read between 1 and 4, but instead it reads a -1. This hasn't occurred with any data added directly to the database through MySQL, but when using the method in my program, it seems this issue appears.
Dim newStudent As New Student
newStudent.strFirstName = txtForename.Text
newStudent.strLastName = txtSurname.Text
newStudent.dateDOB = dtpStudentDOB.Value
If rdbMale.Checked = True Then
newStudent.chrGender = "M"
Else
newStudent.chrGender = "F"
End If
newStudent.intAge = newStudent.calcAge(dtpStudentDOB.Value)
newStudent.intSkillLevel = cmbSkillLevel.SelectedItem
newStudent.intYearGroup = cmbYearGroup.SelectedItem
newStudent.intIDNum = CInt(txtStudentID.Text)
Dim conn As New MySqlConnection("server=localhost;user=root;database=new_project;port=3306;password=********;")
Try
conn.Open()
Dim command As MySqlCommand = New MySqlCommand
command.Connection = conn
command.CommandText = "INSERT INTO Students VALUES(#IdNumber, #firstName, #lastName, #gender, #dateOfBirth, #yearGroup, #skillLevel);"
command.Prepare()
command.Parameters.AddWithValue("#IdNumber", newStudent.intIDNum)
command.Parameters.AddWithValue("#firstName", newStudent.strFirstName)
command.Parameters.AddWithValue("#lastName", newStudent.strLastName)
command.Parameters.AddWithValue("#gender", newStudent.chrGender)
command.Parameters.AddWithValue("#dateOfBirth", newStudent.dateDOB)
command.Parameters.AddWithValue("#yearGroup", newStudent.intYearGroup)
command.Parameters.AddWithValue("#skillLevel", newStudent.intSkillLevel)
command.ExecuteNonQuery()
Catch ex As Exception
MsgBox("Error: " & ex.ToString())
Finally
conn.Close()
End Try
This is the section of code where a student is added to the system. What exactly could be causing this error to appear?
For reference, the main form I add in the data uses an enumeration:
Private Enum SkillLevels As Integer
One = 1
Two = 2
Three = 3
Four = 4
End Enum
And then I link it to the dropdown: cmbSkillLevel.DataSource = System.Enum.GetValues(GetType(SkillLevels))
The obvious possibility is that no selection was made in the SKILL-LEVEL dropdown and that is why you are getting -1, you can check if a selection has been made in the dropdown by ensuring it is not -1
For example your skills drop down may look like this
<select id="SkillLevel">
<option value="0" selected="selected">Please Select</option>
<option value="1">Low</option>
<option value="2">Medium</option>
<option value="3">High</option>
<option value="4">Very-High</option>
</select>
If no selection is made the default value returned will be zero (0)
And you can check for valid values by looking for returned values > 0
As far as I can remember if you omit the selected="selected" then the returned value will be -1
I think I had misinterpreted #tolanj's comments. It appears that when loading my data, the system couldn't read my skillLevel column. To fix it, I changed newStudent.intSkillLevel = row.Item("skillLevel") to newStudent.intSkillLevel = SByte.Parse(row.Item("skillLevel")).

Populate a combobox with tables of a database in VB

I want to populate my combobox with the name of the tables of my database "converter" that has 3 tables. I'm using the following code:
conn.ConnectionString = "server=localhost;userid=root;password=NewPass;database=converter"
Try
conn.Open()
command.Connection = conn
command.CommandText = "SHOW TABLES"
adapter = New MySqlDataAdapter(command.CommandText, conn)
adapter.SelectCommand = command
adapter.Fill(dt)
ComboBox1.DataSource = dt
ComboBox1.DisplayMember = "column_name"
ComboBox1.ValueMember = "column_name"
Catch ex As Exception
End Try
This code populates 3 items inside the combobox (which is the same as the number of tables inside my database). But what shows is this:
How can I populate my combobox with the exact name of my tables and not like this? I tried putting .toString on my dt but it's no good.
Can anyone help me? Thank you in advance. :)
EDIT
I used Steve's method and I was able to get the values. I used this code.
conn.ConnectionString = "server=localhost;userid=root;password=NewPass;database=converter"
Try
conn.Open()
command.Connection = conn
command.CommandText = "SHOW TABLES"
adapter = New MySqlDataAdapter(command.CommandText, conn)
adapter.SelectCommand = command
dt = conn.GetSchema("TABLES")
adapter.Fill(dt)
ComboBox1.DataSource = dt
ComboBox1.DisplayMember = "table_name"
ComboBox1.ValueMember = "table_name"
Catch ex As Exception
End Try
But now it shows 3 additional empty values. refer to this image:
If I am right the correct values to pass to your combobox DisplayMember and ValueMember is
Tables_in_converter
In other words, the name of the only column returned by your command is built automatically using the fixed text "Tables_in_" followed by the <databasename>
If you want to use a different approach you could call the GetSchema method of the connection object in this way
using cnn = new MySqlConnection(...)
cnn.Open()
Dim dt = cnn.GetSchema("TABLES")
ComboBox1.DataSource = dt
ComboBox1.DisplayMember = "table_name"
ComboBox1.ValueMember = "table_name"
End Using