Click here to Skip to main content
16,022,296 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
I want to display event in my calendar. Please take a look the image below.
Image1 [^]

I'm using usercontrol to show date. Please tale a look the image below.
Image2[^]

Now I want to display the event, from my MySql to my calendar.

What I have tried:

I try my code, I insert this code into UserControlDays.

public void DisplayEvent()
        {
            MySqlConnection conn = new MySqlConnection(connString);
            conn.Open();

            String sql = "SELECT * FROM db_booking WHERE dateFrom = ?";
            MySqlCommand cmd = conn.CreateCommand();
            cmd.CommandText = sql;
            cmd.Parameters.AddWithValue("dateFrom", lblevent.Text);
            MySqlDataReader reader = cmd.ExecuteReader();
            if (reader.Read())
            {
                lblevent.Text = reader["customerID"].ToString();
            }
            reader.Dispose();
            cmd.Dispose();
            conn.Close();
        }



and not working for me, Please help me how to show event in my calendar.
Posted

1 solution

Since we know nothing about your UserControlDays, it's impossible to tell you what's going wrong.

But, It would not be a good idea to put the database code in the user control because, from what you've posted, you've got each Day control making a query to the database. If all 30-ish days are querying the database, that's a bit overkill.

It would be better to just query the database once to get all the events for either the month or the year, in your application code, and supply that data to the calendar. Each day can then query the parent calendar for any data in the day its responsible for, and you've only queried the database one time.
 
Share this answer
 
Comments
ras bry 13-Jul-24 18:05pm    
Good day Dave Kreskowiak, My problem is I cannot display data from my DB.
Dave Kreskowiak 13-Jul-24 21:44pm    
And again, since you haven't given ANY details at all about how your control is constructed and coded, it's impossible for anyone to tell you what's wrong.

Oh, and you're not disposing the connection object, so you're leaking resources.
Dave Kreskowiak 13-Jul-24 21:49pm    
You have to run this in the debugger and step through the code line-by-line. It's possible you're not passing in the date parameter correctly, giving you 0 records returned. It's possible you're getting back more than one record, but the data you're trying to show in the lblevent control is getting an empty string or null. Your code doesn't build a string of all possible customerID's returned. It's constantly replacing the content in the lblevent control until there is no more data to read.

Now, are the dates stored in the database as strings or an actual date type?

Since you're passing the date into the parameter as a string, that will not work for a date type in the database. You're passing the date you're searching for as a string, not as a proper date. But, I don't do MySQL, so I may be wrong here.

Again, the debugger is what is going to show you what's going on.

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