Asp.Net News
Free Tech Magazines
Asp.Net Web Hosting
Suggest Us
Link to Us
Feeds Subscription
Questions & Answers
Tips & Tricks
 querry returning zero ,even though data is present in data base
Posted by charan on  6/15/2010 8:31:14 AM Category: Asp.Net 2.0
Add to my favorites
Email to friend

the querry is working fine in sql server but in it is giving result zero

i have check all connection string,session, also every think is fine
using breakpoint also i have checked it is returning zero
can you correct my code which helps me

SqlConnection con = new SqlConnection(strcon);
DateTime dt = Convert.ToDateTime(txtfrom.Text);
string fromDate = dt.ToShortDateString();
DateTime dt1 = Convert.ToDateTime(txtto.Text);
string toDate = dt1.ToShortDateString();

SqlCommand cmdallltransactions = new SqlCommand("select sum(Amount) from AllTransactions where (TransCode <> 0) and (BranchKey = @BranchKey) and ((TransDate > @FromDate )and (TransDate < @ToDate)) GROUP by BranchKey", con);
cmdallltransactions.CommandType = CommandType.Text;
cmdallltransactions.Parameters.Add(new SqlParameter("@BranchKey", Convert.ToInt32(Session["BranchKey"])));
cmdallltransactions.Parameters.Add(new SqlParameter("@FromDate", fromDate));
cmdallltransactions.Parameters.Add(new SqlParameter("@ToDate",toDate));


decimal tot = 0;
tot = Convert.ToDecimal(cmdallltransactions.ExecuteScalar());
txttotal.Text = tot.ToString();

Post Your Reply Post New Question

 Viewer's Reply
SqlCommand ExecuteScalar Method Tips
Answered By Moderator1 on 6/15/2010 9:33:00 PM
There is a few suggestion for you,
1. SqlCommand can convert a string to its parameter datatype directly. So no need for explicit conversion.
2. To get records between a date range, always use BETWEEN instead of '>' & '<' operators.
uqerry is retuning still zero
Answered By charan on 6/16/2010 12:12:58 AM
hi, iam using 2.0 with sql server 2000

i have used the condition between also in querry and < & > operators but both are returning same .but the same querry when iam running in sql it is returning results
can u give example which helps me
ExecuteScalar in SqlCommand
Answered By Moderator1 on 6/16/2010 9:12:35 AM

I found no problem in your query or coding procedure. It simply fine. Can you add Prepare() method or debug this query through SQL Profiler to find exactly what input is passing through SQL Query.
Post Your Reply If you still have problem, post it again under this reply for better response.

Sponsored by
The JavaScript Anthology: 101 Essential Tips, Tricks & Hacks - Free 158 Page Preview
Many .NET teams are underperforming, is yours?
The Beginner's Guide to Joomla