Click here to Skip to main content
15,892,298 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi,

please help with the exception..
"Conversion failed when converting date and/or time from character string."


C#
public void alreadyExists()    // function to check the existing records
    {
        string tdy = DateTime.Now.Date.ToString();
        string aft2dy = DateTime.Now.Date.AddDays(2).ToString();

        string str="select due_date from mConfirm where due_date BETWEEN '"+tdy+"' AND '"+aft2dy+"'";

        sda1 = new SqlDataAdapter(str, cn);
        ds = new DataSet();
        sda1.Fill(ds).ToString();   // HERE I M FACEING THE EXCEPTION

        string d = ds.Tables[0].Rows[0].ItemArray[0].ToString();


        if ((Convert.ToDateTime(d) - DateTime.Today).TotalDays <= 2)
        {
            LblAlreadyExists.Text = "Already Under Process";
        }



    }


THANKS
Posted
Comments
Aniket Yadav 24-Feb-12 1:45am    
In due_date is time necessary or you required just the date?

1 solution

try this:-

public void alreadyExists()    // function to check the existing records
{
    string tdy = DateTime.Now.Date.ToString();
    string aft2dy = DateTime.Now.Date.AddDays(2).ToString();

    string str="select due_date from mConfirm where due_date BETWEEN '"+tdy+"' AND '"+aft2dy+"'";

    sda1 = new SqlDataAdapter(str, cn);
    ds = new DataSet();
    sda1.Fill(ds);
    string d ="";
    if(ds!=null && ds.Tables[0].Rows.Count>0)
      d = Convert.ToString(ds.Tables[0].Rows[0].Cells["due_date "]);

    if ((Convert.ToDateTime(d) - DateTime.Today).TotalDays <= 2)
    {
        LblAlreadyExists.Text = "Already Under Process";
    }
}


Please don't forget to mark this as your answer if it helps you out.

Thanks
 
Share this answer
 
Comments
Tushar1999 24-Feb-12 0:48am    
bro itryed ... but this code is still throwing the exception.....
at..."sda1.Fill(ds);"

as .. Conversion failed when converting date and/or time from character string.
Tushar1999 24-Feb-12 0:49am    
i provide u the complete code... may be then you can help..
Tushar1999 24-Feb-12 0:49am    
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Timers;
using System.Data;
using System.Text;
using System.Drawing;

public partial class COMPLIANCE_CALENDAR : System.Web.UI.Page
{
SqlConnection cn = new SqlConnection("server='fre-ggn-fstest\\sharepoint'; database = 'DMS1'; integrated security = 'true';");
SqlDataAdapter sda,sda1;
SqlCommand cmd;
DataSet ds;


string s;
int i;

protected void Page_Load(object sender, EventArgs e)
{
cn.Open();


s = Session["sessFirstName"].ToString(); // here i m getting the user id from login page

alreadyExists();
// getCompName(); // here i m calling the function that will give me the company name in the label

//LblCompName.Text = s;
}



public void alreadyExists() // function to check the existing records
{


string str = "select due_date from mConfirm where due_date BETWEEN '"+DateTime.Now+"' AND due_date";

sda1 = new SqlDataAdapter(str, cn);
ds = new DataSet();
sda1.Fill(ds);
// string d = ds.Tables[0].Rows[0].ItemArray[0].ToString();
string d = "";
if (ds != null && ds.Tables[0].Rows.Count > 0)
d = Convert.ToString(ds.Tables[0].Rows[0].ItemArray[0]);



if (( DateTime.Today - Convert.ToDateTime(d)).TotalDays <= 2)
{
LblAlreadyExists.ForeColor = System.Drawing.Color.Red;
LblAlreadyExists.Text = "Already Under Process...!!!! you can not proceed";
BtnConfirmd.Enabled = false;
CHKConfirmed.Enabled = false;
}
else
{
BtnConfirmd.Enabled = true;
CHKConfirmed.Enabled = true;
LblAlreadyExists.ForeColor = System.Drawing.Color.Green;
LblAlreadyExists.Text = "You can proceed with the process";
LblDateto.Text = DateTime.Now.ToString(); // showing the time on the page
LblDatetill.Text = DateTime.Now.AddDays(2).ToString();
LblTodayDate.Text = DateTime.Now.ToString();

getCompName();
}


}




public void getCompName()
{
string str = "select comp_name from mCompnay JOIN temp ON ( mCompnay.comp_code = temp.CompCode ) WHERE emp_code='" + s + "'";

sda = new SqlDataAdapter(str, cn);
ds = new DataSet();
sda.Fill(ds);

LblCompName.Text = ds.Tables[0].Rows[0].ItemArray[0].ToString();
}

protected void BtnConfirmd_Click(object sender, EventArgs e)
{

string str = "select doc_id from tdocument td JOIN temp te ON ( td.emp_code = te.emp_code ) where te.emp_code='" + s + "'";
sda = new SqlDataAdapter(str, cn);
ds = new DataSet();
sda.Fill(ds);

string did = ds.Tables[0].Rows[0].ItemArray[0].ToString(); // storing the doc_id here in the variable


if (CHKConfirmed.Checked == true)
{

string str1 = "insert into mConfirm (doc_id,emp_id,confirm_status,assigned_date,due_date) values(@doc_id,@emp_id,@confirm_status,@assigned_date,@due_date)";

cmd = new SqlCommand(str1, cn);
cmd.Parameters.Add("@doc_id", SqlDbType.Int).Value = did;
cmd.Parameters.Add("@emp_id", SqlDbType.Int).Value = s;
cmd.Parameters.Add("@confirm_status", SqlDbType.VarChar).Value = "Confirmed";
cmd.Parameters.Add("@assigned_date", SqlDbType.Date).Value = LblTodayDate.Text;
cmd.Parameters.Add("@due_date", SqlDbType.Date).Value = LblDatetill.Text;

cmd.ExecuteNonQuery();

LBLConfirmed.ForeColor = System.Drawing.Color.Green;
BtnConfirmd.Enabled = false;
LBLConfirmed.Text = "You Have Confirmed and saved the details";
Varun Sareen 24-Feb-12 1:31am    
you must be having a wrong query. Please refer this link:- http://social.msdn.microsoft.com/Forums/en-US/transactsql/thread/f48c5e8e-9d40-4028-8334-2bd240ff7c60/

and reply that whether you able to solve it or not

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


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