Binding data from dateTimePicker to a variable

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

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

Binding data from dateTimePicker to a variable

 
0
  #1
Oct 21st, 2009
Hi!
I have a form with two dateTimePickers (first with date of one month ago, and the secod with current date), a dataGridView and a button.
When i click the button i want to populate the dataGridView, but only between date1 and date2
  1. mySqlCommand = con.CreateCommand();
  2. mySqlCommand.CommandText = "SELECT * FROM MyTable WHERE (MyDate BETWEEN @date1 AND @date2)";
  3. mySqlCommand.Parameters.Add("@date1", SqlDbType.Char);
  4. mySqlCommand.Parameters["@date1"].Value = dateTimePicker1.Text.ToString();
  5. mySqlCommand.Parameters.Add("@date2", SqlDbType.Char);
  6. mySqlCommand.Parameters["@date2"].Value = dateTimePicker2.Text.ToString()
I get this error "Must declare the scalar variable "@date1".
Can anyone help me, please?
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: 630
Sponsor
sknake's Avatar
sknake sknake is offline Offline
.NET Enthusiast
 
0
  #2
Oct 21st, 2009
What type of SQL Connection is that? MSSQL? MySQL? Why are you declaring the command parameter as a char if it is a date value?
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
  #3
Oct 21st, 2009
Originally Posted by sknake View Post
What type of SQL Connection is that? MSSQL? MySQL? Why are you declaring the command parameter as a char if it is a date value?
* i use MSSQL
* in the table mydate is declared as varchar(10) - i think i can change it
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: 630
Sponsor
sknake's Avatar
sknake sknake is offline Offline
.NET Enthusiast
 
0
  #4
Oct 22nd, 2009
You need to change it. Using the >= and <= operators on strings doesn't always work the same as dates...
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
  #5
Oct 22nd, 2009
Originally Posted by sknake View Post
You need to change it. Using the >= and <= operators on strings doesn't always work the same as dates...
OK, i change it
but i get the same error: Must declare the scalar variable "@date1".
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: 630
Sponsor
sknake's Avatar
sknake sknake is offline Offline
.NET Enthusiast
 
0
  #6
Oct 22nd, 2009
With mySQL you need to use "?" for parameter names. Try changing this line:
  1. mySqlCommand.CommandText = "SELECT * FROM MyTable WHERE (MyDate BETWEEN @date1 AND @date2)";

To:
  1. mySqlCommand.CommandText = "SELECT * FROM MyTable WHERE (MyDate BETWEEN ? AND ?)";
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
  #7
Oct 22nd, 2009
Originally Posted by sknake View Post
With mySQL you need to use "?" for parameter names. Try changing this line:
  1. mySqlCommand.CommandText = "SELECT * FROM MyTable WHERE (MyDate BETWEEN @date1 AND @date2)";

To:
  1. mySqlCommand.CommandText = "SELECT * FROM MyTable WHERE (MyDate BETWEEN ? AND ?)";
ERROR: Incorrect syntax near '?'.
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: 630
Sponsor
sknake's Avatar
sknake sknake is offline Offline
.NET Enthusiast
 
0
  #8
Oct 22nd, 2009
I give up

I don't know anything about mySQL. Sorry I couldn't be of more help. I try to stick with MSSQL.
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
  #9
Oct 22nd, 2009
Originally Posted by sknake View Post
I give up

I don't know anything about mySQL. Sorry I couldn't be of more help. I try to stick with MSSQL.
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
  #10
Oct 22nd, 2009
I use MSSQL ... not MySQL
Reply With Quote Quick reply to this message  
Reply

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




Views: 675 | 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