473,385 Members | 2,044 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

how to work average with value from sql in c sharp

dear I.T. lovers

I'm in need of assistance ,currently I'm working an Questionnaire solution project that consists inputting results of a questionnaire(saved in sql database) and then an average of certain questions is given.

I'm having trouble when i need to work the average of the questions

one of my question is there are seven text boxes that cover ages from 0 till 60 that the user has to input in the text boxes
the amount of people that are in a certain group of age.(sort of like below)

0-10 [text box]<--enter amount of people that the user has in the family of that group
11-20 [text box]
21-30 [text box]
31-40 [text box]
41-50 [text box]

the code that i have worked till now is the one below.i think the trouble is where i placed the arrows

Expand|Select|Wrap|Line Numbers
  1. //Connection string
  2. SqlConnection MyConnection = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|dbQuestionaireSolution.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True");
  3.  
  4.  
  5.           string av1 = "SELECT[0-10]FROM Quest3 WHERE (Region = 'NorthernDistrict')";
  6.  
  7.           SqlCommand MysqlCommand =new SqlCommand(av1, MyConnection);
  8.             MyConnection.Open();
  9.             SqlDataReader reader = MysqlCommand.ExecuteReader();
  10.  
  11.             if (reader.Read())
  12.             {
  13.               int ND1 = Int32.Parse(av01); <-------
  14.              // this.textBox1ND.Text = reader["0-10"].ToString();<------
  15.               //int ND1 = int.Parse(textBox1ND.Text);<-----
  16.  
  17.               for (int i = ND1; i == null; i++)
  18.               {
  19.  
  20.                int total = ((i + i)/int.MaxValue);
  21.                total = Convert.ToInt32(textBox1ND.Text);
  22.  
  23.               }
  24.  
pls in urgent need of help thanx a lot
Feb 11 '11 #1
14 3566
Rabbit
12,516 Expert Mod 8TB
Why are you parsing the SQL string? That doesn't make any sense.

If you're trying to get an average, why don't you just use SQL?
Expand|Select|Wrap|Line Numbers
  1. SELECT AVG([FieldName]) FROM Table1
Feb 11 '11 #2
how do i display the value in a textbox

thanx
Feb 11 '11 #3
Rabbit
12,516 Expert Mod 8TB
You would display it how you currently display it. You would just be grabbing the single value instead of trying to step through a recordset and calculating the average.
Feb 11 '11 #4
can you please show me a syntax example on how it should be
Feb 12 '11 #5
Rabbit
12,516 Expert Mod 8TB
You already have an example in your code.
Expand|Select|Wrap|Line Numbers
  1. this.textBox1ND.Text=reader["0-10"].ToString
The only thing you would have to change is the field name to the alias you give the count.
Feb 12 '11 #6
when you said"the field name to the alias you give the count"

the field name is this part ------"0-10"
WHAT is did you ment exactly by"the alias you give the count" (WHAT IS THE COUNT)
Feb 12 '11 #7
Rabbit
12,516 Expert Mod 8TB
Mistype, I meant average.
Feb 12 '11 #8
This is the code how i made it that came without errors

Expand|Select|Wrap|Line Numbers
  1.  
  2. SqlConnection MyConnection = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=C:\Users\Rebecca\Desktop\Project\dbQuestionaireSolution.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True");
  3.                 string av1 = "SELECT AVG([0-10])FROM Quest3 ";
  4.                 SqlCommand MysqlCommand =new SqlCommand(av1, MyConnection);
  5.                     MyConnection.Open();
  6.                     SqlDataReader reader = MysqlCommand.ExecuteReader();
  7.                      if (reader.Read())
  8.                     {
  9.                         //this.textBox1ND.Text = reader["0 - 10"].ToString();
  10.                      }
  11.  

The comented part is the only part that is still giving me the error below
“0 – 10 IndexOutOfRangeExeption was unhendled”
What can i do now for the final push??

Thanx
Feb 12 '11 #9
Rabbit
12,516 Expert Mod 8TB
You just need to give the average an alias and use the alias instead of 0-10.
Feb 12 '11 #10
i m not geting the concept of the alias can you give me a syntax example? because i'm not geting the hang of it
Feb 12 '11 #11
Rabbit
12,516 Expert Mod 8TB
Something like Avg(somefield) As AvgOfSomefield.
Feb 13 '11 #12
do i have to create the AvgOfSomefield as a variable ? i think i'm not doing it correct
Feb 13 '11 #13
Try this, u might come to know use of Alias...Alias is custom name given to Column or SQL table...Have look, u will understand...

# /Connection string
# SqlConnection MyConnection = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirector y|dbQuestionaireSolution.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True");
#
#
# string av1 = "SELECT AVERAGE([0-10]) AS AverageAge FROM Quest3 WHERE (Region = 'NorthernDistrict')";
#
# SqlCommand MysqlCommand =new SqlCommand(av1, MyConnection);
# MyConnection.Open();
# SqlDataReader reader = MysqlCommand.ExecuteReader();
#
# if (reader.Read())
# {
this.textBox1ND.Text=reader["AverageAge"].ToString();
#
# }
#

Hope this will solve ur problems
Feb 13 '11 #14
it
worked perfectly thanx for your help and patience
Feb 13 '11 #15

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: Raider | last post by:
I'm trying to get average value. My first attempt is: #include <...> template <typename T> struct avg : public unary_function<T, void> { T sum, count; avg() : sum(0), count(0) {} void...
11
by: jesper_lofgren | last post by:
Hello, I need to write a function to calculate the average value from a coupe of parameters from database. Question 1 (value) (people that answer the specific value) 5 ...
4
by: Scott | last post by:
In order to give a meaning average value and minimum and maximum values, I would like to have a formula for a group of data after taking out those extremes. Can someone share your way to...
1
by: Theadmin77 | last post by:
Well ...this is a real challenge .....i got everything else working OK...but ... I have to get the average and maximum value out of a group of people thru two functions .I have problems passing...
10
by: blackflicker | last post by:
Hello, I have a table which is: DROP TABLE IF EXISTS dummy; CREATE TABLE dummy ( id int(11) not null auto_increment, entered int(11) not null default 0, primary key(id) ); And dummy...
0
by: anniebai | last post by:
in my SQL code: declare @counter int declare @parm nvarchar(20) declare @avg float set @counter=0 while @counter<10 set @counter=@counter+1
3
by: vileoxidation | last post by:
Hello, and thanks for any help in advance! Basically, as the title says, I am looking for a way to print the number of times the user gave the program an input, and then also print the average of...
12
by: jpjacquez | last post by:
There are 4 fields in this input data. I want to get the average value for the column 2/3/4th. If you notice, disk c53t2d6 and c12t10d4 occured twice..so i need to get an average for them. If it...
1
by: stephen04 | last post by:
dear all i need to use the sql avg() query but i dont know how to use it or even how to display the value in a text box. can any one give me some tips. thanx
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.