how to concert nvarchar to datetime?

Posted on 2009-04-30
Last Modified: 2012-05-06
i have a database where dob is datetime in sql server and coding in c# in visual studios where dob is string. i want to add the dob to the database with the following function

     i = (int)command.ExecuteNonQuery();

but i get the following error

cannot convert nvarchar to datetime

how can i fix this

thank you
public class clsRegister


    //connection strings

    public string connectionString;

    public SqlConnection connection;

    public SqlDataAdapter objAdapter;

    public SqlCommand objCommand;

    public DataTable objDT;

    // Variable for tblRegister 

    public string FName = "";

    public string LName = "";

    public string Dob = "";

    public string Address1 = "";

    public string Address2 = "";

    public string City = "";

    public string Zip = "";

    public string State = "";

    public string Telephone = "";

    public string MemBershipType = "";

    public string EmailId = "";


    //Variables for tblLogin

    public string loginName = "";

    public string Pwd = "";

    public string RegId = "";

    //public string RegTypeId = "";

    //Variables from tblMemberType

    public string RegTypeId = "";

    public string MemberTypeName = "";

    public string Type = "staff";

	public clsRegister()



		// TODO: Add constructor logic here


        connectionString = ConfigurationManager.ConnectionStrings["dbCon"].ConnectionString;

        connection = new SqlConnection(connectionString);


    public int AddRegister()


        int i = 0;


        SqlCommand command = new SqlCommand("AddRegister", connection);

        command.CommandType = CommandType.StoredProcedure;

        command.Parameters.Add(new SqlParameter("FName", FName));

        command.Parameters.Add(new SqlParameter("LName", LName));

        command.Parameters.Add(new SqlParameter("Dob", Dob));

        command.Parameters.Add(new SqlParameter("Address1", Address1));

        command.Parameters.Add(new SqlParameter("Address2", Address2));

        command.Parameters.Add(new SqlParameter("City", City));

        command.Parameters.Add(new SqlParameter("Zip", Zip));

        command.Parameters.Add(new SqlParameter("State", State));

        command.Parameters.Add(new SqlParameter("Telephone", Telephone));

        command.Parameters.Add(new SqlParameter("MemBershipType", MemBershipType));

        command.Parameters.Add(new SqlParameter("EmailId", EmailId));

        command.Parameters.Add(new SqlParameter("@LoginName", loginName));

        command.Parameters.Add(new SqlParameter("@Pwd", Pwd));

        command.Parameters.Add(new SqlParameter("@RegTypeId", RegTypeId));


        i = (int)command.ExecuteNonQuery();


        return i;



Open in new window

Question by:MUDDY_123
    LVL 6

    Accepted Solution

    command.Parameters.Add(new SqlParameter("Dob", Convert.ToDateTime(Dob)));

    LVL 142

    Expert Comment

    by:Guy Hengel [angelIII / a3]
    I presume that is because you wrote:
        public string Dob = "";

    but your stored procedure uses datetime for the relevant parameter.

    please clarify
    LVL 39

    Expert Comment

    Why not do the right thing and declare it as a datetime in your application.  

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Find Ransomware Secrets With All-Source Analysis

    Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

    SQL Server engine let you use a Windows account or a SQL Server account to connect to a SQL Server instance. This can be configured immediatly during the SQL Server installation or after in the Server Authentication section in the Server properties …
    How to leverage one TLS certificate to encrypt Microsoft SQL traffic and Remote Desktop Services, versus creating multiple tickets for the same server.
    In this sixth video of the Xpdf series, we discuss and demonstrate the PDFtoPNG utility, which converts a multi-page PDF file to separate color, grayscale, or monochrome PNG files, creating one PNG file for each page in the PDF. It does this via a c…
    In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor ( If you're interested in additional methods for monitoring bandwidt…

    779 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    12 Experts available now in Live!

    Get 1:1 Help Now