2012-09-26 69 views
0

在這個頁面中只有這個查詢更新查詢工作不C#asp.net

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和行動在頁面上本身對於這個頁面上更新查詢運行。

+1

命令如何不起作用?你是否收到錯誤?如果是這樣,錯誤是什麼?或者你正在查看數據庫並看到「UPDATE」沒有任何影響? –

+4

另外 - 以您正在做的方式構建SQL字符串是一個糟糕的主意 - 使用[參數化查詢](http://www.dotnetperls.com/sqlparameter) –

+1

+1對@ ElRonnoco關於參數化的評論要好得多查詢。請人們,每次你連接SQL像這樣,一隻小貓死亡:-) – jeroenh

回答

0

檢查您parammeters的價值是正確的:

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 
0

你測試你後面添加到查詢語句中的變量的值?例如,在使用這些值進行更新之前,請檢查sno,status和mrg是否實際上包含有效值。如果它們中的任何一個爲空或包含關於您的表格設計的不可接受的值,則查詢將失敗。

你得到的錯誤陳述是什麼?我看到你有一個嘗試......終於沒有趕上。你應該總是抓住異常來促進更好的錯誤處理。

+0

感謝您的建議......價值觀沒有通過,因爲我想....會回來一些改進 –