我作的是一个注册信息的页面 现在发现在提交的时候出现将参数值从 String 转换到 DateTime 失败错误 不知道怎么去写了 请教各位老师 帮帮我!
  if (Page.IsValid)
        {
            string strSql = "selectUser";
            SqlParameter commandParameters = new SqlParameter("@User", SqlDbType.Char, 30);
            commandParameters.Value = this.tbUser.Text.Trim().ToString();
            int users = Convert.ToInt32(SqlHelper.ExecuteScalar(SqlHelper.connectionString, CommandType.StoredProcedure, strSql, commandParameters));
            if (users < 0)
            {
                this.dfUser.ErrorMessage = "对不起,此用户名以存在,请重新填写!";
            }
            else
            {
                string SQL = "addUser";
                SqlParameter[] paras ={
                    new SqlParameter("@User",SqlDbType.Char,30),
                    new SqlParameter("@Name",SqlDbType.VarChar,50),
                    new SqlParameter("@Psw",SqlDbType.Char,15),
                    new SqlParameter("@Age",SqlDbType.TinyInt),
                    new SqlParameter("@Orgin",SqlDbType.VarChar,20),
                    new SqlParameter("@Birthday",SqlDbType.DateTime),
                    new SqlParameter("@Coard",SqlDbType.Char,18),
                    new SqlParameter("@Sex",SqlDbType.Bit),
                    new SqlParameter("@Phone",SqlDbType.Int),
                    new SqlParameter("@Address",SqlDbType.VarChar,100)};
                paras[0].Value = this.tbUser.Text.Trim();
                paras[1].Value = this.tbName.Text.Trim();
                paras[2].Value = this.tbPsw.Text.Trim();
                paras[3].Value = int.Parse(this.tbAge.Text.Trim());
                paras[4].Value = this.tbOrgin.Text.Trim();
                paras[5].Value = DateTime.Parse(this.tbBirthday.Text);
                paras[5].Value = this.tbCardid.Text.Trim();
                paras[6].Value = int.Parse(this.ddSex.SelectedValue.Trim());
                paras[7].Value = this.tbPhote.Text.Trim();
                paras[8].Value = this.tbAddress.Text.Trim();
                SqlHelper.ExecuteNonQuery(SqlHelper.connectionString, CommandType.StoredProcedure, SQL, paras);
                Response.Redirect("waitLogin.aspx");
            }
        }
public static int ExecuteNonQuery(SqlConnection connection, CommandType commandType, string commandText, params SqlParameter[] commandParameters)
        {
if( connection == null ) throw new ArgumentNullException( "connection" );            // Create a command and prepare it for execution
            SqlCommand cmd = new SqlCommand();
bool mustCloseConnection = false;
            PrepareCommand(cmd, connection, (SqlTransaction)null, commandType, commandText, commandParameters, out mustCloseConnection );
    
            // Finally, execute the command
            int retval = cmd.ExecuteNonQuery();
    
            // Detach the SqlParameters from the command object, so they can be used again
            cmd.Parameters.Clear();
if( mustCloseConnection )
connection.Close();
            return retval;
        }
ALTER PROCEDURE [dbo].[addUser]
@User char(30),
@Name varchar(50),
@Psw char(15),
@Age tinyint,
@Orgin varchar(20),
@Birthday datetime,
@Coard char(18),
@Sex bit,
@Phone int,
@Address varchar(100)
as
insert into Staffinfo(
StaUser,StaName,StaPsw,StaAge,StaOrgin,StaBirthday,StrCoard,
StaSex,StaPhone,StaAddress,StaTime,Audit)
values(@User,@Name,@Psw,@Age,@Orgin,@Birthday,@Coard,@Sex,@Phone,@Address,getdate(),0)

解决方案 »

  1.   

    baidu or google下错误信息
      

  2.   

    paras[5].Value = DateTime.Parse(this.tbBirthday.Text);
    改为paras[5].Value = DateTime.Parse(this.tbBirthday.Text).ToString("yyyy-MM-dd hh:mm:ss");
    试试
      

  3.   

    你的this.tbBirthday.Text得到的值是怎么样的
    用Convert.ToDateTime(this.tbBirthday.Text)看看
      

  4.   

    datetime.tryparse() 用方法里加了参数的日期格式来转换
      

  5.   

    我用验证控件控制了格式 是yyyy-MM-dd这样的 应该是没问题 可是怎么还是不成呢?
      

  6.   

                  paras[5].Value = DateTime.Parse(this.tbBirthday.Text);
    为什么不把this.tbBirthday.Text 也 trim()一下呢
      

  7.   

     string tiem = DateTime.Now.ToString();//获取系统当前时间
      

  8.   

    难道楼主以为baidu or google下,问题能自动解决?
      

  9.   

    this.dfUser.ErrorMessage = "对不起,此用户名存在,请重新填写!";
    this.dfUser.ErrorMessage = "对不起,此用户名已存在,请重新填写!";
      

  10.   

    Convert.ToDateTime(this.tbBirthday.Text)
      

  11.   

    跟踪一下你取到的string是什么样子。
      

  12.   

    this.tbBirthday.Text
    取出来的值 不对
      

  13.   

    断点看看this.tbBirthday.Text 的值是什么呀。
      

  14.   

    看了 值是paras[5].Value = this.tbCardid.Text.Trim();
    这个里输入的值 而tbCarid为null 我看前台了 都正确的 没错 
      

  15.   

    他的值是下一个textbox的值 但是我看了前台 没错的 顺序也没有错误 下一个textbox显示的是空值  晕是死了
      

  16.   

    哥们你这两句话有问题啊  下一个覆盖了上一个 值当然是下一个textbox的值了。
    paras[5].Value = DateTime.Parse(this.tbBirthday.Text);
    paras[5].Value = this.tbCardid.Text.Trim();
    索引都是5.
    应该是
    paras[5].Value = DateTime.Parse(this.tbBirthday.Text);
    paras[6].Value = this.tbCardid.Text.Trim();
      

  17.   

    应该就是这个问题了
    paras[5].Value = DateTime.Parse(this.tbBirthday.Text); 
    paras[6].Value = this.tbCardid.Text.Trim(); 
                    paras[7].Value = int.Parse(this.ddSex.SelectedValue.Trim());
                    paras[8].Value = this.tbPhote.Text.Trim();
                    paras[9].Value = this.tbAddress.Text.Trim();