我作的是一个注册信息的页面 现在发现在提交的时候出现将参数值从 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)
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)
解决方案 »
- 请问如何让pictureBox控件里显示的大图片出现滚动条呢?
- 关于连接服务器上数据库的问题
- C#向XML文件插入数据问题,请教!!
- Winform 中遇到一个问题,请指教.在线等......谢谢 解决就结贴
- CSDN blog 上有个叫Trackbacks的是什么意思?
- C#调用DLL的问题
- 求救!! 关于TreeView动态添加的问题(在线等)
- 救命啊,自定义控件Z-Index的问题,高分送上
- 关于JS的问题
- 那位大哥给解释一下反射的概念
- Paradox 在C#.NET怎样设置数据库密码?
- XML转换成的HTML文件中有<?xml version="1.0" encoding="utf-8"?>?
改为paras[5].Value = DateTime.Parse(this.tbBirthday.Text).ToString("yyyy-MM-dd hh:mm:ss");
试试
用Convert.ToDateTime(this.tbBirthday.Text)看看
为什么不把this.tbBirthday.Text 也 trim()一下呢
this.dfUser.ErrorMessage = "对不起,此用户名已存在,请重新填写!";
取出来的值 不对
这个里输入的值 而tbCarid为null 我看前台了 都正确的 没错
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();
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();