简体   繁体   English

如何在C#中将dateTimePickerValue设置为null或为空

[英]How to set dateTimePickerValue to null or empty in c#

I have a windows form of controls combobox and datetimepicker.. I have given null or empty to combobox in page load.. so combobox shows empty intially while loading database values to it in windows form.. but the problem is because of the datetimepicker is not kept to null or empty my form is showing message box as "the day is already existed" before form is desplaying..here my code follows 我有一个Windows窗体的控件combobox和datetimepicker ..在页面加载中,我给了combobox空值或空值。.因此,在以Windows窗体将数据库值加载到它时,combobox最初显示为空。.但是问题是因为datetimepicker是不保持为null或为空我的表单在显示表单之前显示为“日期已经存在”的消息框。

I want to show that message after combobox value is selected.. 我想在选择组合框值后显示该消息。

    try
       {
           ConnectionStringSettings consettings = ConfigurationManager.ConnectionStrings["attendancemanagement"];
           string connectionString = consettings.ConnectionString;
           SqlConnection cn = new SqlConnection(connectionString);
           cn.Open();
           SqlCommand cmd = new SqlCommand("select employee_id,employee_name from Employee_Details", cn);


           SqlDataReader dtr;
           dtr = cmd.ExecuteReader();
           DataTable dt = new DataTable();


           dt.Columns.Add("employee_id", typeof(string));
           dt.Columns.Add("employee_name", typeof(string));
           dt.Load(dtr);



           comboBox1.DisplayMember = "employee_id";
           comboBox1.DisplayMember = "employee_name";
           comboBox1.DataSource = dt;

           comboBox1.SelectedItem = null;
           if(comboBox1.SelectedItem == null)
           {
               txtemployeeid.Text = "";
               txtemployeename.Text = "";

           }

           cn.Close();
       }

       catch (Exception e1)
       {
           MessageBox.Show(e1.Message);

       }

  private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
    {

        ConnectionStringSettings consettings = ConfigurationManager.ConnectionStrings["attendancemanagement"];
        string connectionString = consettings.ConnectionString;
        SqlConnection cn = new SqlConnection(connectionString); 
        cn.Open();
        try
        {
            SqlCommand cmd = new SqlCommand("select employee_id,Employee_name from Employee_Details where employee_name=('" + comboBox1.Text + "')", cn);
            SqlDataReader dtr;

             dtr = cmd.ExecuteReader();
           if (dtr.Read())
            {


                string employee_id = (string)dtr["employee_id"];
                string employee_name = (string)dtr["employee_name"];
                txtemployeeid.Text = employee_id;
                txtemployeename.Text = employee_name;
                dtr.Close();
            }

        }
        catch (Exception e1)
        {
            MessageBox.Show(e1.Message);
        }
        if (comboBox1.SelectedItem != null)
        {
            try
            {

                string dtp = dateTimePicker1.Value.ToString("dd/MM/yyyy");
                SqlCommand cmd1 = new SqlCommand("select date from dailyattendance where date=('" + dtp + "') and employee_id='" + txtemployeeid.Text + "' and empployee_name='" + txtemployeename.Text + "' ", cn);
                SqlDataReader dtr1;
                dtr1 = cmd1.ExecuteReader();
                if (dtr1.Read())
                {
                    string date = (string)dtr1["date"];
                    if (dtp == date)
                    {
                        MessageBox.Show("this day is already existed");
                    }
                }
                dtr1.Close();
            }

            catch (Exception e1)
            {
                MessageBox.Show(e1.Message);
            }
        }

        cn.Close();
    }

can any one solve it please..Thanx in advance 谁能解决这个问题。

您可以简单地使用dtr.Text =“”;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM