我收到错误的输入字符串格式不正确。 [英] I am getting error of input string was not in a correct format.

查看:103
本文介绍了我收到错误的输入字符串格式不正确。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我将数据导入sql表时,得到此错误输入字符串在我的编码行中格式不正确



When i importing data into sql table ,then getting this error Input string was not in a correct format at this line of my coding

DateTime Date = new DateTime(Convert.ToInt32(dateValue.Substring(0, 4)), Convert.ToInt32(dateValue.Substring(4, 2)), Convert.ToInt32(dateValue.Substring(6, 2)));





我的尝试:





What I have tried:

protected void ReadDat(object sender, EventArgs e)
        {
              
            DataTable dt = new DataTable();
    dt.Columns.AddRange(new DataColumn[] {
        new DataColumn("EmployeeCode",typeof(int)),
        new DataColumn("Date",typeof(DateTime)),
        new DataColumn("Time",typeof(TimeSpan)),
        new DataColumn("Status",typeof(string)),
        new DataColumn("MId",typeof(int)) });
    string[] lines = File.ReadAllLines(Server.MapPath(FileUpload1.PostedFile.FileName));  
    for (int i = 0; i < lines.Length; i++)
       
    {
        for (int j = 0; j < lines[i].Split(' ').Length; j++)
        {
            
            string dateValue = lines[i].Split(' ')[j].Substring(0, 7);
            DateTime Date = new DateTime(Convert.ToInt32(dateValue.Substring(0, 4)), Convert.ToInt32(dateValue.Substring(4, 2)), Convert.ToInt32(dateValue.Substring(6, 2)));
            string time = lines[i].Split(' ')[j].Substring(8, 4);
            TimeSpan Times = new TimeSpan(Convert.ToInt32(time.Substring(0, 2)), Convert.ToInt32(time.Substring(2, 2)), 0);
            string INOUT = lines[i].Split(' ')[j].Substring(12, 4);
            Int32 empcode = Convert.ToInt32(lines[i].Split(' ')[j].Substring(16, 7));
            int MID = Convert.ToInt32(lines[i].Split(' ')[j].Substring(23, 2));
            dt.Rows.Add(empcode, Date, Times, INOUT, MID);

            //Insert(empcode, Dates, Times, INOUT, MID);
        }
    }
    gvEmployees.DataSource = dt;
    gvEmployees.DataBind();
}
        }
    }

推荐答案

然后其中一个子串不是数字。让自己的生活更轻松,并使用 DateTime Struct(系统)| Microsoft Docs [ ^ ]。或者更好地使用日历类(系统。 Windows.Controls)| Microsoft Docs [ ^ ]所以你知道你会得到有效的参赛作品。



我看到这已经建议给你四天前在 https://www.codeproject.com/问题/ 1268102 / String-was-not-recognized-as-a-valid-datetime [ ^ ]
Then one of those substrings was not a number. Make life easier for yourself and use one of the proper parse methods from the DateTime Struct (System) | Microsoft Docs[^]. Or better still use a Calendar Class (System.Windows.Controls) | Microsoft Docs[^] so you know that you will get as valid entry.

I see that this was already suggested to you four days ago at https://www.codeproject.com/Questions/1268102/String-was-not-recognized-as-a-valid-datetime[^]


这篇关于我收到错误的输入字符串格式不正确。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆