正确代码如下:
protected void Button1_Click(object sender, EventArgs e)
{
if (TextBox1.Text == "" || TextBox2.Text == "")
{
Response.Write("<script defer>alert('请每项都输入!');</script>");
return;//跳出
}
SqlConnection sqlconn = new SqlConnection(ConfigurationManager.ConnectionStrings["labConnectionString"].ConnectionString);
if (Session["action"].ToString() == "edit")
{
string strCmd = "update lab_dept set dept_ID='"+TextBox1.Text+"',dept_name='"+TextBox2.Text+"' where dept_num='"+int.Parse(Session["deptnum"].ToString())+"'";
SqlCommand myComm = new SqlCommand(strCmd, sqlconn);
myComm.Connection.Open();
myComm.ExecuteNonQuery();
}
else if (Session["action"].ToString() == "add")
{
string strC = "select * from lab_dept where dept_ID='"+TextBox1.Text+"' or dept_name='"+TextBox2.Text+"'";
SqlCommand myC = new SqlCommand(strC, sqlconn);
myC.Connection.Open();
object a = myC.ExecuteScalar();//把a改为object类
if (a != null)
{
Response.Write("<script defer>alert('您输入的已存在,请重新输入!');</script>");
return;
}
else
{
string strCmd = "insert lab_dept(dept_ID,dept_name) values('" + TextBox1.Text + "','" + TextBox2.Text + "')";
SqlCommand myComm = new SqlCommand(strCmd, sqlconn);
myComm.ExecuteNonQuery();
myComm.Connection.Close();
}
}
Response.Redirect("Dept.aspx");
[此贴子已经被作者于2007-3-30 16:23:53编辑过]