Visual Basic combobox with mysql - 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.

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 properly insert data from dynamic DataTable to MySQL?

Actually i have a method where the user is able to choose data from excel file and choose between some headers name which are set statically and are equals to MySQL columns name in a table, then i just collect that data in a DataTable.
Now i would be able to store that data in MySQL database but which would be the best method to insert that data from a DataTable into MySQL? The rows of DataTable could contain more than 50.000/100.000 items.
That's how the DataTable could look's like (The header is choosen dynamically by the user from a combobox)
So in this Insert method which would be the best way to add that data? Should i just loop throw dataTable items and make multiple inserts?
Private Sub InsertDB()
Dim dataTable = ExcelToDT(TxtUpload.Text, ColumnHeader(PanelColumns, Column))
Dim dbCon As New Odbc.OdbcConnection
Dim conStr As String = "DSN=WRRPT"
dbCon.ConnectionString = conStr
dbCon.Open()
Dim cmd As New Odbc.OdbcCommand
With cmd
.CommandText = "INSERT INTO ? VALUES ?"
.Connection = dbCon
End With
Dim reader As Odbc.OdbcDataReader
reader = cmd.ExecuteReader
dbCon.Close()
reader = Nothing
cmd = Nothing
End Sub

Combobox not populating with mysql data

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.

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

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.