急求  在C#中当textbox。text为空的时候,转换为日期型出错  在怎么避免呢?

解决方案 »

  1.   

    if(textbox1.text!="")
    {}
    else
    {}
      

  2.   

    DateTime? dt=null
    DateTim.TryParse("",out dt);
    或string.isnullotempty判断
      

  3.   

    还是不怎么懂,说具体点吧, 数据库中的入学时间为日期型,允许为空。
      DateTime sdate = Convert.ToDateTime(maskedTextBox2.Text.ToString().Trim());
    string s="insert into 学生信息表(入学时间) values('"+sdate+"')";
    con.Open();
    OleDbCommand cmd = new OleDbCommand(stuadd, con);
    int i = cmd.ExecuteNonQuery();
    if (i == 1)
      MessageBox.Show("信息添加成功");
     else
      MessageBox.Show("信息添加失败");
    当maskedTextBox2.Text为空的时候,因为null无法转换为日期型,就报错转换错误,
      

  4.   

    DateTime x = new DateTime();
    string y = string.Empty;
    bool err = false;try
    {
        y = DateTime.Pase(textBox1.text.ToString());
        if(string.IsNullOrEmpty(y)==true)
        {
             err = true;
             MessageBox.Show("啥都没输入","出错了");
        }
        else x = DateTime.Pase(y);
    }
    catch
    {
        err = true;
        MessageBox.Show("不是日期类型","输入日期错误");
    }if(err = false)
    {
         //干你的事
    }
      

  5.   

    y = DateTime.Pase(textBox1.text.ToString());
    改为  y = textBox1.text.ToString();
      

  6.   

    DateTime dt;
    DateTime.TryParse(textbox.Text,out dt);如果为空则dt为最小值
      

  7.   

    if(textbox.Equals(""))
    {
    DataTime da = null;或者""
    }都为空了 还转啥  直接给仍个空就行了
      

  8.   

    DateTime sdate;
    string time = maskedTextBox2.Text.ToString().Trim();
    if(time.equals(""))
    {
    sdate="";
    }else
    {
    sdate = Convert.ToDateTime(maskedTextBox2.Text.ToString().Trim());}
      

  9.   

    动作之前当然是现判断数据完整性了~
    if(string.isNullOrEmpty(txt.Text))
    {
      messageBox.show("can'not be null");
    }
      

  10.   

    DateTime dt;
    DateTime.TryParse(textbox.Text,out dt);
    就行啦
      

  11.   

    呵呵 谢谢大家了,我把那个控件换成时间控件了,已经解决了,现在又有个问题:
    急求光标定位代码
     当用户输入的数据部符合要求的时候,比如输入的数据长度不够或者不输入,因为他是主键,必须输入正确,如果输入不正确,就将光标始终定位在那个文本框中textbox里面。