如何使用if else条件和时间跨度值触发消息框? [英] How do I fire a message box with if else condition and timespan value?

查看:140
本文介绍了如何使用if else条件和时间跨度值触发消息框?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个代码

I have this code

<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();
                }
            }



我真的不能弹出消息框,而且,timepan值有十进制值,是否有连接为什么if条件没有触发?

无论如何,这是我正在处理的表格。



编辑*图片中的MH列向上计数,例如。 1 2 3 4 5 6等等



我的尝试:



我试过将timepan转换为datetime并在if else条件下使用datetime和timespan,它似乎也没有用。


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.

推荐答案

首先,不要做它是这样的:永远不要连接字符串来构建SQL命令。它让您对意外或故意的SQL注入攻击持开放态度,这可能会破坏您的整个数据库。请改用参数化查询。

或者准备好经常从备份中恢复数据库。你定期做备份,不是吗?



然后问自己两件事:

1)这真的是个好主意吗尝试在计时器Tick处理程序中打开MessageBox?你的计时器运行有多快?

2)为什么如果失败 - 你为什么要比较字符串?其中一个边是一个字符串只是因为你将一个数字值转换为一个字符串,然后另一个是一个随机值,可能是一个类级别变量,它在别处设置,你也转换为一个字符串 - 但我们不知道是什么在 f 中开始。而 f 中的值在这里很重要,因为它最终决定是否显示消息(以及其他内容)。但是我们不知道它是什么......



所以,它将取决于你。

在断点上设置断点函数的第一行,并通过调试器运行代码。然后查看您的代码,并查看您的数据并找出手动应该发生的事情。然后单步执行每一行检查您预期发生的情况正是如此。如果不是,那就是当你遇到问题时,你可以回溯(或者再次运行并仔细观察)以找出原因。


对不起,但我们不能为你做到这一点 - 时间让你学习一门新的(非常非常有用的)技能:调试!
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!


这篇关于如何使用if else条件和时间跨度值触发消息框?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆