日期转换问题 [英] date conversion problem

查看:64
本文介绍了日期转换问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的代码.......

here is my code................

private void dateTimePicker1_Leave(object sender, EventArgs e)
        {
            SqlConnection con = Conn.getopenedconnection();

            SqlCommand cmd = con.CreateCommand();
            cmd.CommandType = CommandType.Text;
            cmd.Connection = con;
            DateTime date1 = Convert .ToDateTime(dateTimePicker1.Text) ;
            string date11 = date1.ToShortDateString();
            cmd.CommandText = ("SELECT itemcode,maturity FROM SAUDAMAST WHERE COMPCODE='" + Compcls.Gcomp_cd + "'and itemcode='"+comboBox1.SelectedValue+"' and maturity='"+date11+"'");
            SqlDataReader sdr = cmd.ExecuteReader();
            int count = 0;
            while (sdr.Read())
            {
                count = 1;
            }

            if (count > 0)
            {


                MessageBox.Show("Sauda already exists this comodity and this date");
                dateTimePicker1.Focus();
              
            }
        }


但我得到错误=将char数据类型转换为smalldatetime数据类型导致超出范围的smalldatetime值.


请帮我做什么...

感谢和问候
lakhan


but i get error=The conversion of char data type to smalldatetime data type resulted in an out-of-range smalldatetime value.


please help me what i have to do......

thanks and regards
lakhan

推荐答案

尝试以下操作:
使用C#将DateTime插入SQL Server
使用SqlCommand使用参数调用存储过程
在DB中插入日期仍然存在问题
Try following:
Insert DateTime into SQL Server using C#
Call stored procedure with parameters using SqlCommand
Still problems inserting dates in DB


try:

try:

cmd.CommandText = ("SELECT itemcode,maturity FROM SAUDAMAST WHERE COMPCODE=@COMPCODE and itemcode=@itemcode and maturity=@maturity");
       cmd.Parameters.AddWithValue("@COMPCODE", Compcls.Gcomp_cd);
       cmd.Parameters.AddWithValue("@itemcode", comboBox1.SelectedValue);
       cmd.Parameters.AddWithValue("@maturity", date1);


尝试通过代码进行调试-错误可能在此行上.
Convert .ToDateTime(dateTimePicker1.Text) ;

在尝试将其转换之前,请尝试使用TryParse 方法以确保该值是DateTime类型.
Try debugging through code - the error is probably on this line.
Convert .ToDateTime(dateTimePicker1.Text) ;

Try using the TryParse method to ensure the value is of DateTime type before trying to convert it.


这篇关于日期转换问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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