473,387 Members | 1,379 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,387 software developers and data experts.

sql statement (correction)

jed
string selectstr = "SELECT SUM(hrs) AS total_hours,SUM(mins)AS
total_mins FROM Timerecords WHERE (workdate >='" +
Convert.ToDateTime(dateTimePickerfirst.Text) + "')AND (workdate<='" +
Convert.ToDateTime (dateTimePickersecond.Text) + "')";

I have two datepickerboxs that i want to select dates in.I want the
selectstr string to recieve the values of the the calculated hours and
the calculated minutes but it must only sum up the values between the
two dates that i select in the datepickerboxs.

I dont receive the correct values from total_hours and total_mins.It
doesnt add the the values between the the two dates.I cant work out
where the error is.What is the best way to insert datetime into an SQL
statement.

Jul 3 '07 #1
3 1259
je*@auto-soft.co.za wrote:
string selectstr = "SELECT SUM(hrs) AS total_hours,SUM(mins)AS
total_mins FROM Timerecords WHERE (workdate >='" +
Convert.ToDateTime(dateTimePickerfirst.Text) + "')AND (workdate<='" +
Convert.ToDateTime (dateTimePickersecond.Text) + "')";

I have two datepickerboxs that i want to select dates in.I want the
selectstr string to recieve the values of the the calculated hours and
the calculated minutes but it must only sum up the values between the
two dates that i select in the datepickerboxs.

I dont receive the correct values from total_hours and total_mins.It
doesnt add the the values between the the two dates.I cant work out
where the error is.What is the best way to insert datetime into an SQL
statement.
Hi,

What do you mean the correct values? Can you post an example of the query
in action, giving some sample tuples, and stating what the output you
expect is, and what the output you're getting is?

Thanks!
--
Tom Spink
University of Edinburgh
Jul 3 '07 #2
Howdy,

Use parameters instead of passing date as string. In addition, you could use
Profiler to see what's beeing run on the sql server side.

string selectstr =
"SELECT SUM(hrs) AS total_hours,SUM(mins) AS total_mins " +
"FROM Timerecords WHERE (workdate between @startDate and @endDate)";

SqlConnection connection = new SqlConnection(ConnectionSstring));
SqlCommand command = new SqlCommand(selectstr, connection);
SqlDataReader reader = null;

command.Parameters.Add("@startDate", SqlDbType.DateTime).Value =
DateTime.Parse(dateTimePickerfirst.Text);
command.Parameters.Add("@endDate", SqlDbType.DateTime).Value =
DateTime.Parse(dateTimePickersecond.Text);

try
{
connection.Open();

reader = command.ExecuteReader();

while (reader.Read())
{
// whateever
}
}
catch
{
throw;
}
finally
{
if (reader != null)
reader.Close();
connection.Dispose();
}

Hope thus helps
--

Milosz
"je*@auto-soft.co.za" wrote:
string selectstr = "SELECT SUM(hrs) AS total_hours,SUM(mins)AS
total_mins FROM Timerecords WHERE (workdate >='" +
Convert.ToDateTime(dateTimePickerfirst.Text) + "')AND (workdate<='" +
Convert.ToDateTime (dateTimePickersecond.Text) + "')";

I have two datepickerboxs that i want to select dates in.I want the
selectstr string to recieve the values of the the calculated hours and
the calculated minutes but it must only sum up the values between the
two dates that i select in the datepickerboxs.

I dont receive the correct values from total_hours and total_mins.It
doesnt add the the values between the the two dates.I cant work out
where the error is.What is the best way to insert datetime into an SQL
statement.

Jul 3 '07 #3
On Jul 3, 10:51 am, j...@auto-soft.co.za wrote:
string selectstr = "SELECT SUM(hrs) AS total_hours,SUM(mins)AS
total_mins FROM Timerecords WHERE (workdate >='" +
Convert.ToDateTime(dateTimePickerfirst.Text) + "')AND (workdate<='" +
Convert.ToDateTime (dateTimePickersecond.Text) + "')";

I have two datepickerboxs that i want to select dates in.I want the
selectstr string to recieve the values of the the calculated hours and
the calculated minutes but it must only sum up the values between the
two dates that i select in the datepickerboxs.

I dont receive the correct values from total_hours and total_mins.It
doesnt add the the values between the the two dates.I cant work out
where the error is.What is the best way to insert datetime into an SQL
statement.
The same answer as to yesterday's question: use a parameterised query.

See http://pobox.com/~skeet/csharp/faq/#db.parameters

Jon

Jul 3 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: soni29 | last post by:
hi, i have a small question regarding sql, there are two tables that i need to work with on this, one has fields like: Table1: (id, name, street, city, zip, phone, fax, etc...) about 20 more...
6
by: Karen Middleton | last post by:
In MS Access I can do in one SQL statement a update if exists else a insert. Assuming my source staging table is called - SOURCE and my target table is called - DEST and both of them have the...
22
by: Phil Powell | last post by:
<script> <!-- function isValidAlert() { for (var i = 0; i < document.alertForm.length; i++) { with (document.alertForm.elements) { if (.name == "text" || .name == "password" || .name ==...
5
by: DU | last post by:
Hi! I really need to understand what is so-called browser error correction mechanisms. Can you explain this? I read somewhere (and I no longer can find where I read that) that browsers try to...
0
by: Sori Schwimmer | last post by:
"i += 1" is wrong there. I had in my mind at first a "while" statement. Sorry... Sorin
3
by: Sori Schwimmer | last post by:
0) Sorry, I don't know how to post a reply in the same thread. 1) Grant Edwards wrote: > The "i += 1" line is almost certainly wrong. You're certainly write, as I acknowledged in a follow up...
13
by: jimjim | last post by:
Hello all, I ve come across the following code fragment and I was wondering why is the copy ctr called on return (rather than just returning the string statement obj. TIA string...
19
by: Steve | last post by:
ASP error number 13 - Type mismatch with SELECT...FOR UPDATE statement I got ASP error number 13 when I use the SELECT...FOR UPDATE statement as below. However, if I use SELECT statement without...
18
by: dspfun | last post by:
Hi! The words "expression" and "statement" are often used in C99 and C- textbooks, however, I am not sure of the clear defintion of these words with respect to C. Can somebody provide a sharp...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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:
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
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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.