Click here to Skip to main content
12,067,928 members (38,187 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: C#
Hello,
i'm trying to insert a datetime into a sql table and i'm getting an error here. the code is:

da.InsertCommand = new SqlCommand("INSERT INTO tblTestTable3 VALUES (@PrveCislo, @DruheCislo, @TretieCislo, @StvrteCislo, @PiateCislo, @Date)", conn);
 
da.InsertCommand.Parameters.Add("@Date", SqlDbType.DateTime).Value = (DateTime)this.dataGridView1.CurrentRow.Cells[0].Value;
da.InsertCommand.Parameters.Add("@PrveCislo", SqlDbType.Int).Value = (int)this.dataGridView1.CurrentRow.Cells[1].Value;
da.InsertCommand.Parameters.Add("@DruheCislo", SqlDbType.Int).Value = (int)this.dataGridView1.CurrentRow.Cells[2].Value;
da.InsertCommand.Parameters.Add("@TretieCislo", SqlDbType.Int).Value = (int)this.dataGridView1.CurrentRow.Cells[3].Value;
da.InsertCommand.Parameters.Add("@StvrteCislo", SqlDbType.Int).Value = (int)this.dataGridView1.CurrentRow.Cells[4].Value;
da.InsertCommand.Parameters.Add("@PiateCislo", SqlDbType.Int).Value = (int)this.dataGridView1.CurrentRow.Cells[5].Value;
    da.InsertCommand.ExecuteNonQuery();


the error I'm getting here is :

"Implicit conversion from data type datetime to int is not allowed. Use the CONVERT function to run this query."

Date column in sql table is DateTime and it should be unique.

How do I convert and why is it trying to convert to int. Can anybody help?
JL
Posted 6-Apr-11 5:06am
JML_441
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

da.InsertCommand = new SqlCommand("INSERT INTO tblTestTable3 VALUES (@PrveCislo, @DruheCislo, @TretieCislo, @StvrteCislo, @PiateCislo, @Date)", conn);
da.InsertCommand.Parameters.Add("@PrveCislo", SqlDbType.Int).Value = (int)this.dataGridView1.CurrentRow.Cells[0].Value;
da.InsertCommand.Parameters.Add("@DruheCislo", SqlDbType.Int).Value = (int)this.dataGridView1.CurrentRow.Cells[1].Value;
da.InsertCommand.Parameters.Add("@TretieCislo", SqlDbType.Int).Value = (int)this.dataGridView1.CurrentRow.Cells[2].Value;
da.InsertCommand.Parameters.Add("@StvrteCislo", SqlDbType.Int).Value = (int)this.dataGridView1.CurrentRow.Cells[3].Value;
da.InsertCommand.Parameters.Add("@PiateCislo", SqlDbType.Int).Value = (int)this.dataGridView1.CurrentRow.Cells[4].Value;
da.InsertCommand.Parameters.Add("@Date", SqlDbType.DateTime).Value = (DateTime)this.dataGridView1.CurrentRow.Cells[5].Value;
    da.InsertCommand.ExecuteNonQuery();

I hope the above information will be helpful. If you have more concerns, please let me know.
  Permalink  
Comments
JML_ 7-Apr-11 4:22am
   
Oh, yeaah. Thanks it did work.
Monjurul Habib 7-Apr-11 14:15pm
   
great.
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

What is data type in sql server for you date column. It seems that it is timestamp and this is causing this issue.
  Permalink  
Comments
JML_ 6-Apr-11 10:19am
   
Date Column Properties:

Name - Date
Data Type - datatime
System Type - datatime
Primary Key - False
Allow nulls - False
Identity - False

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web02 | 2.8.160208.1 | Last Updated 6 Apr 2011
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100