网格视图按钮asp.net c#

时间:2015-04-20 09:20:04

标签: gridview

我们希望通过接受按钮只接受gridview中的一条记录,但在我们的代码中,如果我们在最后一条记录中,则接受所有请求。请告诉我代码中的问题也提供解决方案

protected void ChangeStatus(object sender,EventArgs e)         {             var context = new healthCareContext();

        Button btn = (Button)sender;
        string str = btn.CommandName;

        var pemail = str;

        //////int row = GridView1.Rows.Count;
        ////GridView1.SelectedIndex =GridView1.Rows.Count;
        ////string mypemail = GridView1.SelectedRow.Cells[2].Text;

        String status="Accepted";
        if (btn.Text == "Accepted")
            btn.Visible = false;
        string newStatus = "";
        if (btn.Text == "Accept")
        {
            newStatus = "Accepted";
            Response.Write("appointment has been Accepted and added in the database table 'appointments'.");

        }


        using (context)
        {
            var results = (from a in context.appointments where a.pemail == pemail select a);

            foreach (var b in results)
            {
                b.astatus = newStatus;
                //string signupName = b.name;
            }
            //int row = GridView1.Rows.Count;
            //GridView1.Selected
           // string mypemail = GridView1.SelectedRow.Cells[2].Text;
            SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["healthCareContext"].ConnectionString);

            conn.Open();
            Console.WriteLine(conn);
            SqlCommand com = new SqlCommand("INSERT into appointments (astatus) values (@p1) where pemail==" + pemail + "), conn");
            com.Parameters.AddWithValue("@p1", status);
            com.ExecuteNonQuery();
            conn.Close();

            context.SaveChanges();

        }
        Response.AppendHeader("Refresh", "0");
    }

html标记

                                                                                                                                                                                                                         '的onClick =" ChangeStatus">                                                        " SelectCommand =" SELECT * FROM [约会]">

2 个答案:

答案 0 :(得分:0)

当您选择最后一行时,LINQ选择获取结果中的pemail参数可能为空。

答案 1 :(得分:0)

html标记

                                                                                                                                                                                                                         '的onClick =" ChangeStatus">                                                        " SelectCommand =" SELECT * FROM [约会]">