简体   繁体   中英

date time is being set to default, how can I use this parameter and get a value from textbox?

The code below seems to be setting the time to SQL's default rather than converting from the text box. How can i fix this? I'm trying to learn how to do parameters rather than referring to the text box directly, as this was advised in some answers to some of my earlier questions. Additional information language being used is c#, IDE is visual and using local SQL server

command.Parameters.AddWithValue("@ScheduledDateTime", SqlDbType.DateTime);
{
    DateTime.Parse(ScheduledDateTime.Text);
};

The AddWithValue method doesn't take the type as a parameter, it is just the parameter name and it's value:

command.Parameters.AddWithValue("@ScheduledDateTime", DateTime.Parse(ScheduledDateTime.Text));

Or you could use:

command.Parameters.Add("@ScheduledDateTime", SqlDbType.DateTime).Value = DateTime.Parse(ScheduledDateTime.Text);

As it stands though your statement adding the parameter, and statement parsing the datetime are not related, so you never actually pass the value to the command.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM