简体   繁体   English

通过Web服务将DateTime值传递到SQL数据库时,SqlDateTime溢出错误

[英]SqlDateTime overflow error when passing DateTime value to SQL database through webservice

Textview has a text with a date ( DateTime.Now.LocalTime ) that results in this format: 7/4/2016 3:32:40 PM . Textview的文本带有日期( DateTime.Now.LocalTime ),其结果格式如下: 7/4/2016 3:32:40 PM I tried passing this through DateTime time = Convert.ToDateTime(time.Text); 我尝试通过DateTime time = Convert.ToDateTime(time.Text);传递此DateTime time = Convert.ToDateTime(time.Text); but gives me the SqlDateTime overflow error so I tried changing it into DateTime time = DateTime.ParseExact(txt.Text, "MM/dd/yyyy hh:mm:ss tt", CultureInfo.InvariantCulture); 但给了我SqlDateTime溢出错误,因此我尝试将其更改为DateTime time = DateTime.ParseExact(txt.Text, "MM/dd/yyyy hh:mm:ss tt", CultureInfo.InvariantCulture); but gives me an incorrect format error. 但给我一个不正确的格式错误。 What could be the problem? 可能是什么问题呢?

Here are my codes: 这是我的代码:

TextView txt = FindViewById<TextView>(Resource.Id.txt);
DateTime time = DateTime.ParseExact(txt.Text, "MM/dd/yyyy hh:mm:ss tt", CultureInfo.InvariantCulture);

// to pass to webservice

userInfo.TimeStamp = time; // TimeStamp is DateTime type

Relevant codes: 相关代码:

private void DialogBox(object sender, EventArgs eventArgs) // this is for inserting the DateTime value into database
    {
        EmployeeDetails userInfo = new EmployeeDetails();
        DateTime now = DateTime.Now.ToLocalTime();
        TextView txt = FindViewById<TextView>(Resource.Id.txt);

        if (String.IsNullOrWhiteSpace(request.Text))
        {
            Android.Support.V7.App.AlertDialog.Builder builder = new Android.Support.V7.App.AlertDialog.Builder(this, Resource.Style.AppCompatAlertDialogStyle);
            builder.SetTitle("Warning!");
            builder.SetIcon(Resource.Drawable.warning);
            builder.SetMessage("Please specify your comment or suggestion.");
            builder.SetPositiveButton("OK", (s, ev) => { });
            builder.Show();
        }
        else
        {
            txt.Text = now.ToString(); // this is for getting the value of passed datetime for next use
            userInfo.TimeStamp = now;
            _client.InsertToFeedbackTableAsync(userInfo);
        }
    }

private void ClientOnInsertToFeedbackTableCompleted(object sender, InsertToFeedbackTableCompletedEventArgs ea)
    {
        EmployeeDetails userInfo = new EmployeeDetails();
        TextView txt = FindViewById<TextView>(Resource.Id.txt);
        DateTime time = DateTime.ParseExact(txt.Text, "MM/dd/yyyy hh:mm:ss tt", CultureInfo.InvariantCulture);

        string msg = null;

        if (ea.Error != null)
        {
            //blahblah
        }
        else if (ea.Cancelled)
        {
            //blahblah
        }
        else
        {
            msg = null;

            RunOnUiThread(() =>
            {                     
                userInfo.TimeStamp = time;                       

                _client.InsertToAnswersTableRequestAsync(userInfo);
            });
        }
    }

 private void ClientOnInsertToAnswersTableRequestCompleted(object sender, InsertToAnswersTableRequestCompletedEventArgs ea)
    {
        EmployeeDetails userInfo = new EmployeeDetails();
        TextView txt = FindViewById<TextView>(Resource.Id.txt);
        DateTime time = DateTime.ParseExact(txt.Text, "MM/dd/yyyy hh:mm:ss tt", CultureInfo.InvariantCulture);
        string msg = null;

        if (ea.Error != null)
        {
            //blahblah
        }
        else if (ea.Cancelled)
        {
            //blahblah
        }
        else
        { // blahblah}

My code in the webservice: 我在网络服务中的代码:

var fbID = from y in context.Feedbacks where y.timestamp == userInfo.TimeStamp select y.id; // where y.timestamp and userInfo.TimeStamp are DateTime
var fb_ID = fbID.FirstOrDefault();
int returnfbID = Convert.ToInt32(fb_ID);

由于您的日期时间值(例如7/4/2016 3:32:40 PM可以包含7/4/2016 3:32:40 PM数字,而不一定7/4/2016 3:32:40 PM包含2位数字,因此应将格式字符串"M/d/yyyy h:m:s tt"用于DateTime.ParseExact

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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