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

时间:2014-09-04 06:15:16

标签: c# combobox datetimepicker

我有一个控件组合框和日期时间选择器的窗口形式.. 我在页面加载中给组合框提供了null或空。 因此,在Windows窗体中将数据库值加载到组合框时,组合框显示为空。 但问题是 因为datetimepicker没有保持为null或为空我的表单显示消息框为"该日已经存在"在形式出现之前,我的代码遵循

我想在选择组合框值后显示该消息..

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

任何人都可以解决它..提前提前

1 个答案:

答案 0 :(得分:0)

您只需使用dtr.Text ="";