Generate age from sql server. VB.NET - vb.net

Can you tell me how to get ages from 0-5 and so on from sql server and input the total in a textbox or label? please click the link for sample
Generate Ages
thank you!

Do you mean something like
Dim sql As String = "SELECT age_column FROM your_table WHERE age_column BETWEEN '0' AND '5'"
'The above line selects the all records where age is between 0-5 (Change the variables)
Dim da As New OleDbDataAdapter(sql, connectionstring)
Dim ds As New DataSet
Dim dt as new DataTable
da.Fill(ds)
dt = ds.Tables(0).Copy()
' Above code stores the results in an iterable table
Dim i As Integer = 0
For Each dr as DataRow in dt.Rows
i = i + 1
Next
TextBox1.Text = i
'i will be the total number of records where age is between 0-5, and display it in a `TextBox`
Your question is, however, very poor, and normally people would not put too much effort into answering it, since you've put no effort in yourself.

Related

DataTable And Two Row Sorting

So the boss comes to me and says "I want the value of each agent and the project on one line and the average of all the other agents on the next line so I can easily see if they are above or below average."
the table looks like this:
dt.Columns.Add("AGENT", GetType(String))
dt.Columns.Add("PROJECT", GetType(String))
dt.Columns.Add("Sales", GetType(Integer))
dt.Columns.Add("Declines", GetType(Integer))
dt.Columns.Add("Margin", GetType(Integer))
Ok its all good. One row in the datatable is the agent and project. The next row is the average of all the other agents and project like so:
row 1:
John Smith,
ProjectName,
(other column values)
row 2:
John Smith,
ProjectName & " AVERAGE/TOTAL",
(other column values)
The project name is removed in the SSRS report on the AVERAGE/TOTAL line because of space constraints on the piece of paper it is printed on.
I do the sorting by our standard way of sorting a datatable.
Dim dataView As New DataView(dt1)
dataView.Sort = "AGENT,PROJECT"
dt1 = dataView.ToTable
Return dt1
But now the boss has a new requirement later on. He wants to be able to sort by other columns in the table but keep the two rows the (agent/project and the agent/project AVERAGE/TOTAL) together. So in essence he wants to be able to sort not by one row but the two rows together but the sort value could be "AGENT,Margin". Obviously to keep the two rows together I have to find a way to sort the Project value too.
So I am stumped and would appreciate any thoughts you might have. C# ideas are welcome as well.LINQ is fine but it is going to have to become a datatable.
so you create two tables. One with the row values one one with the average values. Loop through the row values and then do another loop inside that loop to match project names. Its a hack but it worked.
If SortValue = "Default" Then
dt1.Merge(dt)
Dim dataView As New DataView(dt1)
dataView.Sort = "AGENT,PROJECT"
dt1 = dataView.ToTable
Else
Dim dataView As New DataView(dt)
dataView.Sort = SortValue
dt = dataView.ToTable
Dim dtCopy As New DataTable
dtCopy = dt.Clone
For Each row As DataRow In dt.Rows
dtCopy.ImportRow(row)
For i = 0 To dt1.Rows.Count - 1
If dt1.Rows(i).Item("PROJECT").ToString.Replace(" AVERAGE/TOTAL", "") = row.Item("PROJECT") And dt1.Rows(i).Item("AGENT") = row.Item("AGENT") Then
dtCopy.Rows.Add(dt1.Rows(i).Item("AGENT"), dt1.Rows(i).Item("PROJECT"), dt1.Rows(i).Item("SALES"), dt1.Rows(i).Item("Declines"), dt1.Rows(i).Item("Margin"))
End If
Next
Next
dt1 = dtCopy
End If

when I choose a value from the first compbox is the value of the remaining compbox is changed

I have 3 compobboxes I have created an import code from a SQL table
The problem when I choose a value from the first compbox
the values of other compbox is changed to be like my choice
I made a separate code for each compbox
But I find it impractical because my project has 90 compoboxes
It needs time to run
Is there a more practical solution?
this is my code...
Dim com As New SqlCommand("select Distinct Name1 from TB_dr", Con)
Dim RD As SqlDataReader = com.ExecuteReader
Dim DT As DataTable = New DataTable
DT.Load(RD)
ComboBox1.DisplayMember = "Name1"
ComboBox1.DataSource = DT
ComboBox2.DisplayMember = "Name1"
ComboBox2.DataSource = DT
ComboBox3.DisplayMember = "Name1"
ComboBox3.DataSource = DT
Only populate a combobox on its dropdown event.
That will make your app faster cause your client may not use them all and if he uses one combobox he would populate a small amount of data only.
And no data is populated on load.
As i look at what you are trying to do which is same data for all comboboxes, you can simply put all your comboboxes in a group, and go through your group to populate them all at once.
like for each cb in that group, cb.datasource = dt.
call the datatable once

Selecting the Nth record in a SQL dataset

Lets say I have a dataset with I dunno 20 records in it.
How do I get to the 16th record in the dataset or the Nth? So any number from 1 to 20
I have spent most of this week trying to think of a method and so far I have gotten no where.
I want the 16th record down in this dataset and there doesn't seem to be a specific command for it.
I'm working in VB.NET with OLE commands.
I'm not sure any code would be of any use to help solve this but I'm populating the dataset something like this:
SQL_Str = "SELECT FROM A TABLE WHERE CRITERIA IS MET"
dbDataAdapter = New OleDbDataAdapter(SQL_Str, dbConnector)
dbDataAdapter.Fill(DataSet, "SelectedRecords")
Now what do I do to get to the 16th row in this dataset knowing that there is 20 records in the dataset?
Since you say you are using VB.NET, just read the row from the dataSet.
Private Function GetRow(ByVal ds As Data.DataSet, ByVal rowNum As Integer) As Data.DataRow
Dim result As Data.DataRow = Nothing
Dim table As Data.DataTable = ds.Tables(0)
result = table.Rows(rowNum)
Return result
End Function
There are overloads to DataSet.Tables: Consider:

How can I display column names of one table in a combobox?

In vb.net 2008, how can I display table column names in a combobox?
In SQL I have a Document table and there are columns in this table like Doc_id, Size, Path.
The combobox must display the columns name like
doc_id in the first line, size in the second line, path in the last line
Probably a little too late since I see this thing is about eight years old, but I just ran into this issue where I had to do exactly what the initial questioner was asking for. Since I resolved it, I will leave it here just incase another poor sap like us comes along looking for answers
'VB.net
Dim ds As DataSet = <define your dataset here>
Dim dt As DataTable = ds.Tables(<Your tablename here>)
For Each column As DataColumn In dt.Columns
combobox1.Items.Add(column.ColumnName)
Next
As per our discussion , i will suggest you not to bind the combobox directly with the DataTalble. you can bind it by looping all the row of the DataTable. you use following snipped as a reference:
Dim objDataTable As DataTable = ds.Tables("Document")
IF objDataTable <> NULL Then
For j As Integer = 0 To objDataTable.Rows.Count
Dim str As String = objDataTable.Rows(j)("Doc_id").ToString()
comboBox1.Items.Add(str)
str = objDataTable.Rows(j)("Size").ToString()
comboBox1.Items.Add(str)
str = objDataTable.Rows(j)("Path").ToString()
comboBox1.Items.Add(str)
Next
END IF
Hope this will work for you ;)

Sorting by date with dataview in VB.NET

Hi and thanks in advance:
So I'm trying to pull a table from an SQL database and sort it on the "Date From" (DtFr) column, which represents the start of a lease. I thought it would be as easy as this:
Dim XmlReader As New XmlNodeReader(xmlDoc)
Dim ds As New DataSet
ds.ReadXml(XmlReader)
myDt = ds.Tables("Cam")
If myDt IsNot Nothing AndAlso myDt.Rows.Count > 0 Then
Dim dv As New DataView(myDt)
dv.Sort = "DtFr ASC"
myDt = dv.ToTable(False, "Id", "Name", "Amount", "Unpaid", "DtFr")
End If
But as you might have guessed this is not working (new user, can't post images it's just a picture of the dates out of order).
Could it be because at some point along the data retrieval it's no longer of type Date and it's a string or something? If I use simpler dates, like 1/1/2012, 1/2/2012, 1/3/2012 or 1/1/2012, 2/1/2012, 3/1/2012 etc. it sorts alright, but as you'll notice above, if i have varied months, it sorts it out of order.
I've tried messing with the culture, thinking it could be that it's sorting them on the months before the year to no avail... I'm at a loss, any help, ideas, or direction to something helpful would be great.
Much Appreciated,
Josh
EDIT:
You're a genius, thanks so much. You were right about the string value. I added a data column called sortDtFr to the datatable and gave it System.DateTime as a datatype and iterated through the rows setting the new column's value equal to the old one casted as a Date. Here's the code for those interested:
ds.ReadXml(XmlReader)
myDt = ds.Tables("Cam")
myDt.Columns.Add(New DataColumn("sortDtFr", System.Type.GetType("System.DateTime")))
For Each row As DataRow In myDt.Rows
row("sortDtFr") = CDate(row("DtFr"))
Next
If myDt IsNot Nothing AndAlso myDt.Rows.Count > 0 Then
Dim dv As New DataView(myDt)
dv.Sort = "sortDtFr ASC"
myDt = dv.ToTable(False, "Id", "Name", "Amount", "Unpaid", "DtFr", "sortDtFr")
End If
The reason why your records are sorted in that way is because the datatype of that DataColumn of your DataTable (Cam) is System.String (by default). Try changing it to System.DateTime.