How to upload what user selected in combo box to MYSQL database - mysql

ok so in vb i have a combo box that i populated with selections for the user to choose from. i want what ever the user selects to upload to the db. I have other things in my form uploading to my db but i cant get what the user selected to upload?? how would i do this.
here is my code for the other upload stuff that i did.
Try
Dim cmd2 As New MySqlCommand
Dim insertStatment As String = "INSERT INTO comment (name,comment) VALUES
(#name,#comment)"
cmd2 = New MySqlCommand(insertStatment, db_con)
cmd2.Parameters.AddWithValue("#name", txtname.Text)
cmd2.Parameters.AddWithValue("#comment", richtxtcomment.Text)
cmd2.ExecuteNonQuery()
MessageBox.Show("Thank your for your comment")
Catch ex As Exception
MessageBox.Show("bad")

In general, for a ComboBox you'll need to do something like this:
cmd2.Parameters.AddWithValue("#whatever", combobox.SelectedValue);

Related

I'm trying to retrieve the whole 1st row inside phpmyadmin: USERNAME; PASSWORD; EMAIL, and make it the value of three label.text in visual basic [duplicate]

I have a MySQL Database called "business elements", which has 4 columns in it (ID,Username,Password & Level). I want to check if a specific username's (given by Usernametextbox.text) "Level" is Admin, Manager or User. These are the 3 values that I want all of my users to have(They are all in the column Level). My connectionstring is "server=localhost;user id=root;password=;database=business elements" and my table is users
Basically I want to check the value of a column with a given user.
All of this is in Visual Basic.
Someone please Help..
Here's how to get the level for a particular username. It should a good starting point for lots of other functions. I haven't tested it, but it should be pretty close. (Userlevel should match the data type for "level"in the table.)
Dim conn as MySqlConnection
Dim cmd As MySqlCommand
conn = New MySqlConnection("server=localhost;user id=root;password=;database=business elements;")
conn.Open()
cmd = New MySqlCommand("SELECT level FROM users WHERE Username=#username LIMIT 1", conn)
cmd.Parameters.AddWithValue("#username", Usernametextbox.Text)
userLevel = cmd.ExecuteScalar()

Reader if and else statement

I am practicing programming in visual basic
I have 2 forms in visual basic.
The first form has a command button that will show the input data from the 2nd form
The second form has a textbox where I need to input a data and save it.
The data that I input in the 2nd form stores it in MySQL
the 1st form has a command button named "Show my Grade" and if I click that I want to display the Form 2 and show me the grade. It does work however if I didn't input any grade and then I click the "show my grade" button it crashes I don't know the error. I tried using the code "if READER.HasRows Then:
but still it won't work
also tried if READER.Read then:
else MessageBox.Show("there is no grade input at the moment")
Please help.
This is my current code in the command button in form 1.
Me.Visible = False
Form2.Show()
MySqlConn = New MySqlConnection
MySqlConn.ConnectionString = "server=localhost;userid=root;password=qwerty;database=ssg"
Dim COMMAND As New MySqlCommand
Dim READER As MySqlDataReader
MySqlConn.Open()
COMMAND.Connection = MySqlConn
COMMAND.CommandText = "select grade from gradetable"
READER = COMMAND.ExecuteReader
Form2.TextBox1.Text = READER("grade")
End Sub
It works as long as I input a grade first, however if I didn't input any grade it crashes.
If I click the "Show my grade" button in form 1 without inputting a grade in form 2 I would like to just display a message saying "You still have no grade at the moment"
Please help.
The Using statement with a matching End Using. This is in place of Dim. It ensures that your objects are properly closed and disposed of even if there is an error. This is especially important for connections which should be closed as soon as possible.
I used the constructor of the connection to pass in the connection string. Setting the property is fine. This just saves a line of code.
Same idea with the command constructor. It can take the command text and the connection. Saves a little typing.
You had the right idea; using .HasRows. You just needed to add the READER.Read to get to the first record as explained in comments by jmcilhinney.
Private Sub GetGrade()
Using MySqlConn As New
MySqlConnection("server=localhost; userid=root; password=qwerty; database=ssg")
Using COMMAND As New MySqlCommand("select grade from gradetable", MySqlConn)
MySqlConn.Open()
Using READER As MySqlDataReader = COMMAND.ExecuteReader
If READER.HasRows Then
READER.Read()
Form2.TextBox1.Text = READER("grade")
Else
MessageBox.Show("Sorry, no grade yet.")
End If
End Using
End Using
End Using
End Sub

why does my datagridview not show any results?

I'm new to vb.net and am trying to display results from a mysql server in my datagridview. Can anyone maybe point out whats wrong with my code? Thanks in advance.
Leaving out my connection string for safety, but it does connect. And if I put a breakpoint in the code it does fill the dataset with the correct data
Dim con As New MySqlConnection("server=;user id=;password=;database=")
con.Open()
Dim adp As New MySqlDataAdapter("Select CONCAT(FirstName,' ',Surname) as Name, LeaveDaysAvailable as 'Leave Days Available' from leave_database.Employees;", con)
Dim ds As New DataSet()
adp.Fill(ds)
dgvMain.DataSource = ds
con.Close()
This is the result. No errors....
this is the result if i run the same query in mysql workbench
A DataSet doesn't contain data directly. It has a Tables collection that contains DataTable objects and each of those contains data. If you assign a DataSet to the DataSource property of your DataGridView, you need to also set the DataMember property to tell it which DataTable to get the data from.
The problem is, you haven't named your DataTable so you have nothing to assign to the DataMember. This line:
adp.Fill(ds)
actually creates a DataTable, populates it and adds it to the Tables collection. It has no name though, so you have no name to set the DataMember to. You would need to name the DataTable first, e.g.
adp.Fill(ds, "MyDataTable")
and then you can set the DataMember:
dgvMain.DataMember = "MyDataTable"
dgvMain.DataSource = ds
The alternative is to just bind the DataTable instead of the DataSet, in which case you don't need a name:
dgvMain.DataSource = ds.Tables(0)
The thing is though, why create a DataSet in the first place if you're just going to use one DataTable? Simply create a DataTable. You can then pass that DataTable to the Fill call and assign it to the DataSource property.
dataGridView1.AutoGenerateColumns = true;
Else You have to add columns to data grid you can do it using designer file or from code like
DataGridTextColumn textColumn = new DataGridTextColumn();
dataColumn.Header = "First Name";
dataColumn.Binding = new Binding("Name");
dataGrid.Columns.Add(textColumn);
DataGridTextColumn textColumn = new DataGridTextColumn();
dataColumn.Header = "Leave day available";
dataColumn.Binding = new Binding("LeaveDaysAvailable");
dataGrid.Columns.Add(textColumn);
Using designer this will may help you https://msdn.microsoft.com/en-us/library/dyyesckz(v=vs.100).aspx

Populating text boxes via Combobox and SQL database table

I am trying to populate a set of textboxes from a combobox on a form. The combo box is populated using a dataset when the form loads. When this is loaded it needs to show only one entry per unit number in the kitcombobox (which is a unit number for a kit with multiple pieces of equipment in it) but display the multiple pieces of equipment's information in the different text boxes when the unit number is selected via the kitcombobox. What approach should I take towards this? I'm really lost and this is all I have so far :(
Private Sub ckunit()
Dim ds As New DataSet
Dim cs As String = My.Settings.MacroQualityConnectionString
Dim kitcombobox As String = "SELECT DISTINCT Unit_Number, Status FROM Calibrated_Equipment WHERE CHARINDEX('CK', Unit_Number) > 0 AND Status='" & ckstatuscombbx.Text & "'"
Dim sqlconnect As New SqlConnection(cs)
Dim da As New SqlDataAdapter(kitcombobox, sqlconnect)
sqlconnect.Open()
da.Fill(ds, "Calibrated_Equipment")
sqlconnect.Close()
kitcombbx.DataSource = ds.Tables(0)
End Sub
Assuming you are using WinForms, I think the key will be adding an event handler for the SelectionChangedCommitted event on kitcombbx.
You can then checked the properties on the combobox to check what is selected and run another query to pull equipment information for that kit. It'd probably look something like this:
Private Sub kitcombbx_SelectionChangeCommitted(sender As Object, e As EventArgs) _
Handles kitcombbx.SelectionChangeCommitted
Dim kit = kitcombbx.SelectedItem.ToString()
Dim kitEquipment = FetchKitEquipmentInformation(kit)
PopulateEquipmentInformation(kitEquipment)
End Sub
The way you're currently constructing your query (by concatenating string parameters directly from user input) results in bad performance for most database systems, and moreover, is a huge security vulnerability. Look up SQL injection for more detail (or read these two questions).
Better DB code would probably look something like this:
Dim query = New StringBuilder()
query.AppendLine("SELECT DISTINCT Unit_Number, Status ")
query.AppendLine("FROM Calibrated_Equipment ")
query.AppendLine("WHERE CHARINDEX('CK', Unit_Number) > 0 ")
query.AppendLine(" AND Status = #STATUS ")
Dim connection As New SqlConnection(My.Settings.MacroQualityConnectionString)
Dim command As New SqlCommand(query, connection);
command.Parameters.Add("#STATUS", ckstatuscombbx.Text);
Dim da As New SqlDataAdapter(kitcombobox, sqlconnect)
'And so on...
Your question is a bit broad (and therefore, likely off-topic for StackOverflow), see How to Ask.

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.