How to display the count value on label? - mysql

I have a monitoring in datagridview that has a display of list of vehicles and their types. I want to count the number of each type of the vehicle.
Example : there are 8 Sedans and 5 Motorcycles in the datagridview and I want it to display in the label.
here is my code:
Dim table As New DataTable()
Dim command As New MySqlCommand("select count(ctype) from tblreport where ctype='SUV'", conn)
command.Parameters.Add("count(ctype)", MySqlDbType.VarChar).Value = Label6.Text
Dim adapter As New MySqlDataAdapter(command)
adapter.Fill(table)
DataGridView2.DataSource = table

After you assign the datasource, just add this :
Label1.Text=datagridview1.rows.count
Text is a property of many controls like label,button and others.This property can be used to display an overlay of text over the control. Datagridview has rows ,right ? So, as u said, u want to display the number of rows the datagridview contains.So all you need to do is use Datagridview1.rows.count,i don't think this one needs any explanation at all.
Anyway, let's advance a bit further.
You also said
there are 8 Sedans and 5 Motorcycles in the datagridview and I want it to display in the label.
By default,no application can understand how many records match Sedans and Motorcycles .But as mentioned above, vb is able to calculate the rows of a datagridview(by default/by simple codes).So try to think for once,if the datagridview only had the rows that contained Sedans or Motorcycles in any cell, vb.net could've easily counted it..right ??
So, if u ask me, how do we get only the rows containing Sedans or Motors , then i'ld as u, how does google show only the results u r looking for instead of showing everything it has ?? The answer is simple,google shows exactly what you Search for. When it comes to databases,we call it filtering/sorting/even seaching.So, all you need to do is filter your database.There are couple of ways,which,if i start explaining,would take a lotta time.However,check these :
• http://csharp.net-informations.com/datagridview/csharp-datagridview-filter.htm
• Filtering DataGridView without changing datasource
Now, let's go deeper.If you go through the links above,you'll be able to show only the rows that contain Sedans or so in any of it's cells.What if you want all the rows but only want the label to display the number(row count) of Sedans or so ?
In this case,you can use a Datatable.
Learn more about Datatable here
You can easily use a datatable to count the rows containg only the QUERY u r looking for.For that, u can code as follows :
Dim cmd as new MySqlCommand("Select * from [tablename](columnname)(#value)",con)
cmd.parametres.addwithvalue("#value","Sedan")
Dim table as new DataTable
Dim adap as new MySqlDataAdapter
adap.fill(table)
If table.rows.count <=0 then
Else
LAbel1.text=table.rows.count
Let me explain from dim table.Here i am declaring a datatable.But before that i am filtering the dataset by using cmd.parametres.addwithvalue.Then,i am using a DataAdapter and filling the DataTable with only th e rows containing SEDAN. Now, focus on If table.Rows.Count <=0 Then ...This indicates if there are no rows(which is less than or equals to 0) containing the query.Then i am using the Else statement which means if the datatable contains rows matching the query.In the else statement,i am adding the row count of the Datatable as a string/text in the Label..
I hope this is enough and this explanation will help u in future as well.

Use the same DataTable you have used to fill the DataGridView. Only change your select statement to Select * from tableName. I used the index of 2 but change to whatever it is in your table. The column index starts at zero.
Dim totalSedans As Integer = 0
Dim totalMotorcycles As Integer = 0
Dim totalConvertable As Integer = 0
For Each row As DataRow In table.Rows
Select Case row(2).ToString
Case "Sedan"
totalSedans += 1
Case "Motorcycle"
totalMotorcycles += 1
Case "Convertable"
totalConvertable += 1
Case Else
Debug.Print("Something doesn't match categories")
End Select
Next
lblConvertable.Text = totalConvertable.ToString
lblMotorcycles.Text = totalMotorcycles.ToString
lblSedan.Text = totalSedans.ToString

Related

VB.net Autocomplete Textbox filter using mysql database as custom source

I'm Having a problem regarding to the autocomplete textbox. First I already made the autocomplete textbox work with mysql database as custom source but the default textfilter of autocomplete is "start with" not "contains". I want to change the textfilter to "contains", so that when I search any part of the string, the whole name which contains the searched word will appear in the autocomplete suggestions.
Can anyone help me fix my code?
This is the code i've done so far:
txtSearch.AutoCompleteMode = AutoCompleteMode.SuggestAppend
txtSearch.AutoCompleteSource = AutoCompleteSource.CustomSource
Dim DataCollection As New AutoCompleteStringCollection()
Dim query As String
sqlcon = New MySqlConnection
sqlcon.ConnectionString =
"server=localhost;userid=root;password=root;database=svfmemberlistdb"
Try
sqlcon.Open()
query = " SELECT Name FROM svfmemberlistdb.svfmemberlist "
sqlcmd = New MySqlCommand(query, sqlcon)
sqladr.SelectCommand = sqlcmd
sqladr.Fill(ds)
sqladr.Dispose()
sqlcon.Close()
For Each row As DataRow In ds.Tables(0).Rows
If row.ToString.Contains(txtSearch.Text) Then
DataCollection.Add(row(0).ToString())
End If
Next
Catch ex As Exception
End Try
txtSearch.AutoCompleteCustomSource = DataCollection
I quote here Mitja Bonca's answer on MSDN.
In this case, autocompletemode will just not do. Its code is not meant
for something like it.
You will have to do your own code, to do the filtering on each letter
press.
So I would suggest not to use autocompletemode, and get all the data
(names) into dataTable. When user presses some button ("1" for
example), you start with your filtering, by creating new Datatable
(leave the main one untached - so you can return back to all data when
clearing comboBox by backspace), with Copy() method - to create a full
copy of original one, and use Select method to do the filteing.
This should look something like by using % simbol on both sides of a
string - to filter inbetween - this is what you want!
DataTable AllNames = new DataTable();
//fill it up and leave it untouched!
//to filter comboBox with names that contains pressed characters do in
private void comboBox1_KeyPress(object sender, KeyPressEventArgs e)
{
string name = string.Format("{0}{1}", comboBox1.Text, e.KeyChar.ToString()); //join previous text and new pressed char
DataRow[] rows = table.Select(string.Format("FieldName LIKE '%{0}%'", name));
DataTable filteredTable = AllNames.Clone();
foreach(DataRow r in rows)
filteredTable.ImportRow(r);
comboBox1.DataSource = null;
comboBox1.DataSource = filteredTable.DefaultView;
comboBox1.DisplayMember = "FieldName";
}
Reference
EDIT: This is of course a c# answer not VB.NET but it might be helpful to get the concept.

VBA code to analyze a HTML table based off certain conditions

So I need to screen scrape data off a website and return it to a spreadsheet based off if a charge amount matched as well the date was the most recent in the table. If there was simply one line in the table, the macro pulls that accordingly. So most of the code is good, I am connected to the website, pulling everything effectively. Where I am struggling is getting the logic to work where the two amounts match as well as the date being the most recent in the HTML table.
I guess what my question is how do I loop through Item(5) the column of that table and specify it to choose the most recent date, also setting the value so that it only finds the one equal to the charge amount. I only want a one to one match. I am new to this so if anyone wants to help me I would greatly appreciate it.
Set IHEC = iHTMLDoc.getElementsByTagName("TR")
If IHEC.Length > 2 Then
For index = 0 to IHEC.Length - 1
Set IHEC_TD = IHEC.Item(index).getElementsByTagName("TD")
Do Until IHEC.Length <2 Or index = IHEC.Length - 1
If IHEC.TD.Item(3).innerText = myBilledAmount Then
myItem1 = IHEC_TDItem(0).innerText
myItem2 = IHEC_TDItem(1).innerText
myItem3 = IHEC_TDItem(2).innerText
myItem4 = IHEC_TDItem(3).innerText
myItem5 = IHEC_TDItem(4).innerText
myItem6 = IHEC_TDItem(5).innerText
myItem7 = IHEC_TDItem(6).innerText
myItem8 = IHEC_TDItem(7).innerText
myItem9 = IHEC_TDItem(8).innerText
End If
End If
Loop
Next Index

How to speed up the populating of listview in vb.net

This is the code for populating the listview
For i = 0 To ds.Tables(0).Rows.Count - 1
For j = 0 To ds.Tables(0).Columns.Count - 1
itemcoll(j) = ds.Tables(0).Rows(i)(j).ToString()
Next
Dim lvi As New ListViewItem(itemcoll)
myLVstyle.Items.Add(lvi)
Next
I tried using the following:
myLVstyle.SuspendLayout()
my code for populating listview
myLVstyle.ResumeLayout()
and
myLVstyle.BeginUpdate()
my code for populating listview
myLVstyle.EndUpdate()
I'm using mysql for database and this is my select statement:
SELECT STYLE_CODE, `DESC`, FAB_CONS, DATE_CREAT, LAST_ORD, QTY_ORD, GRAPHIC, NOTES1, NOTES2, NOTES3 FROM style
But non of these help me speed up the populating of listview.
Thank you.
You should keep the:
BeginUpdate
and the
EndUpdate
If thats not speeding up things, you should look for a faster SQL server.
One little test you could try, is fill the listview with the same number of random items, and skip getting it from SQL. This way you can pinpoint if SQL is the bottleneck...
So you get something like;
with myLVstyle
.beginupdate()
for each row as [rowtype] in ds.tables.rows
for each col as [coltype] in row.colums
dim NewLvi as listviewitem = .items.add(col.itemcoll)
next
next
.endupdate()
end with

invalid property value in vb6

i have a code right here this is for saving records to databse:
If mstrMaintMode = "ADD" Then
lngIDField = GetNextCustID()
strSPName = "InsertCustomer"
Set objNewListItem = mylistview.ListItems.Add(, , txtname.Text)
PopulateListItem objNewListItem
With objNewListItem
**.SubItems(mlngCUST_ID_IDX) = CStr(lngIDField)**
.EnsureVisible
End With
Set mylistview.SelectedItem = objNewListItem
Set objNewListItem = Nothing
Else
lngIDField = CLng(mylistview.SelectedItem.SubItems(mlngCUST_ID_IDX))
strSPName = "UpdateCustomer"
mylistview.SelectedItem.Text = txtname.Text
PopulateListItem mylistview.SelectedItem
End If
the error is: invalid property value in the line with asterisks. ive tried using this code to another database and it works, but for the other it's not.ive checked the stored procedure, it's right, the table fields, also right but im still getting this error.ive spent 3 hrs to find the answer but i culdn't figure it out.
The line you have highlighted will fail with "Invalid property value" when you specify a sub item index that is out of bounds given the number of columns in the listview.
As the index is 1 based but starting from the second column, with your index of 7, you need at least 8 columns added.

Dynamic Columns

Here's my problem. I am passing in a parameter let say it's called ShapesSelected.
ShapeSelected = ",Square, Triangle, Circle,".
The problem is ShapeSelected could be any of the the shapes so it is never static.
Base on this parameter I want to add 3 column to the right of a table in the report. Is this possible? I've starting coding it in Custom Code in Report Properties but I am stuck as in how to add the column.
Public Function GetReportShapes( ByVal ShapesSelected As String )
Dim Shapes() As String
Dim result As String
Dim i As Integer
Entities = Split(ShapesSelected ,", ")
For i = 0 To UBound ( Shapes)
Select case Shapes(i)
case "Square": 'add Square Column here
case "Rectangle": add Rectange Column here
case "Triangle": add Triangle Column here
End Select
Next i
End Function
Thus rendering the columns like this:
Square Triangle Circle
Add all the columns you need and use the hidden or visible property (I forget which) with vb expressions to turn them on and off.