一个菜鸟请问:为什么总是执行到catch这一句,高分相求
下面一个小程序,
private void btnAccept_Click(object sender, System.EventArgs e)
{
if(Page.IsValid)
{
//save new user to the database
SqlConnection con;
string sql;
SqlCommand cmd;
StringBuilder sb=new StringBuilder();
ArrayList values=new ArrayList();
sb.Append("INSERT INTO[User]");
sb.Append("(UserID,Login,Password,FirstName,LastName,");
sb.Append("CellNumber,DateOfBirth)");
sb.Append("VALUES('{0}','{1}','{2}','{3}','{4}','{5}','{6}','{7}',");
sb.Append("{8},{9},{10})");
//add reqired values to replace
values.Add(Guid.NewGuid().ToString());
values.Add(txtLogin.Text);
values.Add(txtPwd.Text);
values.Add(txtFName.Text);
values.Add(txtLName.Text);
values.Add(txtPhone.Text);
values.Add(txtEmail.Text);
values.Add(0);
//add the optional values or Null
if(txtAddress.Text!=string.Empty) values.Add("'"+txtAddress.Text+"'");
else values.Add("NULL");
if(txtMobile.Text!=string.Empty) values.Add("'"+txtMobile.Text+"'");
else values.Add("NULL");
if(txtBirth.Text!=string.Empty) values.Add("'"+txtBirth.Text+"'");
else values.Add("NULL");
//Format the string with the array of values
sql=String.Format(sb.ToString(),values.ToArray());
//Connect and execute the query
con=new SqlConnection("data source=Joyball;initial catalog=Friends;user id=sa");
cmd=new SqlCommand(sql,con);
con.Open();
bool doredirect=true;
try
{
cmd.ExecuteNonQuery();
}
catch
{
doredirect=false;
this.lblMessage.Visible=true;
this.lblMessage.Text="Insert couldn't be performed,User name may be already taken.";
}
finally
{
con.Close();
}
if(doredirect)
Response.Redirect("Login.aspx");
}//end if
else
{
lblMessage.Text="FIx thefollowing errors and retry:";
}//end else
}
执行过之后,总是出现“Insert couldn't be performed,User name may be already taken.”,而我的数据库中什么数据都没有,请教各位高人