字符串未被识别为有效的日期时间。 [英] String was not recognized as a valid datetime.

查看:95
本文介绍了字符串未被识别为有效的日期时间。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  protected void btnGetDetails_Click(object sender, EventArgs e)
    {
     
            SqlCommand cmd = new SqlCommand("SP_CrewMaster", conn);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.CommandText = "SP_CrewMaster";
            cmd.Parameters.AddWithValue("@Flag", "GetPFNoDetails1");
            cmd.Parameters.AddWithValue("@Depot", lblDepot.Text);
            cmd.Parameters.AddWithValue("@PFNo", txtPFNo.Text);
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            conn.Close();
            conn.Open();
            SqlDataReader dr = cmd.ExecuteReader();
            if (dr.Read())
            {
                txtName.Text = (dr["Name"].ToString());
                txtDepot.Text = (dr["Depot"].ToString());
                txtDivision.Text = (dr["Division"].ToString());
                txtTokenNo.Text = (dr["TokenNo"].ToString());
                ddlDesignation.SelectedItem.Text = (dr["Designation"].ToString());
                txtMobileNo.Text = (dr["MobileNo"].ToString());
                ddlGender.SelectedItem.Text = (dr["Gender"].ToString());
                txtPFNo1.Text = (dr["PFNo"].ToString());
                txtCL.Text = (dr["CL"].ToString());
                txtCML.Text = (dr["CML"].ToString());
                txtEL.Text = (dr["EL"].ToString());
                ddlStatus.SelectedItem.Text = (dr["CurrentStatus"].ToString());
                ddlWeekoff.SelectedItem.Text = (dr["WeekOff"].ToString());
                txtDateOfJoining.Text = (dr["DateOfProbitionary"].ToString());


 // date and time is not in correct format i am getting this 


i am retrieving data from database SQL server in server I mentioned date as 1990/12/12  00:00:00 in Date of joining columns

What I have tried:

I tried with TryParse, Parse,

<pre>protected void btnGetDetails_Click(object sender, EventArgs e)
    {
     
            SqlCommand cmd = new SqlCommand("SP_CrewMaster", conn);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.CommandText = "SP_CrewMaster";
            cmd.Parameters.AddWithValue("@Flag", "GetPFNoDetails1");
            cmd.Parameters.AddWithValue("@Depot", lblDepot.Text);
            cmd.Parameters.AddWithValue("@PFNo", txtPFNo.Text);
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            conn.Close();
            conn.Open();
            SqlDataReader dr = cmd.ExecuteReader();
            if (dr.Read())
            {
                txtName.Text = (dr["Name"].ToString());
                txtDepot.Text = (dr["Depot"].ToString());
                txtDivision.Text = (dr["Division"].ToString());
                txtTokenNo.Text = (dr["TokenNo"].ToString());
                ddlDesignation.SelectedItem.Text = (dr["Designation"].ToString());
                txtMobileNo.Text = (dr["MobileNo"].ToString());
                ddlGender.SelectedItem.Text = (dr["Gender"].ToString());
                txtPFNo1.Text = (dr["PFNo"].ToString());
                txtCL.Text = (dr["CL"].ToString());
                txtCML.Text = (dr["CML"].ToString());
                txtEL.Text = (dr["EL"].ToString());
                ddlStatus.SelectedItem.Text = (dr["CurrentStatus"].ToString());
                ddlWeekoff.SelectedItem.Text = (dr["WeekOff"].ToString());
                txtDateOfJoining.Text = (dr["DateOfProbitionary"].ToString());


 // date and time is not in correct format i am getting this 


i am retrieving data from database SQL server in server I mentioned date as 1990/12/12  00:00:00 in Date of joining columns 

推荐答案

我们不知道错误发生在哪里:您使用的只是您显示的代码中的字符串,因此很可能与您的存储过程有关,我们无法看到 - 或代码中的其他位置或数据中的某个位置。我们无法访问任何一个!



所以,这将取决于你。

幸运的是,你有一个工具可供您使用,它将帮助您了解正在发生的事情:调试器。谷歌的Visual Studio调试器应该为您提供使用它所需的信息。



在函数的第一行放置一个断点,然后运行你的代码通过调试器。然后查看您的代码,并查看您的数据并找出手动应该发生的事情。然后单步执行每一行检查您预期发生的情况正是如此。如果不是,那就是当你遇到问题时,你可以回溯(或者再次运行并仔细观察)以找出原因。


对不起,但我们不能为你做到这一点 - 时间让你学习一门新的(非常非常有用的)技能:调试!
We have no idea where the error is coming: all you use are strings in the code you show, so it's most likely something to do with your Stored Procedure, which we can't see - or elsewhere in your code, or somewhere in your data. And we have no access to any of that!

So, it's going to be up to you.
Fortunately, you have a tool available to you which will help you find out what is going on: the debugger. Google for "Visual Studio debugger" should give you the info you need in order to use it.

Put a breakpoint on the first line in the function, and run your code through the debugger. Then look at your code, and at your data and work out what should happen manually. Then single step each line checking that what you expected to happen is exactly what did. When it isn't, that's when you have a problem, and you can back-track (or run it again and look more closely) to find out why.

Sorry, but we can't do that for you - time for you to learn a new (and very, very useful) skill: debugging!


Quote:

字符串未被识别为有效日期时间。

String was not recognized as a valid datetime.



某处您的代码和SQL服务器可能希望日期格式为不是你提供的那个。

你需要得到错误的位置才知道它是你的代码或SQL服务器。

我们无法帮你进一步,因为问题不在代码中,而是在默认日期格式中。


Somewhere your code and probably the SQL server expect dates to be in a format that is not the one you provided.
You need to get the position of error to know it is your code or the SQL server.
We can't help you further because the problem is not in code, but in default date format.


这篇关于字符串未被识别为有效的日期时间。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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