返回首页

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

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值。
请帮助我,我必须做的......

感谢和问候
拉坎

回答

评论会员:Monjurul哈比卜 时间:2012/02/07
尝试以下几点:

{A}
{A2}
评论会员:Yoavi 时间:2012/02/07
尝试:

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);
评论会员:游客 时间:2012/02/07
尝试通过代码调试-错误可能是在这条线。转换ToDateTime(dateTimePicker1.Text)的;尝试使用TryParse方法,以确保DateTime类型的值,然后再尝试转换它是
米卡Wendelius
评论会员:游客 时间:2012/02/07
不要串连值的SQL语句,而是使用{A3的}带参数,你不担心类型转换(文本的东西),他们会使用不同的语言环境,更何况,他们将保护你免受SQL注入