In a TextBox, user enters date in the format dd/MM/yyyy format. I am sending this value to stored procedure as:
cmd.Parameters.AddWithValue("@myDate", Convert.ToDateTime(txtMyDate.Text));
The stored procedure parameter is of the type SmallDateTime. I want to pass the above code value in dd/MM/yyyy format. How to specify the format in the above code to make sure that SQL Server stores dates in dd/MM/yyyy format?
What you describe is a bit confusing:
IF the parameter of the Stored Procedure is indeed SmallDataTime then it inherently has no format.
txtMyDate.Textseems to be a string and thus have a dateformat… you can either be localizetion-friendly and keep your code as it is – at least in a normal desktop app it would take into account the system setting for dateformat. Another option is to explicitely hardcode the format so that the user input has to be in this specific format… this can be done by usingDateTime.ParseExact ( txtMyDate.Text, "dd/MM/yyyy", CultureInfo.InvariantCulture ).MSDN references: