100分请高手解决ASP.NET添加失败问题
privatevoidButton1_Click(objectsender,System.EventArgse){if(Page.IsValid){stringstrco...
private void Button1_Click(object sender, System.EventArgs e)
{
if(Page.IsValid)
{
string strconn="Data Source=ASUS-PC;Database=pms;uid=sa;pwd=;";
SqlConnection cn=new SqlConnection(strconn);
string strsql="insert into personnel_information(UID,UName,UPassWord,UBirthday,UMarriage,UWorktime,UIntime,UDepartment,UPosition,UPhone)values(@UID,@UName,@UPassWord,@UBirthday,@UMarriage,@UWorktime,@UIntime,@UDepartment,@UPosition,@UPhone)";
SqlCommand cm=new SqlCommand (strsql,cn);
cm.Parameters.Add(new SqlParameter("@UID",SqlDbType.VarChar,50));
cm.Parameters["@UID"].Value=Tb_name.Text;
cm.Parameters.Add(new SqlParameter("@UName",SqlDbType.VarChar,50));
cm.Parameters["@UName"].Value=Tb_name.Text;
cm.Parameters.Add(new SqlParameter("@UPassWord",SqlDbType.VarChar,50));
cm.Parameters["@UPassWord"].Value=Tb_password.Text;
cm.Parameters.Add(new SqlParameter("@UBirthday",SqlDbType.DateTime,8));
cm.Parameters["@UBirthday"].Value=Tb_birth.Text;
try
{
DateTime dt = Convert.ToDateTime(Tb_birth.Text);
}
catch
{
Response.Write("<script language=javascript>alert('您输入的不是数字,请输入正确的格式');</script>");
return;
}
cm.Parameters.Add(new SqlParameter("@UMarriage",SqlDbType.VarChar,50));
cm.Parameters["@UMarriage"].Value=Tb_name.Text;
cm.Parameters.Add(new SqlParameter("@RB_marriged",SqlDbType.VarChar,50));
cm.Parameters["@RB_marriged"].Value=Tb_name.Text;
if(RB_marriged.Checked)
{
cm.Parameters["@RB_marriged"].Value="是";
}
else if(RB_unmarriged.Checked)
{
cm.Parameters["@UMarriage"].Value="否";
}
cm.Parameters.Add(new SqlParameter("@UWorktime",SqlDbType.VarChar,50));
cm.Parameters["@UWorktime"].Value=Te_date.Text;
cm.Parameters.Add(new SqlParameter("@UIntime",SqlDbType.DateTime,8));
cm.Parameters["@UIntime"].Value=Tbx_intime.Text;
try
{
DateTime dt = Convert.ToDateTime(Tbx_intime.Text);
}
catch
{
Response.Write("<script language=javascript>alert('您输入的不是数字,请输入正确的格式');</script>");
return;
}
cm.Connection.Open();
try
{
cm.ExecuteNonQuery();
}
catch(SqlException)
{
Response.Write("添加失败!");
}
finally
{
cm.Connection.Close();
}
}
}
总是显示添加失败。数据库定义为:
3 UID varchar 50 0
0 UName varchar 50 1
0 UPassWord varchar 50 1
0 UBirthday datetime 8 1
0 UMarriage varchar 50 1
0 UWorktime varchar 50 1
0 UIntime datetime 8 1
0 UDepartment varchar 50 1
0 UPosition varchar 50 1
0 UPhone varchar 50 1 展开
{
if(Page.IsValid)
{
string strconn="Data Source=ASUS-PC;Database=pms;uid=sa;pwd=;";
SqlConnection cn=new SqlConnection(strconn);
string strsql="insert into personnel_information(UID,UName,UPassWord,UBirthday,UMarriage,UWorktime,UIntime,UDepartment,UPosition,UPhone)values(@UID,@UName,@UPassWord,@UBirthday,@UMarriage,@UWorktime,@UIntime,@UDepartment,@UPosition,@UPhone)";
SqlCommand cm=new SqlCommand (strsql,cn);
cm.Parameters.Add(new SqlParameter("@UID",SqlDbType.VarChar,50));
cm.Parameters["@UID"].Value=Tb_name.Text;
cm.Parameters.Add(new SqlParameter("@UName",SqlDbType.VarChar,50));
cm.Parameters["@UName"].Value=Tb_name.Text;
cm.Parameters.Add(new SqlParameter("@UPassWord",SqlDbType.VarChar,50));
cm.Parameters["@UPassWord"].Value=Tb_password.Text;
cm.Parameters.Add(new SqlParameter("@UBirthday",SqlDbType.DateTime,8));
cm.Parameters["@UBirthday"].Value=Tb_birth.Text;
try
{
DateTime dt = Convert.ToDateTime(Tb_birth.Text);
}
catch
{
Response.Write("<script language=javascript>alert('您输入的不是数字,请输入正确的格式');</script>");
return;
}
cm.Parameters.Add(new SqlParameter("@UMarriage",SqlDbType.VarChar,50));
cm.Parameters["@UMarriage"].Value=Tb_name.Text;
cm.Parameters.Add(new SqlParameter("@RB_marriged",SqlDbType.VarChar,50));
cm.Parameters["@RB_marriged"].Value=Tb_name.Text;
if(RB_marriged.Checked)
{
cm.Parameters["@RB_marriged"].Value="是";
}
else if(RB_unmarriged.Checked)
{
cm.Parameters["@UMarriage"].Value="否";
}
cm.Parameters.Add(new SqlParameter("@UWorktime",SqlDbType.VarChar,50));
cm.Parameters["@UWorktime"].Value=Te_date.Text;
cm.Parameters.Add(new SqlParameter("@UIntime",SqlDbType.DateTime,8));
cm.Parameters["@UIntime"].Value=Tbx_intime.Text;
try
{
DateTime dt = Convert.ToDateTime(Tbx_intime.Text);
}
catch
{
Response.Write("<script language=javascript>alert('您输入的不是数字,请输入正确的格式');</script>");
return;
}
cm.Connection.Open();
try
{
cm.ExecuteNonQuery();
}
catch(SqlException)
{
Response.Write("添加失败!");
}
finally
{
cm.Connection.Close();
}
}
}
总是显示添加失败。数据库定义为:
3 UID varchar 50 0
0 UName varchar 50 1
0 UPassWord varchar 50 1
0 UBirthday datetime 8 1
0 UMarriage varchar 50 1
0 UWorktime varchar 50 1
0 UIntime datetime 8 1
0 UDepartment varchar 50 1
0 UPosition varchar 50 1
0 UPhone varchar 50 1 展开
1个回答
展开全部
try
{
cm.ExecuteNonQuery();
}
catch(SqlException)
{
Response.Write("添加失败!");
}
这说明你在执行cm.ExecuteNonQuery()有错,那么,肯定是这里有错:
cm.Parameters.Add(new SqlParameter("@UWorktime",SqlDbType.VarChar,50));
cm.Parameters["@UWorktime"].Value=Te_date.Text;
cm.Parameters.Add(new SqlParameter("@UIntime",SqlDbType.DateTime,8));
cm.Parameters["@UIntime"].Value=Tbx_intime.Text;
一是检查一下你的数据库字段,二是检查一下这几句有没有错.
{
cm.ExecuteNonQuery();
}
catch(SqlException)
{
Response.Write("添加失败!");
}
这说明你在执行cm.ExecuteNonQuery()有错,那么,肯定是这里有错:
cm.Parameters.Add(new SqlParameter("@UWorktime",SqlDbType.VarChar,50));
cm.Parameters["@UWorktime"].Value=Te_date.Text;
cm.Parameters.Add(new SqlParameter("@UIntime",SqlDbType.DateTime,8));
cm.Parameters["@UIntime"].Value=Tbx_intime.Text;
一是检查一下你的数据库字段,二是检查一下这几句有没有错.
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询