Click here to Skip to main content
15,897,371 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
here is my code................

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

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
Posted
Updated 23-Dec-11 23:03pm
v2
Comments
Amir Mahfoozi 24-Dec-11 5:18am    
In which line you have got error ?
[no name] 24-Dec-11 5:19am    
while (sdr.Read())

 
Share this answer
 
Comments
Abhinav S 24-Dec-11 5:26am    
Link should help OP. 5.
Monjurul Habib 24-Dec-11 5:32am    
thank you
try:

SQL
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);
 
Share this answer
 
v2
Comments
[no name] 24-Dec-11 5:27am    
but i get error.......Arithmetic overflow error converting expression to data type smalldatetime.
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.
 
Share this answer
 
Comments
Monjurul Habib 24-Dec-11 5:25am    
5!
Abhinav S 24-Dec-11 5:26am    
Thank you.
Never concatenate values to the SQL statement, instead use SqlParameter[^].

With parameters you don't have to worry about type conversions (something to text) and they'll work with different locales, not to mention that they will protect you from SQL injections.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900