Click here to Skip to main content
15,900,378 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
i am adding the price for the products between the two dates..

i have two textboxes to select the the dates which are integrated with ajax tool kit calender control..

but in case if user enter the date. which is not between the dates present in db. i get an exception.. even if one date it db and the other one is not i am getting exception..

how i sort out this problem....
here is the code.

C#
try
       {
           SqlConnection MAconn = new SqlConnection();
           MAconn.ConnectionString = connectionString;
           MAconn.Open();
           //int c = System.Convert.ToInt32(txtamount.Text);

           SqlCommand MAcmd = MAconn.CreateCommand();

           MAcmd.CommandText = "select Sum(price) from sale where (startdate between '" + TextBox1.Text + "' and '" + TextBox2.Text + "')";
           lblsale.Text = System.Convert.ToString(MAcmd.ExecuteScalar());


           MAconn.Close();
           //Response.Redirect("profit.aspx");
       }
       catch (Exception ex)
       {
           lblres.Text = "oh date entered is not in range" + ex;
       }

   }


eand the exception is as fallows.

HTML
System.Data.SqlClient.SqlException: The conversion of a char data type to a datetime data type resulted in an out-of-range datetime value. at System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection) at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection) at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj) at System.Data.SqlClient.TdsParser.Run(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj) at System.Data.SqlClient.SqlDataReader.HasMoreRows() at System.Data.SqlClient.SqlDataReader.ReadInternal(Boolean setTimeout) at System.Data.SqlClient.SqlDataReader.Read() at System.Data.SqlClient.SqlCommand.CompleteExecuteScalar(SqlDataReader ds, Boolean returnSqlValue) at System.Data.SqlClient.SqlCommand.ExecuteScalar() at profit.Button1_Click(Object sender, EventArgs e)
Posted

It seems like you didn't provided the right datetime range to the query. Just validate the datetime input textboxs and you should be fine.

[EDITED]
Perhaps this can be helpful.
C#
try
    {
        SqlConnection MAconn = new SqlConnection();
        MAconn.ConnectionString = connectionString;
        MAconn.Open();
        //int c = System.Convert.ToInt32(txtamount.Text);

        SqlCommand MAcmd = MAconn.CreateCommand();

        DateTime startDate, endDate;
        DateTime.TryParse(TextBox1.Text, out startDate);
        DateTime.TryParse(TextBox2.Text, out endDate);
        if (startDate != null && endDate != null && !string.IsNullOrEmpty(startDate.ToString()) && !string.IsNullOrEmpty(endDate.ToString()) && endDate.CompareTo(startDate) >= 0)
        {
            MAcmd.CommandText = "select Sum(price) from sale where startdate between @startDate and @endDate";
            MAcmd.Parameters.AddWithValue("@startDate", startDate.ToString());
            MAcmd.Parameters.AddWithValue("@endDate", endDate.ToString());
            lblsale.Text = System.Convert.ToString(MAcmd.ExecuteScalar());
        }
        else
            lblres.Text = "oh date entered is not in range";


        MAconn.Close();
        //Response.Redirect("profit.aspx");
    }
    catch (Exception ex)
    {
        lblres.Text = "oh date entered is not in range" + ex;
    }
 
Share this answer
 
v2
Comments
codegeekalpha 3-Dec-11 14:47pm    
THIS IS MY PROBLEM.. I WANT SOME THING TO DISPLAY IF THAT HAPPENS..
Wonde Tadesse 3-Dec-11 15:15pm    
Answer updated.
thatraja 3-Dec-11 21:50pm    
5!
Wonde Tadesse 3-Dec-11 21:51pm    
Thanks
codegeekalpha 4-Dec-11 5:20am    
my five too
Never use string concatenation to add conditions to an SQL statement. Instead use SqlParameter[^] class.

Using concatenation may cause problems with datatype conversions as you have experienced but also leaves you wide open to SQL injections.
 
Share this answer
 
Comments
thatraja 3-Dec-11 21:50pm    
5!
Wendelius 4-Dec-11 3:37am    
Thank you :)
As I said a number of times in your previous questions you are never going to solve this until you stop using strings for dates and use proper DateTime variables. A small amount of pain now will save you from lots of anguish in the future.
 
Share this answer
 
Comments
codegeekalpha 3-Dec-11 15:50pm    
i am not getting u.. if u give me code example than maybe i get u...
Richard MacCutchan 4-Dec-11 3:13am    
Use DateTime.Parse() method to validate your text fields before sending to SQL. Alternatively, replace the TextBoxes by DateTimePicker objects, so your dates are automatically validated.
thatraja 3-Dec-11 21:50pm    
5!
just to leave this error, try the following:
C#
try
        {
            SqlConnection MAconn = new SqlConnection();
            MAconn.ConnectionString = connectionString;
            MAconn.Open();
            //int c = System.Convert.ToInt32(txtamount.Text);

            SqlCommand MAcmd = MAconn.CreateCommand();
 
            MAcmd.CommandText = "select Sum(price) from sale where (startdate between '" + TextBox1.Text + "' and '" + TextBox2.Text + "')";
            lblsale.Text = System.Convert.ToString(MAcmd.ExecuteScalar());
 

            MAconn.Close();
            //Response.Redirect("profit.aspx");
        }
        catch (System.Data.SqlClient.SqlException sqlEx)
        {
            lblres.Text = "oh date entered is not in range: " + sqlEx.ToString();
        }
        catch (Exception ex)
        {
            lblres.Text = "GENERAL ERROR: " + ex.ToString();
        }
 
    }


but also, as solution1 said, do not use concatenation for database manipulation. Your codes are prone to SQL injection.

Regards,
Eduard
 
Share this answer
 
Comments
thatraja 3-Dec-11 21:50pm    
5!
[no name] 3-Dec-11 21:58pm    
thanks thatraja!

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