asp.net
protected void Button8_Click(object sender, EventArgs e){
string st_strconn = ConfigurationManager.ConnectionStrings["ST_StuMangerConnectionString"].ToString();
SqlConnection con = new SqlConnection(st_strconn);
con.Open();
SqlCommand cmd = new SqlCommand("select_student_1", con);
cmd.CommandType = CommandType.StoredProcedure;
SqlParameter parstudent_id = cmd.CreateParameter();
parstudent_id.ParameterName = "@student_id";
parstudent_id.SqlDbType = SqlDbType.VarChar;
parstudent_id.Size = 50;
parstudent_id.Value = TextBox1.Text;
cmd.Parameters.Add(parstudent_id);
SqlParameter parstudent_name = cmd.CreateParameter();
parstudent_name.ParameterName = "@student_name";
parstudent_name.SqlDbType = SqlDbType.VarChar;
parstudent_name.Size = 50;
parstudent_name.Value = TextBox2.Text;
cmd.Parameters.Add(parstudent_name);
SqlParameter parstudent_易做图 = cmd.CreateParameter();
parstudent_易做图.ParameterName = "@student_易做图";
parstudent_易做图.SqlDbType = SqlDbType.Char;
parstudent_易做图.Size = 10;
parstudent_易做图.Value = RadioButtonList1.Text;
cmd.Parameters.Add(parstudent_易做图);
SqlParameter Parstudent_nation = cmd.CreateParameter();
Parstudent_nation.ParameterName = "@student_nation";
Parstudent_nation.SqlDbType = SqlDbType.VarChar;
Parstudent_nation.Size = 50;
Parstudent_nation.Value = TextBox3.Text;
cmd.Parameters.Add(Parstudent_nation);
SqlParameter Parstudent_birthday = cmd.CreateParameter();
Parstudent_birthday.ParameterName = "@student_birthday";
Parstudent_birthday.SqlDbType = SqlDbType.DateTime;
Parstudent_birthday.Value = Convert.ToDateTime(TextBox4.Text);
cmd.Parameters.Add(Parstudent_birthday);
SqlParameter Parstudent_time = cmd.CreateParameter();
Parstudent_time.ParameterName = "@student_time";
Parstudent_time.SqlDbType = SqlDbType.DateTime;
Parstudent_time.Value = Convert.ToDateTime(TextBox5.Text);
cmd.Parameters.Add(Parstudent_time);
SqlParameter Parstudent_classid = cmd.CreateParameter();
Parstudent_classid.ParameterName = "@student_class_id";
Parstudent_classid.SqlDbType = SqlDbType.VarChar;
Parstudent_classid.Size = 50;
Parstudent_classid.Value = DropDownList1.SelectedValue.ToString();
cmd.Parameters.Add(Parstudent_classid);
SqlParameter Parstudent_home = cmd.CreateParameter();
Parstudent_home.ParameterName = "@student_home";
Parstudent_home.SqlDbType = SqlDbType.VarChar;
Parstudent_home.Size = 50;
Parstudent_home.Value = TextBox7.Text;
cmd.Parameters.Add(Parstudent_home);
SqlParameter Parstudent_else = cmd.CreateParameter();
Parstudent_else.ParameterName = "@student_else";
Parstudent_else.SqlDbType = SqlDbType.VarChar;
Parstudent_else.Size = 50;
Parstudent_else.Value = TextBox7.Text;
cmd.Parameters.Add(Parstudent_else);
SqlParameter parState = cmd.CreateParameter();
parState.ParameterName = "@BackStatis";
parState.DbType = DbType.Int32;
parState.Direction = ParameterDirection.Output;
cmd.Parameters.Add(parState);
cmd.ExecuteNonQuery();
con.Close();
if ((int)parState.Value != 1)
{
Response.Write("<script>alert('保存成功!')</script>");
}
else
{
Response.Write("<script>alert('保存失败,学号唯一!')</script>");
}
con.Close();
}
“/web”应用程序中的服务器错误。
--------------------------------------------------------------------------------
该字符串未被识别为有效的 DateTime。
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。
异常详细信息: System.FormatException: 该字符串未被识别为有效的 DateTime。
源错误:
行 61: Parstudent_birthday.ParameterName = "@student_birthday";
行 62: Parstudent_birthday.SqlDbType = SqlDbType.DateTime;
行 63: Parstudent_birthday.Value = Convert.ToDateTime(TextBox4.Text);
行 64: cmd.Parameters.Add(Parstudent_birthday);
行 65: SqlParameter Parstudent_time = cmd.CreateParameter();
源文件: f:\asp.net案例开发\web\addstudent.aspx.cs 行: 63
堆栈跟踪:
[FormatException: 该字符串未被识别为有效的 DateTime。]
System.DateTimeParse.Parse(String s, DateTimeFormatInfo dtfi, DateTimeStyles styles) +2271922
System.DateTime.Parse(String s, IFormatProvider provider) +26
System.Convert.ToDateTime(String value) +86
Default2.Button8_Click(Object sender, EventArgs e) in f:\asp.net案例开发\web\addstudent.aspx.cs:63
System.Web.UI.WebControls.Button.OnClick(EventArgs e) +75
System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +97
System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +7
System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +11
System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +33
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +4919
该怎么改改啊!
--------------------编程问答--------------------
Parstudent_birthday.Value = Convert.ToDateTime(TextBox4.Text);
Parstudent_time.Value = Convert.ToDateTime(TextBox5.Text);
有空值 --------------------编程问答-------------------- 这代码长的, --------------------编程问答-------------------- 先用DateTime对象实例后,再接受该Text的值。然后再赋值
Parstudent_time.ParameterName = "@student_time";
Parstudent_time.SqlDbType = SqlDbType.DateTime;
DateTime dtime=new DateTime();
dtime=Convert.ToDateTime(TextBox5.Text.Trim());
Parstudent_time.Value = dtime;
这样试下,调试一起看值的错误 --------------------编程问答-------------------- +1
建议楼主适当写点注释哈、
代码那么乱 又没注释 别人看着会易做图的。。。
--------------------编程问答-------------------- Parstudent_birthday.Value = Convert.ToDateTime(TextBox4.Text);
Parstudent_time.Value = Convert.ToDateTime(TextBox5.Text);
从数据库里面读取出来的日期格式不正确 不符合转换成C#日期格式 --------------------编程问答--------------------
这个异常也太明显不过了,addstudent.aspx.cs页面第63行,发生转换异常。
断点看看这两句话:
Parstudent_birthday.Value = Convert.ToDateTime(TextBox4.Text);
Parstudent_time.Value = Convert.ToDateTime(TextBox5.Text);
是不是TextBox4与TextBox5哪个文本框为空,或者包含一些非法字符。 --------------------编程问答-------------------- 都知道是这个错误,小弟我怎么改改啊! --------------------编程问答-------------------- TextBox4或TextBox5的文本框如果输入的不符合下面的日期格式:
2011-1-1或2011/1/1
就会报错,所以,建议你用日期控件,让人选,别让人输入。
如果上面的都不想做,那你干脆在这里加一个校验:
try{
Parstudent_birthday.Value = Convert.ToDateTime(TextBox4.Text);
Parstudent_time.Value = Convert.ToDateTime(TextBox5.Text);
}catch(Exception){
Response.Write("请输入正确的日期格式!例如:2011-1-1");
} --------------------编程问答-------------------- 能调试么?不是大问题!
补充:.NET技术 , ASP.NET