Click here to Skip to main content
15,890,336 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
for example :

command : select sum(Salary) from company i.e salary total=10000

i want to store the total sum into a variable X i.e X=10000

and i want to display that 1000 to textbox


C#
try
{
SqlConnection con = new SqlConnection(con_str);
con.Open();
 
SqlCommand cmd10 = new SqlCommand("select SUM(salary_amt) company ", con);
decimal x = cmd10.ExecuteNonQuery();
Label1.text="Total"+.ToString();
 
con.Close();
}
catch (Exception ex)
{
Response.Write(ex.Message);
}
Posted
Updated 11-Mar-15 2:13am
v2
Comments
[no name] 11-Mar-15 8:02am    
Did you try it and got stuck somewhere? Then please post the relevant parts of your code. You can use the "Improve question"-widget to edit your question.
10923679 11-Mar-15 8:06am    
try
{
SqlConnection con = new SqlConnection(con_str);
con.Open();



SqlCommand cmd10 = new SqlCommand("select SUM(salary_amt) company ", con);
decimal x = cmd10.ExecuteNonQuery();
Label1.text="Total"+x.ToString();




con.Close();
}
catch (Exception ex)
{
Response.Write(ex.Message);

}

Both Solution 1 and Solution 2 provide you with links that lead you to some example code that will work for you.

As an exaplanation why your code is not working - this:
C#
cmd10.ExecuteNonQuery();

doesn't return anything but the number of rows affected by the query, hence "ExecuteNonQuery". It's usually used for Update- and Delete-Statements. To query something where you expect a query result (a Select-Query) you have to use ExecuteReader(..), as you can see in the linked examples of Solution 1 and 2.
 
Share this answer
 
Comments
Praveen Kumar Upadhyay 11-Mar-15 8:37am    
He has not added the code when I replied. thanks
[no name] 11-Mar-15 8:41am    
Yeah, you were probably typing while he added the code, the time difference is 1 minute :-)
10923679 11-Mar-15 8:52am    
you all have help me .. thank you so much
Praveen Kumar Upadhyay 11-Mar-15 8:53am    
Welcome
10923679 11-Mar-15 8:54am    
i need to know abt this ...

can you please help me with this







for example :

command1 : select sum(B1) from company i.e total=10000

command2: select sum(B2) from company i.e total=6000

command3 : select sum(B3) from company i.e total=500

i want to store the total B1 into a variable X i.e X=10000

total B2 into a variable Y i.e Y=6000

total B3 into a variable Z i.e Z=5000
if(b2 and b3 =0)
{
make payment for b1
}

if(b1 and b3 =0)
{
make payment for b2
}
if(b1 and b2 =0)
{
make payment for b3
}
Have look Here
 
Share this answer
 
Comments
10923679 11-Mar-15 8:26am    
This is right.. it is working ...
C#
     try 
	{	        
		SqlConnection con = new SqlConnection(con_str);
        SqlDataAdapter adapterDa = new SqlDataAdapter("select SUM(salary_amt) as totalSalary from company", con);
        con.Open();
        DataSet dsData = new DataSet();
        adapterDa.Fill(dsData);
        Label1.text="Total"+ dsData.Tables[0].Rows[0]["totalSalary"].ToString();
        con.Close();
	}
	catch (Exception ex)
{
Response.Write(ex.Message);
}


Try this code
 
Share this answer
 
v3

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