为什么Update命令在gridview中不起作用

时间:2013-02-12 05:42:55

标签: c# asp.net

<
asp:GridView ID="GridView1" runat="server" Width="550px" AutoGenerateColumns="False"

 AlternatingRowStyle-BackColor="#C2D69B" HeaderStyle-BackColor="green" 

 ShowFooter="True" EnableModelValidation="True" Height="183px"
        DataKeyNames="id"  OnRowEditing="EditRecord"

 OnRowCancelingEdit="CancelRecord" OnRowUpdating="UpdateRecord" 

  OnRowDeleting="DeleteRecord">'


protected void UpdateRecord(object sender, GridViewUpdateEventArgs e)

 {

string strConnString = 

ConfigurationManager.ConnectionStrings["dbConnectionString"].ConnectionString;

SqlConnection conn = new SqlConnection(strConnString);
    SqlCommand com;

    GridViewRow row = (GridViewRow)GridView1.Rows[e.RowIndex];

    int autoid = Int32.Parse(GridView1.DataKeys[e.RowIndex].Value.ToString());

    TextBox tbranchname = (TextBox)GridView1.Rows[e.RowIndex].FindControl("tbbranchname");

    TextBox tsemno = (TextBox)GridView1.Rows[e.RowIndex].FindControl("tbsemno");
    TextBox tscode = (TextBox)GridView1.Rows[e.RowIndex].FindControl("tbscode");
    TextBox tsname = (TextBox)GridView1.Rows[e.RowIndex].FindControl("tbsname");
    TextBox tstype = (TextBox)GridView1.Rows[e.RowIndex].FindControl("tbstype");



    try
    {



        conn = new SqlConnection(strConnString);



        conn.Open();
        string query = "UPDATE [Semester_Wise_Subject] SET branch_name=@branchname,sem_no=@semno,sname=@sname,scode=@scode,subject_type=@stype WHERE (id=@attid) ";

        // string query = "UPDATE Batches_Allocation SET batch_type=@batchtype,branch_name=@branchname,sem_no=@semno,stu_distribution=@studis WHERE (batch_year=@attid AND batch_name=@attid1) ";

        com = new SqlCommand(query, conn);

       com.Parameters.Add("@branchname", tbranchname.Text.ToString());
        com.Parameters.Add("@semno", tsemno.Text.ToString());
        com.Parameters.Add("@sname", tsname.Text.ToString());
        com.Parameters.Add("@scode", tscode.Text.ToString());
        com.Parameters.Add("@stype", tstype.Text.ToString());

        com.Parameters.Add("@attid", autoid);
        // com.Parameters.Add("@attid1", tbatchname.Text.ToString());



        n = com.ExecuteNonQuery();



        lblMessageLine.Text = n + "" + "Record Updated successfully.";
        lblMessageLine.ForeColor = Color.Green;




        // Refresh the data

        GridView1.EditIndex = -1;

        BindData();

    }

    catch (SqlException ee)
    {

        lblMessageLine.Text = ee.Message;
        lblMessageLine.Visible = true;
        lblMessageLine.ForeColor = Color.Green;

    }

    finally
      {
        //  lblMessageLine.Text = n + "" + "Record Updated successfully.";
       //   lblMessageLine.ForeColor = Color.Green;


          conn.Close();

          conn.Dispose();

      }


}

删除命令正在运行,但更新无效,在我的表中“id”是p.k ..

0 个答案:

没有答案