Click here to Skip to main content
15,881,797 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
this is my code but not work:-
C#
void CountTotal()
        {
            con.Open();
            s = "select Count(*) from ResrvationData where Date_Reservation = @Date_Reservation";
            sCommand = new SqlCommand(s , con);
            sCommand.Parameters.AddWithValue("@Date_Reservation", dateTimePicker_Time.Text);
            object count = sCommand.ExecuteScalar();
            if (count != null) txt_TotalBooks.Text = count.ToString();
            con.Close();
        }

private void FRM_Reservation_date_Load(object sender, EventArgs e)
        {
           CountTotal();
        }


What I have tried:

i want display count where data
Posted
Updated 15-Feb-16 20:30pm
v2
Comments
koolprasad2003 16-Feb-16 2:25am    
code looks fine, what is the issue ? have you got any error ? I think only conversion is need. see below
Int32 count = (Int32)sCommand.ExecuteScalar();
dan!sh 16-Feb-16 2:27am    
Not needed. OP is anyways going to cast it to string.
koolprasad2003 16-Feb-16 2:28am    
Yeah. not needed actually.
Member 12244977 16-Feb-16 2:35am    
when i change date textbox not changed , i want textbox change when i change date
koolprasad2003 16-Feb-16 2:46am    
When your date get changed, does your same code get called ? if it is then it should change the textbox contents, when you change the date what you get in 'dateTimePicker_Time.Text' ?

1 solution

when i change date , count not changed in textbox

Well - that's because you haven't told it to change.

The code you show calls the CountTotal method in the form Load event - and nowhere else. Which means that the SQL is executed only once: when the form is first loaded. Any changes you make via the DateTimePicker do not automatically update the count at all.
You could try calling the method in your DateTimePicker.ValueChanged Event (System.Windows.Forms)[^] handler.

But please, don't use the Text property of the DTP to check the DB - use the Value property which is already a DateTime directly, so there is no chance of miscommunication between SQL and your app as concerns data formats!
 
Share this answer
 

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