How to save only date in sql and How to convert dd/MM/yyyy to sql date format?

image description here

When i try to save this type of date from c# to sql getting format exception Please suggest me the best way to save date to sql

  • Get a count based on the row order
  • dbo.TempGetStateItemExclusive3 called repeatedly
  • Comparing last group of IP Addresses and appending missing values
  • Timeout issue in Backup-SQLDatabase cmdlet
  • Can “constant” lookups be done efficiently within a single query?
  • Slow “Select” Query with varchar(max)
  • Get Connection String from Method returning Connection error Object Refrence not Set to the instance of object ???
  • How to read data in each row and each column from sql table in using C#?
  • SQL Nested Queries in C# and Connection Open Problem
  • Accounting Database - storing a transaction
  • How to connect to SQL Server database using windows authentication remotely?
  • What is the most cost-effective way to break up a centralised database?
  • 2 Solutions collect form web for “How to save only date in sql and How to convert dd/MM/yyyy to sql date format?”

    You are getting that error because you are passing a string instead of DateTime, also you need should use paramaterized query instead to avoid SQL Injection attack and consider using the using statement to automatically dispose you expensive resources like connection objects. These are few best practices you should keep in mind when working with a SQL server. You can use below code:-

    string bDate = "30/06/1989";
    DateTime bDateparam = Convert.ToDateTime(bDate);
    string sqlquery = "insert into BirthTable (bdate) values (@bdate)";
    using (SqlConnection conn = new SqlConnection(CS))
        using (SqlCommand cmd = new SqlCommand(sqlquery,conn))
            cmd.Parameters.Add("@bdate",SqlDbType.DateTime).Value = bDateparam;

    You can also use TryParseExact to parse the date in any specific format.

    Convert Your DateString To DateTime And Then Try Passing It To The Query, Like..

     String Bday="27/11/2015";  
     DateTime  dtBDay = DateTime.ParseExact(Bday,"dd/MM//yyyyy", CultureInfo.InvariantCulture);

    Now Pass dtBDay To Your Query.
    Hope It’ll Help.

    MS SQL Server is a Microsoft SQL Database product, include sql server standard, sql server management studio, sql server express and so on.