更新查询无法正常工作C#asp.net

时间:2012-09-26 09:55:06

标签: c# asp.net sql-server sql-update

在此页面中仅显示此查询

c1.cmd.CommandText = "update mechprofile set mech_status ='busy' where mech_regid ='" + alotmech + "'";

正在运行,其余查询无效。

public partial class customercare_alotmechanic : System.Web.UI.Page
{
    Class1 c = new Class1();
    Class1 c1 = new Class1();

    int sno;
    string license;
    string status;
    string alotmech;
    string mechregid;
    string mrg;
    protected void Page_Load(object sender, EventArgs e)
    {
            try
            {


                sno = Convert.ToInt32((Request.Form["sno"]));
                status = Request.Form["sta"];
                alotmech = Request.Form["Sel"];
                mrg = Request.Form["mechregid"];

                if (alotmech.Equals("Alloted"))
                {

                    c.con.Open();
                    c.cmd.CommandText = "update probprofile set Status = 'done' where Sno ='" + sno + "'";
                    c.cmd.ExecuteNonQuery();
                    c.con.Close();

                    c.con.Open();
                    c.cmd.CommandText = "update mechprofile set mech_status = 'free' where mech_regid ='" + mrg + "'";
                    c.cmd.ExecuteNonQuery();
                    c.con.Close();
                }
                else
                {
                    c.con.Open();
                    c.cmd.CommandText = "update probprofile set mechregid = '" + alotmech + "' where Sno ='" + sno + "'";
                    c.cmd.ExecuteNonQuery();
                    c.con.Close();

                    c1.con.Open();
                    c1.cmd.CommandText = "update mechprofile set mech_status ='busy' where mech_regid ='" + alotmech + "'";
                    c1.cmd.ExecuteNonQuery();
                    c1.con.Close();
                }




            }


            finally
            {
                string strScript = "<script>";
                strScript += "alert('ALOT MECHANIC PAGE..');";
                strScript += "window.location='problemstatus.aspx';";
                strScript += "</script>";
                Page.RegisterClientScriptBlock("strScript", strScript);

            }
    }
}

在上面的代码更新命令不起作用...我在页面上有一个表单,其中method = post和action se到此页面,运行更新查询。

2 个答案:

答案 0 :(得分:0)

检查参数的值是否正确:

If your data type is Nvarhcar so you should use N before your constants data. i.e.
"... column= N'"+ value+ "'"
Or if your data type is int so don't use '. i.e.
"... column= "+ value

答案 1 :(得分:0)

您是否测试了后缀为查询语句的变量值?例如,在使用这些值进行更新之前,请检查sno,status和mrg是否实际包含有效值。如果它们中的任何一个为null或包含关于表设计的不可接受的值,则查询将失败。

你得到的错误陈述是什么?我看到你有一个尝试...最后但没有抓住。您应该始终捕获异常以促进更好的错误处理。