Click here to Skip to main content
15,886,815 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have this code
C#
<pre>private void timer2_Tick(object sender, EventArgs e)
        {
//start time is another DateTime.now
//timeSinceStart is an array of timeSpan
//i believe that the for loop is for me to be able to create and store many values for //the timespan and update the listview items(see link below for the photo of the form)
            for (int w = 0; w < listView1.Items.Count; w++)
            {
                timeSinceStart[w] = DateTime.Now - startTime[w];
                listView1.Items[w].SubItems[6].Text = (timeSinceStart[w].TotalSeconds.ToString());
                if (f.ToString() == listView1.Items[w].SubItems[6].Text)
                {
//you can ignore the mysql codes here
//f.ToString() is default 60(its an integer)
                    string constring = "server=localhost;database=dbhorizon;uid=root;password=1234";
                    string Query = "update tblUserIdd set User_Available = 'Available' where User_ID='" + listView1.Items[w].SubItems[0].Text + "' ";

                    MySqlConnection conDatabase = new MySqlConnection(constring);
                    MySqlCommand cmdDatabase = new MySqlCommand(Query, conDatabase);
                    MySqlDataReader myReader;

                    conDatabase.Open();
                    myReader = cmdDatabase.ExecuteReader();
                    dgvref();
//this is what matters 
                    MessageBox.Show("dedede");
                }
                else
                {
                    label3.Text = timeSinceStart[w].TotalSeconds.ToString();
                }
            }

I really cant make the message box pop up and also, the timespan value has decimal values, does it have a connection why the if condition doesnt trigger?
anyway, this the form im working on.

edit* the MH column in the picture is counting upwards, for ex. 1 2 3 4 5 6 etc etc

What I have tried:

I have tried converting timespan to datetime and use datetime and timespan in the if else condition, it also doesnt seem to work.
Posted
Updated 27-Aug-17 21:51pm
v2
Comments
Richard MacCutchan 28-Aug-17 3:50am    
You just need to find out why the if statement never triggers; the debugger will help. And why are you converting numbers to strings in order to compare them?
Member 13378284 28-Aug-17 4:39am    
I think it worked when I replaced this code if (f.ToString() == listView1.Items[w].SubItems[6].Text) with this one if (timeSinceStart[w].TotalSeconds >= f && timeSinceStart[w].TotalSeconds <= f + 1)

1 solution

Firstly, don't do it like that: Never concatenate strings to build a SQL command. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Use Parametrized queries instead.
Or be prepared to restore your DB from backup frequently. You do take backups regularly, don't you?

Then ask yourself two things:
1) Is it really a good idea to try and open a MessageBox in a timer Tick handler? How fast is your timer running?
2) Why is the if failing - and why are you comparing strings? One of the sides is a string only because you converted a numeric value to a string, then other is a random value that is presumably a class level variable that is set elsewhere that you also convert to a string - but we have no idea what is in f to start with. And the value in f is important here, because it's ultimately what decides if the mesage is shown or not (among other things). But we have no idea what it is...

So, its going to be up to you.
Put a breakpoint on the first line in the function, and run your code through the debugger. Then look at your code, and at your data and work out what should happen manually. Then single step each line checking that what you expected to happen is exactly what did. When it isn't, that's when you have a problem, and you can back-track (or run it again and look more closely) to find out why.

Sorry, but we can't do that for you - time for you to learn a new (and very, very useful) skill: debugging!
 
Share this answer
 
Comments
Member 13378284 28-Aug-17 4:39am    
I think it worked when I replaced this code if (f.ToString() == listView1.Items[w].SubItems[6].Text) with this one
if (timeSinceStart[w].TotalSeconds >= f && timeSinceStart[w].TotalSeconds <= f + 1)
Member 13378284 28-Aug-17 4:40am    
its like I trapped the value that I need or something like that. Im sorry im not that familiar to the jargons of computer eng./I.t. and etc
Member 13378284 28-Aug-17 4:41am    
anyway the debugger + the breakpoint you were saying was very helpfull
OriginalGriff 28-Aug-17 4:47am    
It's a damn good tool - get used to it, it can make your whole life a huge amount easier!

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