user3219150
user3219150

Reputation: 51

int is incompatible with datetime2

When my stored procedure is used in my asp web form I'm trying to make it grab the current time and date. The info will be stored in a column with datetime2 data type. When filling out my form I get this..

Operand type clash: int is incompatible with datetime2

command.Parameters.AddWithValue("@dt2LastLoginDate", SqlDbType.DateTime2);

What should be used in order to store the date and time as the correct data type?

protected void Submit_Click(object sender, EventArgs e)
{
    try
    {

        SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["RegDNMembershipConnectionString"].ConnectionString);
        con.Open();
        SqlCommand command = new SqlCommand("dbo.P_AddAccount");
        command.Connection = con;
        command.CommandType = CommandType.StoredProcedure;
        command.Parameters.Add(new SqlParameter("@nvcAccountName", TextBoxUN.Text));
        command.Parameters.Add(new SqlParameter("@inyAccountLevelCode", 100));
        command.Parameters.Add(new SqlParameter("inyCharacterCreateLimit", 4));
        command.Parameters.Add(new SqlParameter("@inyCharacterMaxCount", 4));
        var param = new SqlParameter("@dt2LastLoginDate", System.Data.SqlDbType.DateTime2);
        param.Value = DateTime.Now;
        command.Parameters.Add(param);
        var wrongParam = new SqlParameter("@dt2LastLoginDate", System.Data.SqlDbType.DateTime2);
        wrongParam.Value = System.Data.SqlDbType.DateTime2;
        command.Parameters.Add(new SqlParameter("@vchLastLoginIP", null));
        command.Parameters.Add(new SqlParameter("@@IntLastSessionID", null));
        command.Parameters.Add(new SqlParameter("@vchJoinIP", null));
        command.Parameters.Add(new SqlParameter("@inyPublisherCode", 4));
        command.Parameters.Add(new SqlParameter("@inyGenderCode", null));
        command.Parameters.Add(new SqlParameter("@DaTBirthDate", null));
        command.Parameters.Add(new SqlParameter("@vchPassphrase", TextBoxPass.Text));
        command.Parameters.Add(new SqlParameter("@inyNationalityCode", null));
        command.Parameters.Add(new SqlParameter("@inyChannelPartnerCode", null));
        command.Parameters.Add(new SqlParameter("@EmailAddress", TextBoxEA.Text));
        command.Parameters.Add(new SqlParameter("@FullName", TextBoxFN.Text));
        command.Parameters.Add(new SqlParameter("@Country", DropDownListCountry.SelectedValue));
        command.ExecuteNonQuery();
        con.Close();


    }

Upvotes: 2

Views: 3365

Answers (1)

Nico
Nico

Reputation: 12683

The SqlDBType.DataTime2 (MSDN reference) is an Enum which will be converted to an int (value 33) when parameter is executed. You will need to provide the set the actual DateTime value you wish to set to the paramter on execution. A sample would be:

var param = new SqlParameter("@dt2LastLoginDate", System.Data.SqlDbType.DateTime2);
param.Value = System.DateTime.Now;
command.Parameters.Add(param);

This creates a new paramter with the parameter name, and data type. Next we set the value of the paramter to DateTime.Now or the date value of your choice. Finally this is added to the paramters collection.

Now in your sample the Parameters.AddWithValue() method accepts two parameters. The name of the parameter (ie @dt2LastLoginDate) and the value to set the paramter to during execution. Therefore you can rewrite this statement can be rewritten as:

var wrongParam = new SqlParameter("@dt2LastLoginDate", System.Data.SqlDbType.DateTime2);
wrongParam.Value = System.Data.SqlDbType.DateTime2;

Which as you can see is setting the value to the enum value of SqlDbType.DateTime2

Upvotes: 1

Related Questions