Click here to Skip to main content
15,891,136 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
SQL
select sum(amountpaid) from tbl_payments where uid=29

it is getting null

how to check the condition
i am trying like this

if(cmd.executescaler()==null)

but it is generating the

invalid cast dbnull cannot be cast to other types.

C#
SqlCommand cmd = new SqlCommand("select sum(isnull(amountpaid,0)) from tbl_payments tbl_payments where uid=" + Label2.Text, con);
                amount = Convert.ToInt64(cmd.ExecuteScalar());
                if(amount!=0)
                 {



when the scalar sum funtion returing null.it is getting the error like cannot cast from dbnull to other types
Posted
Updated 15-Apr-12 19:48pm
v3
Comments
Nilesh Patil Kolhapur 16-Apr-12 1:58am    
hey check my updated solution.
u can use Both Way k hope ur problem is solved now k
happy coding

you can try like this
SQL
select isnull(sum(isnull(amountpaid,0)),0) from tbl_payments where uid=29
 
Share this answer
 
v3
Comments
[no name] 16-Apr-12 1:36am    
i am getting the same error
Vipin_Arora 16-Apr-12 1:41am    
Hi dipesh,
I agree but... what if uid=29 does not exist?? then you have to handle it on front end only..
Dipesh202 16-Apr-12 2:10am    
then you can use this
select isnull(sum(isnull(amountpaid,0)),0) from tbl_payments where uid=29
Hi,
you can assign this cmd execute to any integer variable,and check that condition like this.

int result = Convert.ToInt32(cmd.ExecuteScalar());
if(result==0)
{

}
 
Share this answer
 
v2
Comments
[no name] 16-Apr-12 1:36am    
i am getting the same error
Hi,

You can following code:
C#
string ConString = ConfigurationManager.ConnectionStrings["ConString"].ConnectionString;
SqlConnection con = new SqlConnection(ConString);
string CmdString = "select sum(isnull(amountpaid,0)) from tbl_payments where uid=@UID";
SqlCommand cmd = new SqlCommand(CmdString, con);
cmd.Parameters.AddWithValue("@UID", 5734999994);
con.Open();
double AmountPaid;
if (cmd.ExecuteScalar() != null)
{
    AmountPaid = Convert.ToDouble(cmd.ExecuteScalar());
}
con.Close();
 
Share this answer
 
v2
Hi ,
Try this make stored procedure
SQL
CREATE PROCEDURE usp_test
(@id int )
AS
BEGIN
SET NOCOUNT ON;
declare @count int ;
SELECT @count = (SELECT SUM(amountpaid) from  tbl_payments where  uid=@id)
IF(@count is null)
BEGIN
SET @count = -1;
END
 select @count;
END


In your code behind
C#
int sum=Convert.TOInt64(cmd.executescaler().tostring());
if(sum == -1)
{
//do something 
}
else
{
//something else
}

Best Regards
M.Mitwalli
 
Share this answer
 
Hi,

You can try this

SQL
select sum(isnull(amountpaid,0)) as [sum]  from tbl_payments where uid=29


and in aspx.cs file write following code

C#
----------------------First Solution-----------------------------

  if (Cmd.ExecuteScalar().GetType().ToString() != "System.DBNull")
 {
    // your code
 }

----------------------Second Solution----------------------------

DataTable dt=new DataTable();
SqlDataAdapter da=new SqlDataAdapter(cmd);
da.Fill(dt);
if(dt.Rows.Count > 0)
{
   int sum=Convert.TOInt64(dt.Rows[0][0].ToString());
   // Your Code
}


Check It Out

Best Luck
Happy Coding :)
 
Share this answer
 
v5
Comments
[no name] 16-Apr-12 1:44am    
input strng was not in correct format
[no name] 16-Apr-12 1:45am    
i am getting above error
Nilesh Patil Kolhapur 16-Apr-12 1:49am    
is any record present in table for uid=29 ?
give rpy

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