Click here to Skip to main content
15,883,901 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I am getting an error that this date is overflowing it must be betwenn the date specfied in the question please help me. I can' t understand how to solve this error.

C#
string p = this.pictureBox1.ImageLocation;
            string name = this.textBox1.Text;
            string description = this.richTextBox1.Text;

            DateTime da = new DateTime();
            string date = da.ToString("dd/MM/YYYY");
            string connString = @"Data Source=(LocalDB)\v11.0;AttachDbFilename=C:\Users\Gardezi\Documents\Visual Studio 2012\Projects\homeWork2\homeWork2\Database1.mdf;Integrated Security=True";
            SqlConnection con = new SqlConnection(connString);
            string query = "insert into diaryDB([Title] , [Description] , [Date] , pic) values (@name , @description ,@date , @p)";
            SqlCommand comm = new SqlCommand(query, con);
            comm.Parameters.AddWithValue("@name", name);
            comm.Parameters.AddWithValue("@description", description);
            comm.Parameters.AddWithValue("date", da);
            comm.Parameters.AddWithValue("@p", p);
            con.Open();
            comm.ExecuteNonQuery();
            con.Close();
Posted
Updated 18-Nov-19 16:18pm
Comments
Manas Bhardwaj 23-Nov-14 12:56pm    
Shouldn't this be like this: (you missed the @ before date)

comm.Parameters.AddWithValue("@date", da);

replace
C#
DateTime da = new DateTime();

with
C#
DateTime da = DateTime.Now;

and also you need to use "@date" when setting parameter value
like below
C#
comm.Parameters.AddWithValue("@date", da);
 
Share this answer
 
v2
Comments
Manas Bhardwaj 23-Nov-14 13:13pm    
yes +5!
Syed Muhammad Ali Gardezi 23-Nov-14 13:18pm    
I am getting this error

The parameterized query '(@name nvarchar(4),@description nvarchar(9),@date nvarchar(10),@' expects the parameter '@p', which was not supplied.
DamithSL 23-Nov-14 13:26pm    
as per your error, date is saved as string, so need to insert string as below

comm.Parameters.AddWithValue("@date", DateTime.Now.ToString("dd/MM/YYYY"));

for the picture column what is the data type you have used in the database column?
Syed Muhammad Ali Gardezi 23-Nov-14 13:32pm    
I am storing its path
Member 13605134 19-Jan-18 10:04am    
con.Open();
SqlCommand cmd = new SqlCommand("SP_AddEmployee", con);

cmd.CommandType = CommandType.StoredProcedure;

cmd.Parameters.AddWithValue("Empname", entities.FullName);
cmd.Parameters.AddWithValue("Empdob", entities.Dateofbirth);
cmd.Parameters.AddWithValue("Empdoj", entities.Dateofjoinning);
cmd.Parameters.AddWithValue("Empdpt", entities.Department);
cmd.Parameters.AddWithValue("Empdesgn", entities.Designation);
cmd.Parameters.AddWithValue("Empsalary", entities.Salary);
cmd.Parameters.AddWithValue("Empnumber", entities.Contactnumber);
cmd.Parameters.AddWithValue("EmpEmail", entities.Emailid);

cmd.Parameters.AddWithValue("createDate", entities.createddate);

returnvalue = cmd.ExecuteNonQuery();
con.Close();



i am getting the error of sql date time overflow in created date parameter how to solve.
thanks in advance
It is pretty simple.
C#
DateTime da = new DateTime();

will give you the value
{1/1/0001 12:00:00 AM}

This value is outside the limits, hence the error.

You need to set the variable da to a proper value.
 
Share this answer
 
Comments
Manas Bhardwaj 23-Nov-14 13:13pm    
+5!
George Jonsson 23-Nov-14 13:19pm    
Thanks Manas.
Alternatively, use SqlDateTime rather than System.DateTime.

http://msdn.microsoft.com/en-us/library/system.data.sqltypes.sqldatetime(v=vs.110).aspx[^]
 
Share this answer
 
Comments
Manas Bhardwaj 23-Nov-14 13:13pm    
Yup +5!
Member 14554959 14-Sep-19 1:37am    
how to update date wise in .net core
try to update but click to save button to date value chang

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900