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.
Related
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
So here's my problem, I have a combo box that I want to fill with data from a table in MySql on load. I used this code for doing that:
Dim strSQL As String = "SELECT containers FROM tbl_items_containers"
Dim da As New MySqlDataAdapter(strSQL, SQLConnection)
Dim cmd As New MySqlCommand
Dim ds As New DataSet
SQLConnection.ConnectionString = ServerString
cmd.Connection = SQLConnection
da.Fill(ds, "tbl_items_containers")
With cb_container_type
.DataSource = ds.Tables("tbl_items_containers")
.DisplayMember = "containers"
.ValueMember = "containers"
.SelectedIndex = 0
End With
The error shown whenever i run it is:
An unhandled exception of type 'System.ArgumentOutOfRangeException' occurred in System.Windows.Forms.dll
Additional information: InvalidArgument=Value of '0' is not valid for 'SelectedIndex'.
Now here's the thing, I tried getting data from another table in the same database and the code worked. My combo box was populated with data. I assumed that the problem is in the database, But I'm really lost here. I have no idea what to do. Any Ideas as to what may have cause the error? Thanks in advance.
I am using vb.net to try and fill a combobox with values from my MySQL database which works fine by inserting the code below:
Dim strSQL As String = "SELECT distinct Department FROM users"
Dim da As New MySqlDataAdapter(strSQL, connectionString)
Dim ds As New DataSet
da.Fill(ds, "users")
With Find_Dep
.Items.Add("Select")
.DisplayMember = "Department"
.ValueMember = "Department"
.DataSource = ds.Tables("users")
.SelectedIndex = 0
End With
I have three two queries which rely on TextBoxes to search my DataGrid which also work fine. I am trying to use the current code to allow my ComboBox to grab the values from MySQL and filter them specific values from the datagrid:
Dim DV As New DataView(dbDataSet1)
DV.RowFilter = String.Format("[Department] Like '%" & Find_Dep.Text & "%'")
DataGridView1.DataSource = DV
This code above works fine if i manually add the pre-defined values into the ComboBox without populating the ComboBox from MySQL. So there is a conflict between the two and i cant figure out what it is as i am new to VB.
Retrieving error message: .Cannot Find Column[Department]' upon adding both chunks of code above. Whats the conflict?
Any help would be greatly appreciated :)
Try to fix your quotes. Opened and closed.Number of quotes must be even.
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.
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).