read and modify/update cell in datatable - asp.net

Scenario
I want to calculate and rank the suitability of healthcare workers for a particular patients, by comparing parameters such as race, religion, gender, spoken language, etc.
HealthcareWorker table
----------------------
ID (int)
name (varchar)
race (varchar)
religion (varchar)
gender (varchar)
german (bit)
french (bit)
english (bit)
Patient table
-------------
ID (int)
name (varchar)
race (varchar)
religion (varchar)
gender (varchar)
german (bit)
french (bit)
english (bit)
Webpage Layout
On page1.aspx there will be a gridview table showing the details of all Patients.
User will then select a Patient and be directed to page2.aspx
On page2.aspx there will a table showing the details of each Healthcare Worker and his suitability with the selected patient in the form of a score. there will also be checkboxes inside the table in order to assign more than one Healthcare Worker to the selected Patient.
page2.aspx
| ID | name | religion | gender | german | french | english | score | |
|----+-------+----------+---------+----------+---------+----------+--------+----------|
| 4 | mary | | f | 1 | 0 | 1 | 132 | checkbox |
| 2 | john | | m | 0 | 1 | 1 | 125 | checkbox |
| 3 | tim | | m | 1 | 0 | 1 | 98 | checkbox |
| 1 | jane | | f | 1 | 1 | 0 | 55 | checkbox |
the computation of the score will be something like this:
if patient.race = healthcareworker.race then healthcarework.score +10
if patient.religion = healthcareworker.religion then healthcarework.score +10
if at least one of patient's language match with healthcareworker's language then healthcarework.score +10
code:
Dim strCon As String = ConfigurationManager.ConnectionStrings("ConnectionString1").ConnectionString
Dim con As New SqlConnection(strCon)
Dim query As String = "select *, CAST ( 0 as int ) score from HealthcareWorker;"
Dim cmd As SqlCommand = New SqlCommand(query, con)
con.Open()
Dim dr As SqlDataReader = cmd.ExecuteReader(CommandBehavior.CloseConnection)
Dim dt As DataTable = New DataTable()
dt.Load(dr)
'code to read cell
'code to compare and compute
'code to update the score in each row
GridView1.DataSource = dt
GridView1.DataBind()
con.Close()
How do I read and modify the cells in the datatable?
I want read and compare the data from some cells in each row and then update the score value.
I need to update the score for each row so I am going to need a loop but I do not know the number of rows that will be loaded into the datatable before hand.
How do I get the number of rows in the datatable?
edit/update: reworked the entire question to provide (a lot) more background information

The last question is answered easily:
I need to update the score for each row so I am going to need a loop
but I do not know the number of rows that will be loaded into the
datatable before hand. How do I get the number of rows in the
datatable?
You just have to use the Rows.Count property:
Dim rowCount As Int32 = tblPatient.Rows.Count ' tblPatient => DataTable '
If you want to loop all rows you can use a foreach:
For Each row As DataRow In tblPatient.Rows
' ... '
Next
or a for-loop:
For i As Int32 = 0 To tblPatient.Rows.Count - 1
Dim row As DataRow = tblPatient.Rows(i)
' ... '
Next
To update the rows you can use the SetField extension method which also support nullable-types:
For Each row As DataRow In tblPatient.Rows
Dim id As Int32 = row.Field(Of Int32)("ID")
Dim race As String = row.Field(Of String)("Race")
row.SetField("Score", CalculateScore(row))
Next
Sorry that i cannot provide you the correct calculation but it's still not clear how you want to calculate it since you haven't provided the source of the Score(theres no column). But it might give you an idea anyway.

Related

highlight gridview cell by header and row values

I have a gridview and I was able to highlight a cell based on field name and criteria of cell in that column. Now I want to do the same thing but also consider the row value from the second column as well. Example below
Gender | Part Number | Small | Medium | Large
Men | C-888-TVN | 5 | 6 | 9
Men | C-777-TV4 | 7 | 7 | 8
So if I want to highlight the '7' in the C-777-TV4 row and under the Medium size how would I go about doing both criteria. This is not based on a number value criteria just the name of the header "Medium" and string value in the part number row "C-777-TV4".
Here is the new code that i'm getting an out of range error:
Protected Sub gridStock_HtmlRowPrepared(sender As Object, e As ASPxGridViewTableRowEventArgs) Handles gridStock.HtmlRowPrepared
If e.RowType <> GridViewRowType.Data Then
Return
End If
Dim name As String = e.GetValue("CorePN").ToString()
If name = "777-M-MBL" Then
e.Row.Cells(10).BackColor = System.Drawing.Color.Red
End If
End Sub
Getting the error at e.Row.Cells(10).Backcolor
I need to see more code of yours. Here is what I thought:
If row.Cell(1).Text = "C-777-TV4" Then
e.Row.Cells(5).BackColor = System.Drawing.Color.Cyan
End If

compare 2 varchar for partial match

Table1
+---------------+
| varchar1 |
+---------------+
| aaa, aba, aab |
| aac, aca, caa |
+---------------+
Table2
+---------------+
| varchar2 |
+---------------+
| bbb, abb, aba |
| bbc, bcb, cbb |
+---------------+
How to compare the varchar from 2 different tables for a partial match?
Such that aaa, aba, aab from Table1 matches with bbb, abb, aba from Table2 because both contains aba.
As mentioned in the comments, you REALLY should normalize this. That's definitely a better solution than having to parse out strings like this. However, if you really wanted to, you could do something like this:
Dim varchar1 As IEnumerable(Of String) = {"aaa, aba, aab", "aac, aca, caa"}
Dim varchar2 As IEnumerable(Of String) = {"bbb, abb, aba", "bbc, bcb, cbb"}
Dim varchar1Tokens As IEnumerable(Of String) = (From foo In varchar1
Let tokens = foo.Split(","c)
From bar In tokens
Select bar.TrimEnd)
Dim varchar2Tokens As IEnumerable(Of String) = (From foo In varchar2
Let tokens = foo.Split(","c)
From bar In tokens
Select bar.TrimEnd)
Dim matches As IEnumerable(Of String) = (From item In varchar1Tokens
Where varchar2Tokens.Contains(item)
Select item)
For Each match As String In matches
Console.WriteLine(match)
Next
Console.ReadLine()

How to Display doctors ordered by number of performed surgeries

I have table [Surgery_By] table[Surgery] table[Doctor] i'm using ASP.NET with SQL Server :
The table [Surgery_By] contains the following columns:
1-ID (PK)
2-Surgery ID (FK)
3-Doctor ID (FK)
How to Display doctors ordered by number of performed surgeries ?
Try it this way
SELECT d.id, d.fullname, COUNT(s.id) total_surgeries
FROM doctor d LEFT JOIN surgery_by s
ON d.id = s.doctor_id
GROUP BY d.id, d.fullname
ORDER BY total_surgeries DESC
Sample output:
| ID | FULLNAME | TOTAL_SURGERIES |
|----|------------|-----------------|
| 1 | John Doe | 3 |
| 2 | Jane Doe | 1 |
| 3 | Mark Smith | 0 |
Here is SQLFiddle demo
This is a stab in the dark.
Select Doctor.ID As DoctorID
,Count(*) As Count
From Doctor
Join Surgery_By
On Doctor.ID = Surgery_By.DoctorID
Group By Doctor.DoctorID
Order By Count(*)
I am not sure if you want the table Surgery incorporated (but if you do, the join will be pretty straight forward - just be sure to add selected columns to the Group By statement.)
From ASP.NET, you may select this data from a SQL Command.

GridView : Bind Multiple value from database in single column

I want data in gridview like this:
CategoryName Subcategory Name
--------------------------------
Abc Abc1,abc2,abc3
Bcs Bcs1,bcs2
def Null / No Record
How can I do that?
okay i want data from database by using single table. i have one table categories in which i have field like categryid, parentid,name. when parentid is 0 then its known by Categories else all other is subCategories.
I Am using asp.net with c# and i want to do this in gridview with using boundfield. for categories i have done but for sub categories i dont have any idea how to do.
subcategories is idedntify by its parentid. in sub categories parentid =categryid
Assuming data in your table is something like this
CategoryId| ParentID| Name |
1 | 0 | A |,
2 | 1 | B |,
3 | 0 | C |,
4 | 3 | D |
You can do something like
Create Table #ReportTable(Id identity int,CategoryId int,Category varchar(10),SubCategory Varchar(10))
Declare #CountOfRecords int = Select count(categoryid) from categories
Declare #TableIterator int = 1
While #TableIterator <= #CountOfRecords
Begin
Declare #ParentId int = (Select ParentId From Categories Where CategoryId=#TableIterator)
If #ParentId = 0
Begin
Insert Into #ReportTable(CategoryId,Category)
Select CategoryId,Category
From Categories
Where CategoryId = #TableIterator
End
Else
Begin
Update #ReportTable
Set SubCategory =
(Select SubCategory From Categories Where CategoryId = #TableIterator)
And Id = #ParentId
End
Set #TableIterator = #TableIterator + 1
End

How to select n rows using IDataReader

[ASP .Net - Microsoft Visual Web Developer 2010]
Hi all,
I've problem with this code:
With MenuNavCatDataSource
Dim xReader As Data.IDataReader = .Select(DataSourceSelectArguments.Empty)
If xReader.Read Then
MenuNavCat1.Text = xReader.Item("MenuCategoryName")
MenuNavCat2.Text = xReader.Item("MenuCategoryName")
MenuNavCat3.Text = xReader.Item("MenuCategoryName")
MenuNavCat4.Text = xReader.Item("MenuCategoryName")
MenuNavCat5.Text = xReader.Item("MenuCategoryName")
End If
End With
I've 5 label and I want to parse the content of the label from the database. The database contains menus ordered from 1 to 5. And I want to show it on:
- 'MenuNavCat1' label, menu with order number 1 on database,
- 'MenuNavCat2' label, menu with order number 2 on database, and so on...
How to add where statement to the code, just like 'WHERE OrderNo = 1', and so on..?
Need your help guys..
Thank you so much.
EDIT
Here is the database:
Table MenuNavCategory
| Column Name | Data Type |
|-----------------------------|
| MenuNavCatID | int |
| CategoryName | varchar(20) |
| OrderNumber | int |
|-----------------------------|
And there is some value inserted to the table.
I want to show the 'CategoryName' onto some label.
For example:
lblCat1 | lblCat2 | lblCat3 | lblCat4
Then, using that xReader.Read, store the value on the database onto that label based on the OrderNumber...
The DataReader only reads one row of data at a time. If you have 5 labels then you should have 5 rows in your database. Then you iterate through each row using the DataReader and assign the labels:
With MenuNavCatDataSource
Dim xReader As Data.IDataReader = .Select(DataSourceSelectArguments.Empty)
'i is just a counter to keep track of what row we are on.
Dim i as integer = 1
If xReader.Read Then
Select Case i
Case 1 'First Row from DB.
MenuNavCat1.Text = xReader.Item("MenuCategoryName")
Case 2 'Second Row from DB... etc.
MenuNavCat2.Text = xReader.Item("MenuCategoryName")
Case 3
MenuNavCat3.Text = xReader.Item("MenuCategoryName")
Case 4
MenuNavCat4.Text = xReader.Item("MenuCategoryName")
Case 5
MenuNavCat5.Text = xReader.Item("MenuCategoryName")
x+=1
End If
End With
The above is just an example to try and explain how the DataReader works. I'd find another way to actually code it as the above relies on the rows in the database being in the correct order and is pretty ugly.

Resources