执行cmd.ExecuteNonQuery()后没有任何更新

时间:2014-01-13 21:25:40

标签: c# asp.net sql

我正在执行此check_Clicked过程来更新ListView,但是在cmd.ExecuteNonQuery()的exec之后,更新似乎不起作用;当我调试时,我可以看到keyId值,这是我表中的正确ID,但由于不明原因,它没有进行更新。任何提示为什么?感谢

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 doctorName=@doctorName,dateApt=@dateApt,
       hrApt=@hrApt,clientname=@clientName WHERE apointmentId=@apointementId";

    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();
   };
}

1 个答案:

答案 0 :(得分:1)

您的查询现在看起来像这样:

string cmdText = "UPDATE Doctor SET doctorName=@doctorName,dateApt=@dateApt,
   hrApt=@hrApt,clientname=@clientName WHERE apointmentId=@apointementId";

在我看来,您要在查询中包含一个未使用的参数:

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);