Click here to Skip to main content
15,886,362 members
Please Sign up or sign in to vote.
1.33/5 (2 votes)
See more:
var connectionString = ConfigurationManager.ConnectionStrings["newproject"].ConnectionString;
SqlConnection cn = new SqlConnection(connectionString);
cn.Open();
string commandString = "select SUM(termfee ) from newstd_termfee where admin_no='" + txtadmin .Text + "'";

SqlCommand sqlCmd = new SqlCommand(commandString, cn);
SqlDataReader read = sqlCmd.ExecuteReader();
while (read.Read())
{
textBox2.Text = read["termfee"].ToString();
}
cn.Close();
Posted
Updated 22-Sep-14 19:36pm
v2
Comments
KaushalJB 23-Sep-14 1:53am    
What error does it shows ?

Please Replace this Query to your Query.. Defiantly it will Work..

SQL
select SUM(termfee) as termfee from newstd_termfee where admin_no='" + txtadmin .Text + "'"
 
Share this answer
 
C#
var connectionString = ConfigurationManager.ConnectionStrings["newproject"].ConnectionString;
SqlConnection cn = new SqlConnection(connectionString);
cn.Open();
string commandString = "select SUM(termfee ) as Total from newstd_termfee where admin_no='" + txtadmin .Text + "'";
 
SqlCommand sqlCmd = new SqlCommand(commandString, cn);
SqlDataReader read = sqlCmd.ExecuteReader();
while (read.Read())
{
textBox2.Text = read["Total"].ToString();
}
cn.Close();
 
Share this answer
 
Do like this.

TextBox2.Text = read[0].ToString();
 
Share this answer
 
Comments
Member 11065510 23-Sep-14 2:10am    
thank u so much kalyani bibin..........its working........
KALYANI BIBIN 23-Sep-14 2:21am    
k welcome..
KALYANI BIBIN 23-Sep-14 2:46am    
Please accept one of the solutions so that the qustion is marked answered. Thank you.
Sinisa Hajnal 23-Sep-14 2:16am    
Solution 2 is better, you will not have problems when changing / adding new fields.

Also, never trust user input, clean the text before concatenating to your command. Consider this:
What will happen if someone enters in txtAdmin following text (without brackets) ['; DROP TABLE newstd_termfee; --]
Single quote will close your query, semicollon will mark the end of the command, drop table will delete your table and double minus will comment out the rest of your command.

Read about SQL Injection attacks. Use parametrized query or better yet, stored procedure. Even then, do Server.HtmlEncode and / or replace critical characters (such as single quotes and double minus)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900