public partial class addstu : System.Web.UI.Page
{
SqlConnection connections;
protected void Page_Load(object sender, EventArgs e)
{
if (Session["user_id"] == null)
{
Response.Redirect("default.aspx");
}
else if ((int)Session["user_lever"] == 0)
{
Response.Redirect("default.aspx");
}
this.Title = "学生管理——添加新生";
dropNation.Items.Add("汉族");
dropNation.Items.Add("回族");
dropNation.Items.Add("满族");
dropNation.Items.Add("白族");
dropNation.Items.Add("藏族");
dropNation.Items.Add("维吾尔族");
dropNation.Items.Add("苗族");
dropNation.Items.Add("壮族");
string strconn = @"data source=.;Initial Catalog=jiaowums;Integrated Security=True";
//string strconn = System.Configuration.ConfigurationManager.AppSettings["connstring"];
SqlConnection connections0 = new SqlConnection(strconn);
if (!IsPostBack)
{
connections0.Open();
string mysql = "select * from tabclas";
SqlCommand commnd1 = new SqlCommand(mysql, connections0);
SqlDataReader sdreader1 = commnd1.ExecuteReader();
while (sdreader1.Read())
{
dropClass.Items.Add(new ListItem(sdreader1["Cname"].ToString(), sdreader1["Cid"].ToString()));
}
connections0.Close();
}
// 在此处放置用户代码以初始化页面
connections = new SqlConnection(strconn);
}
protected void lbtnOK_Click(object sender, EventArgs e)
{
if (txtNo.Text == "" || txtBirthday.Text=="" || txtTime.Text ==""|| txtBirthday.Text =="")
{
Response.Write("alert('除【备注】字段外,其他字段必填!');");
return;
}
SqlCommand commnd=new SqlCommand("addstu",connections);
commnd.CommandType=CommandType.StoredProcedure;
try
{
commnd.Parameters.Add(new SqlParameter("@sid",SqlDbType.VarChar,50));
commnd.Parameters["@sid"].Value=txtNo.Text;
commnd.Parameters.Add(new SqlParameter("@sname",SqlDbType.VarChar,50));
commnd.Parameters["@sname"].Value=txtName.Text;
commnd.Parameters.Add(new SqlParameter("@ssex",SqlDbType.Char,10));
commnd.Parameters["@ssex"].Value = radlSex.SelectedItem.Text;
commnd.Parameters.Add(new SqlParameter("@snati",SqlDbType.Char,10));
commnd.Parameters["@snati"].Value = dropNation.Text;
commnd.Parameters.Add(new SqlParameter("@sbir",SqlDbType.VarChar,50));
commnd.Parameters["@sbir"].Value=txtBirthday.Text;
commnd.Parameters.Add(new SqlParameter("@sclaid", SqlDbType.VarChar, 50));
commnd.Parameters["@sclaid"].Value = dropClass.SelectedValue;
commnd.Parameters.Add(new SqlParameter("@stime",SqlDbType.VarChar,50));
commnd.Parameters["@stime"].Value=txtTime.Text;
commnd.Parameters.Add(new SqlParameter("@sdep", SqlDbType.VarChar, 50));
commnd.Parameters["@sdep"].Value = txtAddress.Text;
commnd.Parameters.Add(new SqlParameter("@scol", SqlDbType.VarChar, 50));
commnd.Parameters["@scol"].Value = txtElse.Text;
commnd.Connection.Open();
commnd.ExecuteNonQuery();
commnd.Connection.Close();
//insertstudent();
}
catch
{
Response.Write("alert('添加新记录失败!');");
}
}
public void insertstudent()
{
string strsl="insert into users(User_id,User_password,User_lever) values(@User_id,@User_password,0)";
SqlCommand commnd=new SqlCommand(strsl,connections);
try
{
commnd.Parameters.Add(new SqlParameter("@User_id",SqlDbType.VarChar,50));
commnd.Parameters["@User_id"].Value=txtNo.Text;
commnd.Parameters.Add(new SqlParameter("@User_password",SqlDbType.VarChar,50));
commnd.Parameters["@User_password"].Value=txtNo.Text;
commnd.Connection.Open();
commnd.ExecuteNonQuery();
Response.Redirect("admin_stu.aspx");
commnd.Connection.Close();
}
catch(SqlException)
{
Response.Write("<script language=javascript>alert('添加新记录失败!');</script>");
}
}
protected void lbtnReset_Click(object sender, EventArgs e)
{
Response.Redirect("addstu.aspx");
}
protected void lbtnBack_Click(object sender, EventArgs e)
{
Response.Redirect("admin_stu.aspx");
}
protected void CustomValidator1_ServerValidate(object source, ServerValidateEventArgs args)
{
string strconn = @"data source=.;Initial Catalog=jiaowums;Integrated Security=True";
//string strconn = System.Configuration.ConfigurationManager.AppSettings["connstring"];
connections = new SqlConnection(strconn);
connections.Open();
SqlCommand commnd = new SqlCommand("selstu", connections);
commnd.CommandType = CommandType.StoredProcedure;
commnd.Parameters.Add("@sid", SqlDbType.VarChar, 50);
commnd.Parameters["@sid"].Value = txtNo.Text;
SqlDataReader sdreader = commnd.ExecuteReader();
if (sdreader.Read())
{
args.IsValid = false;
}
else
{
args.IsValid = true;
}
connections.Close();
}
}