I am creating a program in VB.NET which uses an online MySQL database to retrieve certain data. I have now succeeded in connecting and getting some basic stuff out of it. Now, what I want to do is that when an user presses a button it has to update the list. What happens is that the stuff that is already in the database also gets resent and so the list just doubles himself, although it adds the new database value.
How can I make sure that it only adds new values to the list, instead of adding all values from the list again? I have read that you can use the Preserve keyword with arrays, though this isn't an array and neither have I figured out how to convert my data into an array.
Private Sub generateList()
DB.writeQuery("SELECT snacks.ID, snacks.naam, snacks.baktijd FROM snacks")
While DB.DR.Read
ListBox1.Items.Add(DB.DR.Item("naam"))
End While
DB.closeConnection()
End Sub
This is the piece of code I use to generate the list, I reuse this code to refresh the list. As you can see, it uses my own written MySQL class. I know it makes an connection so there is nothing wrong with that.
Any help would be appreciated.
What you will want to do is to clear the ListBox before you reload the list. You can do this like so:
ListBox1.Items.Clear()
Then when you reload the list, the items will not be duplicated.
Looks like you need to clear your listbox before adding the items.
ListBox1.Items.Clear()
Try this my friend:
If Not listBox1.Items.Contains(DB.DR.Item("naam")) Then
ListBox1.Items.Add(DB.DR.Item("naam"))
else
'this item already exists.
end if
Related
Im stucked. I am trying to insert data from the grid to the database.But I'm stucked.
Slugsie's comment is pertinent, but needs to go a little further:
You're actually clearing the entire parameters collection before you call the procedure. If it were a house, you went to all the effort to build the whole thing, then at the last minute you demolish it and show the customer a cleared lot
Don't clear the Parameters collection at all. Add all the parameters to it OUTSIDE the loop, then inside the loop, set the values:
'general pattern
cmd.Parameters.AddWithValue("param1", "...")
cmd.Parameters.AddWithValue("param2", "...")
cmd.Parameters.AddWithValue("param3", "...")
cmd.Parameters.AddWithValue("param4", "...")
For Each something In somethingElse
cmd.Parameters("param1").Value = something.Whatever1
cmd.Parameters("param2").Value = something.Whatever2
cmd.Parameters("param3").Value = something.Whatever3
cmd.Parameters("param4").Value = something.Whatever4
cmd.Execute...
Next something
That's the pattern you should be adopting
Though jmc also makes a useful comment; you could make your life a lot easier by
adding a DataSet type file to your project
open it, right click the surface, choose Add TableAdapter
when configuring the wizard for the adapter, say you want to use stored procedures for your select, insert, update etc
wire the procedures up to the relevant table columns
Now in code you can save data just by a single line or two:
Dim t as New WhateverTableAdapter
t.Update(yourWhateverDatatable) 'the WhateverDataTable is bound to a grid, the user has edited the rows in it by changing the grid
That Update() call will perform any necessary use of the Insert/Update/Delete procedures depending on if the datarow in the table is an Added, Modified or Deleted state
Forgive me if this has already been asked- I can’t seem to find a well written answer.
I am developing a small application for personal use.
Essentially what I have is two forms. Form 1 is a master view of all my contacts listed on a data grid view. Form 2 will be loaded on the cell/row double click of a particular record in order to edit it’s details.
My question is, what is the best practice/method for achieving this? I have seen many different methods.
Should I:
Pass only the primary key of the selected row then populate the fields on form 2 load
Pass all fields as a variable within a class then populate form 2 from that
Maybe I’m headed in the complete wrong direction though.
I have tried both ways, but wondering what the best method is for scalability.
My personal preference would be to pass a datarow into the opening argument of the form (rather than the PK / all the variables). You can then use the datarow inside your Form2 to bind to your controls or set their values, whichever you think is appropriate.
There are some useful examples on working with a datarow if you're unsure, alternatively you can also check out Microsoft Docs.
Public Sub New(ByVal row As DataRow)
InitializeComponent()
' your code for working with row here
End Sub
Edit:
In terms of "Binding" vs "Setting", you can either have your controls linked to your data to be two way (as you edit the data in a control at run time you alter the data in your database) or you can just set the values of the controls.
E.G. TextBox1.Text = row(0)("ColumnName")
You can find more on data binding on the Microsoft Docs page
I have a HTML multiple select being used which caused no issue until a selection with a comma was entered. When saving / loading the values are split by ',' into a list. Therefore causing an issue
I have tried to find a way of possibly changing the character that is being used to split the values when the form is posted but came to a dead end.
Would be very grateful if someone has any insight into this.
Thanks in advance.
---Update with Code---
The control is created dynamically
Dim SelectName1 As New HtmlSelect
SelectName1.ID = "SelectName" & id
SelectName1.Name = "SelectName"
SelectName1.Multiple = True
and filled by looping though the values.
For Each value As String In Request.Form(idToFind).Split(",")
If Not IsDBNull(SelectName.Items.FindByValue(value)) Then....
I cannot add any more code than that, Apologies
After updating the question by add code, I think the available options are limited:
Create an extra Javascript method, that stores the selected values in a json object and store it into a hidden input field. I'm not an expert in asp.net, though I'm sure there exists a method to parse json objects from a string.
Create an extra javascript method, that concatinating all values to a String and store it into a hidden input field. With creation of a single string, you can define your own delimiter.
After that, add this extra Javascript mehod to the event onSubmit. Then submit the form and read the value from hidden input field.
P.S. In my eyes the second idea is more simple to create, than the first one. And maybe there are better ways.
I have a visual basic program with a datagridview that is bound to a mysql database upon loading... I also have fields where you can update selected rows, add, delete, etc. and I'd like to have an auto-refresh upon updating... However, I cannot figure out how to do this as the table just adds the new rows underneath the old rows. Is there a way to have a complete table refresh for the mysql bound datagridview without having to re-launch the program?
Thanks in advance!
The grid is not bound to MySQL. It's bound to a DataTable. If you want a complete refresh of the data then simply clear that table and then repopulate it.
myDataTable.Rows.Clear()
As jmcilhinney suggested, datagridviews are bound to DataTables (or sometimes lists in my own case), so what he said sounds like it should work. Since, apparently, it didn't do as you wanted, here's my suggestion:
You said you have "fields" where you can update, add, delete, etc. a selected row, correct? It also sounds like you're doing something with new rows. As you've suggested that it's a databound gridview, I doubt that you're actually adding them in the typical way but instead have some sort of programmatic alternative.
So, try implementing your solution as less of a "rewrite" and more of a SQL update. When the user enters the data for the row that they wish to update or the new row they'd wish to create, search the rows for a given primary key.
In the case of a datatable, the code would look something like this:
string s = "primaryKeyValue";
DataRow foundRow = dataSet1.Tables["AnyTable"].Rows.Find(s);
foundRow.Item["DesiredColumn"] = newValue;
source: https://msdn.microsoft.com/en-us/library/y06xa2h1.aspx
In the case of a list bound as a DataSource, I believe you'd have to do something like this:
List<Thing> things = new List<Thing>(); // in case this wasn't obvious
Thing tempThing;
tempThing = things.Find(x => x.PartName.Contains("seat")));
tempThing.Value = newValue;
After you've modified the underlying datasource, then, theoretically, you should be able to simply call DataGridView.Refresh(), but you may have to do something trickier like set the datasource to null and then rebind it in order to force EndEdit.
I know this is late, but I hope it still helps someone.
I have a form that I intend to allow users to browse powerpoint presentations through, because there are a lot of different briefs for metrics, which is what I use this database for. So, I already know how to set up everything, just got one little hang up.
If I place a combo box on the form to select from a list of different presentation, can I use that in the file path string (that I have to use to pull the ppt into theobject frame in access.
example:
"C:\Users\Justin\Desktop\" & cmbTitle & ".ppt"
I tried it and it gives me the error message variable not defined. I never defined a control before on in these things, would it be as a string?
I realize that the exact file path much match the entered value. Access 2000-2003/XP
Thanks as always guys!
You need to refer to the field as Me.cmbTitle. As it is written, it looks like you're calling the variable cmbTitle which doesn't exist.
Is the value of cmbTitle some ID/Integer field or is it the actual string value? You may want to use the immediate window to check this. Also, make sure the value of cmbTitle doesn't have any backslashes or spaces (That may require quotes?).
I'm somewhat confused as to what you're trying to do. I will write my answer assuming:
you have a form in an Access database.
on that form is combo box that lists the PowerPoint presentations your users are working with.
the bound column of the combo box lists the filename (without path) of each PPT file.
when the user selects a filename from the combo box, you want to display it in an unbound OLE object frame.
The code for that, assuming the list of PPT files is called cmbTitle, would be in the combo box's AfterUpdate event and would look like this:
Private Sub cmbTitle_AfterUpdate()
Dim strPresentation As String
If IsNull(Me!cmbTitle) Then Exit Sub
strPresentation = "C:\Users\Justin\Desktop\" & Me!cmbTitle & ".ppt"
Me!olePPT.SourceDoc = strPresentation
End Sub
Now, I can't get a test unbound OLE object frame to work with this, but it seems to me to be the right way to do it.
My suspicion is that you're either attempting to set the wrong property, or you've defined your OLE frame wrongly, but I can't offer any more advice on that without knowing more about what you're actually attempting to do, and exactly what line of code is causing the error.