Binding data from dateTimePicker to a variable

Please support our C# advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Feb 2009
Posts: 3,473
Reputation: sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of 
Solved Threads: 629
Sponsor
sknake's Avatar
sknake sknake is offline Offline
.NET Enthusiast
 
0
  #11
Oct 22nd, 2009
Actually post the complete code. Are you using an OleDb or ODBC driver? OleDb supports named parameters depending on the driver but ODBC does not. Post where you are instantiating the database connection.
Scott Knake
Custom Software Development
Apex Software, Inc.
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 3,473
Reputation: sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of 
Solved Threads: 629
Sponsor
sknake's Avatar
sknake sknake is offline Offline
.NET Enthusiast
 
1
  #12
Oct 22nd, 2009
Oh, try something like this:

  1. private void button2_Click(object sender, EventArgs e)
  2. {
  3. const string connStr = "Data Source=apex2006sql;Initial Catalog=Leather;Integrated Security=True;";
  4. const string query = "Select * From Invoice Where (InHomeDate BETWEEN @Date1 and @Date2)";
  5. using (SqlConnection conn = new SqlConnection(connStr))
  6. {
  7. conn.Open();
  8. using (SqlCommand cmd = new SqlCommand(query, conn))
  9. {
  10. cmd.Parameters.Add(new SqlParameter("@Date1", SqlDbType.DateTime)).Value = DateTime.Today.AddDays(-180);
  11. cmd.Parameters.Add(new SqlParameter("@Date2", SqlDbType.DateTime)).Value = DateTime.Today;
  12. using (SqlDataReader dr = cmd.ExecuteReader())
  13. {
  14. DataTable dt = new DataTable();
  15. dt.Load(dr);
  16. dataGridView1.DataSource = dt;
  17. }
  18. }
  19. }
  20. }
Scott Knake
Custom Software Development
Apex Software, Inc.
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 14
Reputation: beginerC# is an unknown quantity at this point 
Solved Threads: 0
beginerC# beginerC# is offline Offline
Newbie Poster
 
0
  #13
Oct 22nd, 2009
Originally Posted by sknake View Post
Oh, try something like this:

  1. private void button2_Click(object sender, EventArgs e)
  2. {
  3. const string connStr = "Data Source=apex2006sql;Initial Catalog=Leather;Integrated Security=True;";
  4. const string query = "Select * From Invoice Where (InHomeDate BETWEEN @Date1 and @Date2)";
  5. using (SqlConnection conn = new SqlConnection(connStr))
  6. {
  7. conn.Open();
  8. using (SqlCommand cmd = new SqlCommand(query, conn))
  9. {
  10. cmd.Parameters.Add(new SqlParameter("@Date1", SqlDbType.DateTime)).Value = DateTime.Today.AddDays(-180);
  11. cmd.Parameters.Add(new SqlParameter("@Date2", SqlDbType.DateTime)).Value = DateTime.Today;
  12. using (SqlDataReader dr = cmd.ExecuteReader())
  13. {
  14. DataTable dt = new DataTable();
  15. dt.Load(dr);
  16. dataGridView1.DataSource = dt;
  17. }
  18. }
  19. }
  20. }
Hey!! Thanks a lot, it works! \:d/ - I get no error
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 14
Reputation: beginerC# is an unknown quantity at this point 
Solved Threads: 0
beginerC# beginerC# is offline Offline
Newbie Poster
 
0
  #14
Oct 22nd, 2009
the code was
  1. mySqlCommand.Parameters.Add(new SqlParameter("@date1", SqlDbType.DateTime));
  2. mySqlCommand.Parameters["@date1"].Value = dateTimePicker1.Value;
for each one @date
Reply With Quote Quick reply to this message  
Join Date: Jul 2009
Posts: 972
Reputation: DdoubleD is a jewel in the rough DdoubleD is a jewel in the rough DdoubleD is a jewel in the rough DdoubleD is a jewel in the rough 
Solved Threads: 213
DdoubleD DdoubleD is offline Offline
Posting Shark
 
0
  #15
Oct 22nd, 2009
Please mark as SOLVED unless you are still having trouble.
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 14
Reputation: beginerC# is an unknown quantity at this point 
Solved Threads: 0
beginerC# beginerC# is offline Offline
Newbie Poster
 
0
  #16
Nov 3rd, 2009
Sorry, i forgot to mark this as solved.
Thanks a lot, sknake!
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:




Views: 674 | Replies: 15
Thread Tools Search this Thread



Tag cloud for C#
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC