C# 如何在c中将dateTimePickerValue设置为null或空#
我有一个windows窗体的控件组合框和日期时间选择器。。 我在页面加载中为combobox指定了null或空。。 因此,在windows窗体中将数据库值加载到combobox时,combobox初始显示为空。。 但问题是 由于datetimepicker未保持为null或为空,因此在表单解压缩之前,我的表单将消息框显示为“日期已存在”。下面是我的代码 我想在选择组合框值后显示该消息C# 如何在c中将dateTimePickerValue设置为null或空#,c#,combobox,datetimepicker,C#,Combobox,Datetimepicker,我有一个windows窗体的控件组合框和日期时间选择器。。 我在页面加载中为combobox指定了null或空。。 因此,在windows窗体中将数据库值加载到combobox时,combobox初始显示为空。。 但问题是 由于datetimepicker未保持为null或为空,因此在表单解压缩之前,我的表单将消息框显示为“日期已存在”。下面是我的代码 我想在选择组合框值后显示该消息 try { ConnectionStringSettings con
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();
}
任何人都能解决它吗?..Thanx提前您只需使用dtr.Text=“” 首先要解决的问题是:停止像那样构建SQL查询。使用参数化查询,避免SQL注入攻击、转换问题等。下一步,使用
使用语句关闭连接、读卡器等。我不知道如何构建参数化查询请使用参数化查询修改它,对于转换问题等,请添加此处我将在下一步使用该代码。现在是学习的好时机,在创建具有SQL注入漏洞的应用程序之前。请查看SqlCommand.Parameters
的文档作为起点。