how to concert nvarchar to datetime?

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;
        connection.Open();
        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();
        connection.Close();
        return i;
    }
}

Open in new window

MUDDY_123Asked:
Who is Participating?
 
openshacCommented:
command.Parameters.Add(new SqlParameter("Dob", Convert.ToDateTime(Dob)));

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

but your stored procedure uses datetime for the relevant parameter.

please clarify
0
 
BrandonGalderisiCommented:
Why not do the right thing and declare it as a datetime in your application.  
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.