62,046
社区成员
发帖
与我相关
我的任务
分享
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:strConnection1 %>"
SelectCommand="SELECT [ExamId], [ExamName] FROM [Examinations]"></asp:SqlDataSource>
<asp:DropDownList ID="DropDownList1" runat="server" DataSourceID="SqlDataSource1"
DataTextField="ExamName" DataValueField="ExamId" DataMember="DefaultView">
</asp:DropDownList>
<br />
<asp:Button ID="Button1" runat="server" Text="提交" OnClick="Button1_Click" />
protected void Button1_Click(object sender, EventArgs e)
{
string strConnection = ConfigurationSettings.AppSettings["strConnection"];
SqlConnection con = new SqlConnection(strConnection);
con.Open();
string strselect = "select StuId from Registration where ExamId='"+ this.DropDownList1.SelectedValue +"'";
SqlCommand cmd = new SqlCommand(strselect, con);
DataSet ds = new DataSet();
SqlDataAdapter da = new SqlDataAdapter(strselect, con);
da.Fill(ds, "Registration");
try
{
//是否重复报名
if (Session["User"].ToString() == ds.Tables[0].Rows[0].ItemArray[0].ToString().Trim())
{
con.Close();
Response.Write("<script language=javascript>alert('您已经报名!')</script> ");
}
else
{
string strinsert = "insert into Registration (StuId,ExamId,RegTime) values('" + Session["User"].ToString() + "','" + this.DropDownList1.SelectedValue + "','" + DateTime.Now.ToString("yyyy-MM-dd") + "')";
SqlCommand cmd1 = new SqlCommand(strinsert, con);
cmd1.ExecuteNonQuery();
con.Close();
Response.Write("<script language=javascript>alert('报名成功!')</script> ");
}
}
//错误处理
catch
{
con.Close();
Response.Write("<script language=javascript>alert('出错了!')</script> ");
}
CREATE PROC spCheckAndUpdate
(
@StuId VARCHAR(50),
@ExamId VARCHAR(50)
)
AS
IF NOT EXISTS (SELECT 1 FROM Registration WHERE StuId = @StuId AND ExamId = @ExamId) BEGIN
INSERT INTO Registration(StuId, ExamId, RegTime) VALUES (@StuId, @ExamId, GETDATE())
SELECT 0
END ELSE BEGIN
SELECT 1
END
protected void Button1_Click(object sender, EventArgs e)
{
//string strConnection = @"server=XXY\SQLEXPRESS;database=XSample;Integrated Security=SSPI";
string strConnection = ConfigurationSettings.AppSettings["strConnection"];
using (SqlConnection conn = new SqlConnection(strConnection))
{
SqlCommand comm = new SqlCommand();
comm.Connection = conn;
comm.CommandText = "spCheckAndUpdate";
comm.CommandType = CommandType.StoredProcedure;
comm.Parameters.AddRange(
new SqlParameter[] {
new SqlParameter("@StuId", Session["User"].ToString()),
new SqlParameter("@ExamId", this.DropDownList1.SelectedValue),
});
//comm.Parameters.AddRange(
// new SqlParameter[] {
// new SqlParameter("@StuId", "X.X.Y1"),
// new SqlParameter("@ExamId", "001"),
// });
conn.Open();
int fResult = (int)comm.ExecuteScalar();
string fMsg = string.Format("alert('{0}');", (fResult == 0) ? "报名成功!" : "您已经报名!");
this.ClientScript.RegisterStartupScript(typeof(string), "exist", fMsg, true);
}
}