Displaying number of PWD(y-axis) in every purok(x-axis) - vb.net

Desired chart
Current chart
Hey guys, Im trying to make a column chart using the purok and status column. How can you show the PWD and Pregnant as a bar with label and count also? thankyou in advance
The table name is PWDPregnant | columnName "status" is for PWD/Pregnant. | columnName "purok" is for Pepsi/Coke.
Dim dt As New DataTable
Using pwdcon As New OleDbConnection(con.ConnectionString),
cmd As New OleDbCommand("SELECT purok, status, COUNT(*) As RecCount FROM PWDPregnant GROUP BY purok, status ORDER BY purok", con)
If con.State = ConnectionState.Closed Then
con.Open()
End If
Using reader = cmd.ExecuteReader
dt.Load(reader)
End Using
End Using
Chart5.Series("PWD").XValueMember = "purok"
Chart5.Series("PWD").YValueMembers = "status"
Chart5.Series("PWD").Label = "#AXISLABEL #VALY"
Chart5.Series("Pregnant").YValueMembers = "status"
Chart5.Series("Pregnant").Label = "#AXISLABEL #VALY"
Chart5.DataSource = dt.DefaultView
Chart5.DataBind()

Finally I found the fix but it still cant support the 2nd series which is Pregnant.
Dim dt As New DataTable
Using pwdcon As New OleDbConnection(con.ConnectionString),
cmd As New OleDbCommand("SELECT purok, status, COUNT (*) as RecCount FROM PWDPregnant WHERE status = #stat GROUP BY purok, status", con)
cmd.Parameters.Add("#stat", OleDb.OleDbType.VarChar).Value = "PWD"
If con.State = ConnectionState.Closed Then
con.Open()
End If
Using reader = cmd.ExecuteReader
dt.Load(reader)
End Using
End Using
Chart5.ChartAreas(0).AxisX.Interval = 1
Chart5.Series("PWD").XValueMember = "purok"
Chart5.Series("PWD").YValueMembers = "RecCount"
Chart5.Series("PWD").Label = "#VALY"
Chart5.DataSource = dt.DefaultView
Chart5.DataBind()

Related

Access Select query values into variables

I'm trying to create a project app, in said app i have a database to store data that the user inserts which i later use for various things.
I'm trying to get the value for the Weight and Height from the DB and im currently using this code:
Sub load()
Dim weight As Double
Using getData = New OleDbCommand("SELECT TOP 1 Weight FROM Data WHERE ID_User = #ID_User ORDER BY id DESC", conn)
getData.Parameters.Add("ID_User", OleDbType.VarChar).Value = My.Settings.currentUserID
If conn.State = ConnectionState.Closed Then conn.Open()
weight = getData.ExecuteScalar
End Using
Dim height As Integer
Using getData = New OleDbCommand("SELECT TOP 1 Height FROM Data WHERE ID_User = #ID_User ORDER BY id DESC", conn)
getData.Parameters.Add("ID_User", OleDbType.VarChar).Value = My.Settings.currentUserID
If conn.State = ConnectionState.Closed Then conn.Open()
height = getData.ExecuteScalar
End Using
End Sub
This works fine but im trying to reduce my code, is there a way to select multiple fields values in a single select query?
Example:
SELECT TOP 1 Weight, Height FROM Data WHERE ID_User = #ID_User ORDER BY id DESC
I already tried to use the example code but its just returning me the weight value twice for some unknown reason.
Thanks to #Jeremy and #ParrishHusband for helping me i managed to solve the problem, if anyone needs the solution this is how i did it:
Sub load()
Dim weight As Double
Dim height As Integer
Using getData = New OleDbCommand("SELECT TOP 1 Weight, Height FROM Data WHERE ID_User = #ID_User ORDER BY id DESC", conn)
getData.Parameters.Add("ID_User", OleDbType.VarChar).Value = My.Settings.currentUserID
If conn.State = ConnectionState.Closed Then conn.Open()
Dim reader As OleDbDataReader = getData.ExecuteReader
If reader.HasRows Then
While reader.Read
weight = reader.GetString(0)
height = reader.GetString(1)
End While
Else
MsgBox("No rows found")
End If
reader.Close()
End Using
MsgBox(weight & ", " & height)
End Sub

Use datagridview cell value from form 1 to filter datagridview 2 in form 2

I have 2 different datagridview (1. Client List 2. Payment History of that Client) in two different forms. When I click the client from form 1 I want to use his clientID to filter the datagridview on Form2 problem is when I use WHERE as a query it doesn't show the table, if I remove WHERE all of the payments in the payment table will show.
here's the code for when clicking the row from form 1
ElseIf colName = "colViewPayment" Then
Dim row As DataGridViewRow = datagridviewClient.Rows(e.RowIndex)
Dim fname As String
Dim lname As String
Using cmd As SqlCommand = New SqlCommand("SELECT * FROM tbtClients WHERE ClientID = #ClientID")
cmd.CommandType = CommandType.Text
cmd.Parameters.AddWithValue("#ClientID", row.Cells("ClientID").Value)
cmd.Connection = con
con.Open()
Using sdr As SqlDataReader = cmd.ExecuteReader()
sdr.Read()
fname = sdr("ClientFirstName")
lname = sdr("ClientLastName")
frmLoanerPaymentHistoryBunifu.NameR.Text = fname + " " + lname
frmLoanerPaymentHistoryBunifu.CpNo.Text = sdr("ClientMobileNumber").ToString()
frmLoanerPaymentHistoryBunifu.ClientID.Text = sdr("ClientID").ToString()
End Using
con.Close()
frmLoanerPaymentHistoryBunifu.ShowDialog()
End Using
Here is the code for populating the datagrid in Payment History Form (form2)
Private Sub BindGrid()
Dim CID As String = ClientID.Text
Dim x As Integer = Convert.ToInt64(CID)
Using cmd As New SqlCommand("SELECT * FROM PaymentTable WHERE ClientID = '" + ClientID.Text + "'", con)
cmd.CommandType = CommandType.Text
Using sda As New SqlDataAdapter(cmd)
Using dt As New DataTable()
sda.Fill(dt)
pDataGrid.AutoGenerateColumns = False
'Add Columns
pDataGrid.Columns(0).Name = "PID"
pDataGrid.Columns(0).DataPropertyName = "PaymentID"
pDataGrid.Columns(1).Name = "CID"
pDataGrid.Columns(1).DataPropertyName = "ClientID"
pDataGrid.Columns(2).Name = "Pdate"
pDataGrid.Columns(2).DataPropertyName = "PDate"
pDataGrid.Columns(3).Name = "Pamt"
pDataGrid.Columns(3).DataPropertyName = "PAmt"
pDataGrid.Columns(4).Name = "Pren"
pDataGrid.Columns(4).DataPropertyName = "ReloanDate"
pDataGrid.Columns(5).Name = "Pcol"
pDataGrid.Columns(5).DataPropertyName = "PColl"
pDataGrid.DataSource = dt
End Using
End Using
End Using
End Sub

i have change the background color of row in datagridview if already expired the medicine?

i have change the background color of row in datagridview if already expired the medicine?
con.Open()
Dim query As String
query = "Select product_code,drug_name,quantity,expiration_date from medicine where expiration_date"
command = New MySqlCommand(query, con)
readers = command.ExecuteReader
Dim count As Integer
count = 0
While readers.Read
count = count + 1
End While
con.Close()
If count = 0 Then
MsgBox("no expiration")
Else
Dim SQL As String = ""
Dim da As MySqlDataAdapter = Nothing
Dim dt As New DataTable
SQL = "Select product_code,drug_name,quantity,expiration_date from medicine where expiration_date"
command = New MySqlCommand(SQL, con)
End If
Ok fist of all you need to load de grid with the database data in this way:
con.Open()
Dim query As String
Dim da As new MySqlDataAdapter
Dim dt As New DataTable
query = "Select product_code,drug_name,quantity,expiration_date from medicine where expiration_date is not null"
command = New MySqlCommand(query, con)
da.SelectCommand = cm
da.Fill(dt)
dgv1.datasource = dt
Then you have to set the color in the cellformating event:
Private Sub dgv1_CellFormatting(sender As Object, e As DataGridViewCellFormattingEventArgs) Handles dgv1.CellFormatting
If dgv1.Rows(e.RowIndex).Cells("expiration_date").Value < now Then
dgv1.Rows(e.RowIndex).cells("expiration_date").Style.BackColor = Color.Red
End If
I do not understand what you are trying to do or how you are going to load the grid. any way to change the color of a column all you have to do is:
dgv1.Columns("columnsName").DefaultCellStyle.BackColor = Color.Red

display data into textbox vb.net

how to display result from data base into textbox and if the result exceed 1 key up and down to preview
con = New SqlConnection(cs)
con.Open()
Dim sql As String = " Select RTRIM(visit.regdate),RTRIM(Patientno) from visit where visit.accno =#d6 "
cmd = New SqlCommand(sql, con)
cmd.Parameters.AddWithValue("d6", accno.Text)
rdr = cmd.ExecuteReader()
While (rdr.Read() = True)
regdate.Value = rdr.GetValue(0)
patientno.Text = rdr.GetValue(1)
End While
You must use listbox instead textbox to put your data, here is an example:
ListBox1.ColumnCount = 3
ListBox1.Columnwidths = "100,100,100"
ListBox1.AddListItem("row1 col1", 1,1)
ListBox1.AddListItem("row1 col2", 1,2)
ListBox1.AddListItem("row2 col2", 2,2)

VB Auto populate datagridview from datatable

Is it possible to have a datagrieview populate directly from a datable ? Below is what I am trying in the form load. Basically I want it to show all the columns this query returns automatically
Dim con As New OleDbConnection
con = New OleDbConnection(connStr)
If con.State = ConnectionState.Closed Then
con.Open()
End If
Dim titleSQLStr As String = "SELECT * FROM Titles ORDER BY YearPublished DESC"
daYears = New OleDbDataAdapter(titleSQLStr, connStr)
daYears.Fill(dtYears)
cboYearsFillBy.DataSource = dtYears
cboYearsFillBy.DisplayMember = "YearPublished"
cboYearsFillBy.ValueMember = "YearPublished"
DataGridView1.AutoGenerateColumns = True
DataGridView1.DataSource = daYears
con.Close()
Change it to:
DataGridView1.DataSource = dtYears