2014-01-13 149 views
1

我正在做這個check_Clicked過程來更新一個ListView,但更新似乎無法工作後,執行cmd.ExecuteNonQuery();當我調試時,我可以看到keyId值,它是我表中正確的Id,但是由於未知原因它沒有執行更新。任何暗示爲什麼?由於執行後沒有什麼更新cmd.ExecuteNonQuery()

protected void Check_Clicked(Object sender, EventArgs e) 
{ 
    int keyId = 0; 

    foreach (ListViewDataItem item in ListView1.Items) 
    { 
     CheckBox MyCheckBox = (CheckBox)item.FindControl("MyCheckBox"); 
     if (MyCheckBox.Checked) 
     { 
      keyId = Convert.ToInt32(ListView1.DataKeys[item.DataItemIndex].Value); 
     } 
    } 

    SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager. 
     ConnectionStrings["ConnectionString"].ConnectionString); 

    //======= Insert Query. 
    string cmdText = "UPDATE Doctor SET [email protected],[email protected], 
     [email protected],[email protected] WHERE [email protected]"; 

    SqlCommand cmd = new SqlCommand(cmdText, con); 

    cmd.Parameters.AddWithValue("@clientName", Convert.ToString(Session["nom"])); 
    cmd.Parameters.AddWithValue("@isAvailable", "False"); 

    cmd.Parameters.AddWithValue("@apointementId", keyId); 

    if (con.State == ConnectionState.Closed) 
    { 
     con.Open(); 
    } 
    cmd.ExecuteNonQuery(); 
    con.Close(); 
    ListApointement(); 
} 

private void ListApointement() 
{ 
    using (ListApointementDataContext db = new ListApointementDataContext()) 
    { 
     var aptItems = from ListApointement in db.ListApointement select ListApointement; 
     ListView1.DataSourceID = null; 
     ListView1.DataSource = aptItems; 
     ListView1.DataBind(); 
    }; 
} 
+0

進入調試並粘貼SQL,它在那裏工作? –

+0

'aptItems' vs'rdvItems'? –

+2

看起來像很多缺少的參數給我... – ganders

回答

1

您的查詢,因爲它是現在,看起來是這樣的:

string cmdText = "UPDATE Doctor SET [email protected],[email protected], 
    [email protected],[email protected] WHERE [email protected]"; 

它看起來像你對我是包括你是不是在你的查詢中使用參數:

cmd.Parameters.AddWithValue("@isAvailable", "False"); 
// You aren't passing this in your query but have it listed as a parameter... 

並且在您的查詢中引用了您未使用的參數:

cmd.Parameters.AddWithValue("@doctorName", // @doctorName type needs to go here); 
cmd.Parameters.AddWithValue("@dateApt", // @dateApt type needs to go here); 
cmd.Parameters.AddWithValue("@hrApt", // @hrApt type needs to go here); 
+0

sql怎麼沒有生成錯誤?我添加了缺失的列。 string cmdText =「UPDATE Doctor SET doctorName = @ doctorName,dateApt = @ dateApt, hrApt = @ hrApt,clientname = @ clientName,isAvailable = @ isAvailable WHERE apointmentId = @ apointementId」; – user3127986

+0

但沒有更多的成功數據庫仍然沒有更新 – user3127986

+0

你能解釋我如何看到使用Visual Studio 2013在後面生成的SQL代碼? – user3127986

相關問題