Combobox not populating with mysql data - mysql

I have a combobox which needs to be populated with data from mysql table, here is my code, I cannot see what's wrong with it? It doesn't throw any error, just comes blank when I run the program.
Dim StrSql As String = "SELECT PaymentID FROM payment_details"
Dim cmd As New MySqlCommand(StrSql, objconnection)
Dim da As MySqlDataAdapter = New MySqlDataAdapter(cmd)
Dim dt As New DataTable("Payment_details")
da.Fill(dt)
If dt.Rows.Count > 0 Then
cbxPaymentID.DisplayMember = "PaymentID" 'What is displayed
cbxPaymentID.ValueMember = "PaymentID"
cbxPaymentID.DataSource = dt
End If

The ComboBox has Items. What you need to do is create an new Item for each row in your datatable.
something like....
If dt.Rows.Count > 0 Then
For r = 0 to dt.Row.Count - 1
cbxPaymentID.Items.Add(new ListItem(dt.Row(r).Item("PaymentID"))
Next
End If
The above is based on standard ASP toolkit ComboBox. Other versions may need different code.

Related

Remove duplicated rows on gridview

Hi There I have the following grid view developed with vb.net and html
This is the code to fill the gridview
Private Sub BindGrid()
Dim modelo As String = txtModelo.Text
Dim linha As String = txtLinha.Text
Dim data As String = txtData.Text
Dim constr As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Using con As New MySqlConnection(constr)
Using cmd As New MySqlCommand("SELECT * FROM tempo where timermodelo = #timermodelo and timerlinha = #timerlinha and timerdata = #timerdata")
Using sda As New MySqlDataAdapter()
cmd.Parameters.AddWithValue("#timermodelo", modelo)
cmd.Parameters.AddWithValue("#timerlinha", linha)
cmd.Parameters.AddWithValue("#timerdata", data)
cmd.Connection = con
sda.SelectCommand = cmd
Using dt As New DataTable()
sda.Fill(dt)
GridView1.DataSource = dt
GridView1.DataBind()
End Using
End Using
End Using
End Using
End Sub
My question is, is it possible to remove the duplicated rows on the column Operador, Posto, Linha and Modelo?
And also how can I form the date to show 'dd-mm-yyyy'?
Actually, I want to merge these values, if they are the same, show just one time
Sorry if it is difficult to understand, if more information is needed let me know
Thanks in advance
Remove duplicates : Why don't you use SqlStatement that return rows without duplication, depending on your Database type 'you did not mention', if this is achievable in your SqlStatement then this problem is solved.
Format Date in GridView1 cell : Use GridView1_CellFormatting event [ Where your Date ColumnIndex = 4] as follows
Private Sub GridView1_CellFormatting(sender As Object, e As System.Windows.Forms.DataGridViewCellFormattingEventArgs) Handles GridView1.CellFormatting
If Not IsNothing(e.Value) Then
If e.ColumnIndex().ToString = 4 And IsDate(e.Value) Then
e.Value = Format(e.Value, "dd-mm-yyyy")
End If
End If
End Sub
I want to merge these values, if they are the same
What values are we talking about here ?

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

Need help filling datagrid from MySQL

I have made a table in mysql with attributes Product code,Quantity,company,price. And I have created a datagridview in vb 2012 and I want to take input from the form and then display the results in a datagridview. I also want to display price from the table I have created in mysql. But, i'm not able to do so.
Here is the code of my program. plz help me
Dim row As Integer = DataGridView1.Rows.Add()
Dim connection As String
Dim command As String
Dim command2 As String
command2 = "select Company from Stock WHERE Product_Code =('" + TextBox1.Text + "');"
connection = "Data Source=localhost; Database=Entry; User Id=root; Password=;"
command = "select Price from Stock WHERE Product_Code =('" + TextBox1.Text + "');"
Dim con As New MySqlConnection(connection)
Dim cmd As New MySqlCommand(command)
Dim data As DataTable
Dim adp As New MySqlDataAdapter
Dim data2 As DataTable
Dim adp2 As New MySqlDataAdapter
DataGridView1.Rows.Item(row).Cells(0).Value = TextBox1.Text
DataGridView1.Rows.Item(row).Cells(2).Value = TextBox2.Text
Try
adp = New MySqlDataAdapter(command, connection)
adp2 = New MySqlDataAdapter(command2, connection)
data = New DataTable
data2 = New DataTable
adp.Fill(data)
adp2.Fill(data2)
DataGridView1.Rows.Item(row).Cells(1).Value = data
DataGridView1.Rows.Item(row).Cells(3).Value = data
Catch ex As Exception
MessageBox.Show("Error")
End Try
You should be able to find examples of how to do this all over SO (stack overflow). But to give you a helping hand, here are the things you need to research:
First, you should parameterize your SQL to prevent injection and readability: http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlcommand.parameters.aspx
Second, you dont add rows to a datagrid, you set the datasource to something that implements IList: http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridview.datasource.aspx Then you add items to your list if you need to. If you just want to display the rows from your table, you can set the datasource to your datatable (DATA).

Visual Basic combobox with mysql

I have this VB code that populates a combobox with data from a MySQL table
Dim sStmt As String = "SELECT sequence, name FROM product_categories"
Dim cmd As New MySqlCommand(sStmt, sqlcon)
Dim da As MySqlDataAdapter = New MySqlDataAdapter(cmd)
Dim dt As New DataTable("product_categories")
da.Fill(dt)
If dt.Rows.Count > 0 Then
category.DataSource = dt
category.DisplayMember = "name" 'What is displayed
category.ValueMember = "sequence" 'The ID of the row
End If
what would be the easiest way to add an option at the top of the list above all the data from the MySQL table to say none also with a value of none?
Add a row with a 'sequence' of 0, named 'none'
Dim newRow = dt.NewRow()
newRow("sequence") = 0
newRow("name") = "none"
dt.Rows.Add(newRow)
Of course you will want to handle this row specially, and not insert it if you decide to write back to the database.
See "Inserting a New Record into an Untyped Dataset" in http://msdn.microsoft.com/en-us/library/5ycd1034(v=vs.80).aspx for more info.

put the contents of the database to a combobox

I am using vb.net and mysql as the database.
I have created a field containing the abbreviations of words. My vb.net form provides a combobox which I want to connect to the field abbreviation. I want to configure the combobox so that when clicked, all the acronyms that exist in the database appear in the combobox.
Any suggestions on how I can get started?
Something like this:
Dim conn As New SqlConnection(connString)
Dim strSQL As String = "SELECT * FROM abbreviations"
Dim da As New SqlDataAdapter(strSQL, conn)
Dim ds As New DataSet
da.Fill(ds, "abbreviations")
With ComboBox1
.DataSource = ds.Tables("abbreviations")
.DisplayMember = "abbreviation_name"
.ValueMember = "abbreviation_id"
.SelectedIndex = 0
End With
Should give you the right idea to get started.