Link to home
Start Free TrialLog in
Avatar of MUDDY_123
MUDDY_123

asked on

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

ASKER CERTIFIED SOLUTION
Avatar of openshac
openshac

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of 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
Why not do the right thing and declare it as a datetime in your application.