how to concert nvarchar to datetime?

Posted on 2009-04-30
Medium Priority
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

Accepted Solution

openshac earned 2000 total points
ID: 24269678
command.Parameters.Add(new SqlParameter("Dob", Convert.ToDateTime(Dob)));

LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 24269682
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

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

Featured Post

Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this article I will describe the Detach & Attach method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Integration Management Part 2
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…

850 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