Last post Apr 04, 2011 07:01 PM by hajan
Apr 03, 2011 02:09 PM|hknpr|LINK
I m trying to get a value in a database. For example, in my database, in the patient table, I have a location_code column and the location_code can be 1, 2, or 3.
I want to get this values from the database and count how many patients are in such locations. Can you give me an advise how to do?
Apr 03, 2011 02:50 PM|akhhttar|LINK
There could be couple of ways to achive the desired functionality , based on the data access framework you are using.
all you need is
1. Create a stored procedure in database, which should take Location_Code as input parameter and return the patient list.
2. Create a connection with your databse in your application.
3. Create stored procedure command and provide location_code value in parameter.
4. Execute the command and Get the results.
Please have look at this article for code http://support.microsoft.com/kb/310070
Let me know if you have any question.
Apr 03, 2011 03:38 PM|hknpr|LINK
I just want to read the data row by row and compare with if statement. In that article, it just gives row number.
I have a button in my application and when I click, it will show the statistics: how many patient in location 1, how many in 2, and how many in 3.
Can anyone help me about it?
Apr 04, 2011 01:50 PM|akhhttar|LINK
I would like to suggest you to go through some getting started ado.net/entity framework tutorial, Following vidoes could be a helpful for you to get start work with Entity Framework and to get an understanding of how to implement the required functionality,
Apr 04, 2011 05:26 PM|hajan|LINK
Well, since I don't have your database table schema and the current implementation you use, I have made something that might be similar. Have made this scenario in order to help you change the inside logic and make your own one work.
As I know, this is second query after the one which is resolved in the other thread. So, if you use the same code, you have the data in
DataTable and then add the DataTable to a GridView or any other data binding control.
So, now, if you want to use the data in the DataTable to get some statistics from it, you can do this on the following way (this is sample code working with one of my db tables):
protected void SearchByTagButton_Click(object sender, EventArgs e)
String strConn = "Data Source=.;Initial Catalog=MyTestDatabase;Integrated Security=True";
SqlConnection conn = new SqlConnection(strConn);
SqlCommand cmd = new SqlCommand("Select * FROM [Childs] WHERE Gender = @SearchByTagTB", conn);
SqlParameter search = new SqlParameter();
search.ParameterName = "@SearchByTagTB";
search.Value = SearchByTagTB.Text.Trim();
SqlDataReader dr = cmd.ExecuteReader();
DataTable dt = new DataTable();
//THIS IS THE NEW CODE BLOCK ADDED FROM THE PREVIOUS CODE
int lessThen10 = 0;
int between10and20 = 0;
int greaterThan20 = 0;
foreach (DataRow tDr in dt.Rows)
if (tDr["Years"] != null)
int val = Convert.ToInt32(tDr["Years"]);
if (val < 10)
else if (val >= 10 && val < 20)
gvPatients.DataSource = dt;
lblYearsInfo.Text = "Less then 10:" + lessThen10 + ", Between 10 and 20:" + between10and20 + ", Greater than 20:" + greaterThan20; //PRINT THE RESULT IN LABEL
catch (Exception ex)
//Connection Object Closed
So, I basically loop each row in the Data Table, and get the row Years. Then I make some inner comparison logic and increment the appropriate predefined counter. At the end I show the result.
If I understood your query well, you should do something similar with your own logic.
Hope this helps.
Apr 04, 2011 06:41 PM|hknpr|LINK
Thank you so much Hajan, It works right now thanks again
Apr 04, 2011 07:01 PM|hajan|LINK
Always welcomed! ;)