注册 登录
编程论坛 ASP.NET技术论坛

无语的错误!特急!

雪雨星风 发布于 2008-06-14 15:34, 603 次点击
protected void Button1_Click(object sender, EventArgs e)
    {
        OleDbConnection myconn = DB.CreateDB();
        myconn.Open();
        OleDbCommand mycomm = new OleDbCommand("select * from jieguo where [user_name] = '" + Session["name"].ToString() + "'", myconn);  //验证是否已经考试过
        int number = Convert.ToInt32(mycomm.ExecuteScalar());
        if (number > 0)
        {            //若考试过就用这次的答案更新以前的答案
            try
            {
                OleDbConnection myconnection = DB.CreateDB();
                myconnection.Open();
                for (int i = 0; i < this.DataList1.Items.Count; i++)
                {
                    Label l = (Label)this.DataList1.Items[i].FindControl("Lb");
                    TextBox tx = (TextBox)this.DataList1.Items[i].FindControl("Txt");
                    OleDbCommand mycommand = new OleDbCommand("update jieguo set zy_ID = " + Convert.ToInt32(l.Text) + ",[user_name] = '" + Session["name"].ToString() + "',body = '" + tx.Text + "',[time] = '" + DateTime.Now + "'", myconnection);
                    int num = mycommand.ExecuteNonQuery();
                }
                Response.Write("<script language=javascript>window.alert('恭喜你交卷成功!');window.location('login.aspx')</script>");
                myconnection.Close();
            }
            catch (Exception ex)
            {
                Response.Write(ex.Message);
            }
        }
        else
        {        //若没考试过就往数据库里插入答案
            try
            {
                OleDbConnection myconnection = DB.CreateDB();
                myconnection.Open();
                for (int i = 0; i < this.DataList1.Items.Count; i++)
                {
                    Label la = (Label)this.DataList1.Items[i].FindControl("Lb");
                    TextBox tx = (TextBox)this.DataList1.Items[i].FindControl("Txt");
                    OleDbCommand mycommand = new OleDbCommand("insert into jieguo (zy_ID,[user_name],body,[time]) values (" + Convert.ToInt32(la.Text) + ",'" + Session["name"].ToString() + "','" + tx.Text + "','" + DateTime.Now + "')", myconnection);
                    int num = mycommand.ExecuteNonQuery();
                }
                Response.Write("<script language=javascript>window.alert('恭喜你交卷成功!');window.location('login.aspx')</script>");
                myconnection.Close();
            }
            catch (Exception ex)
            {
                Response.Write(ex.Message);
            }
        }        
    }


//////////////////////////////////////////////////////////////////////////
//错误信息:(在数据库中查不到数据时)
语法错误 (操作符丢失) 在查询表达式 ''protected void Button1_Click(object sender, EventArgs e) { OleDbConnection myconn = DB.CreateDB(); myconn.Open(); OleDbCommand mycomm = new OleDbCommand("select * from jieguo where [user_name] = '" + Session["name"].ToStri' 中。
//////////////////////////////////////////////////////////////////////////
//错误信息:(在数据库中查到数据时)
不提示错误,但是数据库中有多少条数据就被更新多少条,并且更新的结果都是最后一题的答案
1 回复
#2
冰彩虹2008-06-14 21:17
没听明白
1