0

您好,请看以下代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;

public partial class viewdutyslip : System.Web.UI.Page
{

 SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["constr"].ToString());

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            LoadData();
        }
    }

    protected void LoadData()
    {
        SqlDataAdapter da = new SqlDataAdapter("select duty_slip_no, slip_date, company, guest, booked_by, car_no, driver_name, meter_start, DATEDIFF(Day,start_date,end_date), toll from add_duty_slip", con);
        DataSet ds = new DataSet();
        da.Fill(ds);

        GridView1.DataSource = ds;
        GridView1.DataBind();
    }

    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        Label duty_slip_no = (Label)GridView1.Rows[e.RowIndex].FindControl("lbldutyslip");
        SqlCommand cmd = new SqlCommand("delete from add_duty_slip where duty_slip_no='" + duty_slip_no.Text + "'",con);
        con.Open();
        cmd.ExecuteNonQuery();
        con.Close();
        GridView1.EditIndex = -1;

        LoadData();
        lblMessage.Text = "Successfully Deleted";

    }

    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {
        GridView1.EditIndex = e.NewEditIndex;
        LoadData();
    }

    protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {

        Label duty_slip_no = (Label)GridView1.Rows[e.RowIndex].FindControl("lbldutyslip");
        TextBox slip_date = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtdate");
        TextBox company = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtcompany");
        TextBox guest = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtguest");
        TextBox booked_by = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtbooked");
        TextBox car_no = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtcarno");
        TextBox driver_name = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtdrivername");
        TextBox meter_start = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtmeter");
        TextBox toll = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txttoll");

        SqlCommand cmd = new SqlCommand("update add_duty_slip set slip_date='"+slip_date.Text+"',company='"+company.Text+"',guest='"+guest.Text+"',booked_by='"+booked_by.Text+"',car_no='"+car_no.Text+"', driver_name='"+driver_name.Text+"',meter_start='"+meter_start.Text+"',toll='"+toll.Text+"' where duty_slip_no='"+GridView1.DataKeys[e.RowIndex].Values[0].ToString()+"'",con);
        con.Open();
        cmd.ExecuteNonQuery();
        GridView1.EditIndex = -1;
        con.Close();
        lblMessage.Text = "Update Successfully";

    }


    protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        GridView1.EditIndex = -1;
        LoadData();
    }

    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {

    }


}

我想更新我的 Gridview 值。但它给出了一个错误“对象引用未设置为对象的实例”。

4

1 回答 1

1

试试这个,它可以在我的机器上在你的 aspx.cs 页面上运行:

 protected void GridView1_RowUpdating1(object sender, GridViewUpdateEventArgs e)
{
    string id = GridView1.DataKeys[e.RowIndex].Values["duty_slip_no"].ToString();
    Label duty_slip_no = (Label)GridView1.Rows[e.RowIndex].FindControl("lbldutyslip");
    TextBox txtdate = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtdate");

    SqlCommand cmd = new SqlCommand("update add_duty_slip set slip_date='" + txtdate.Text + "',company='" + txtcompany.Text + "' where duty_slip_no=" + id, con);
    con.Open();
    cmd.ExecuteNonQuery();
    GridView1.EditIndex = -1;
    con.Close();
    lblMessage.Text = "Update Successfully";
}

在 aspx 页面上:

   <asp:GridView ID="GridView1" DataKeyNames="duty_slip_no" runat="server" AutoGenerateDeleteButton="True" 
        AutoGenerateEditButton="True" onrowcancelingedit="GridView1_RowCancelingEdit" 
        onrowdeleting="GridView1_RowDeleting1" onrowediting="GridView1_RowEditing1" 
        onrowupdated="GridView1_RowUpdated" onrowupdating="GridView1_RowUpdating1" 
        AutoGenerateColumns="False">
        <Columns>
         <asp:TemplateField HeaderText="Date">
          <EditItemTemplate>
           <asp:TextBox ID="txtdate" runat="server" Text='<%#Eval("slip_date") %>'/>
           </EditItemTemplate>
          <ItemTemplate>
           <asp:Label ID="lbldate" runat="server" Text='<%#Eval("slip_date") %>'/>
        </ItemTemplate>
             </asp:TemplateField>
              <asp:TemplateField HeaderText="company">
               <EditItemTemplate>
              <asp:TextBox ID="txtcompany" runat="server" Text='<%#Eval("company") %>'/>
         </EditItemTemplate>
        <ItemTemplate>
       <asp:Label ID="lblcompany" runat="server" Text='<%#Eval("company") %>'/>
       </ItemTemplate>
      </asp:TemplateField>
       </Columns>
     </asp:GridView>

其他列也一样,它也适用于我的机器

于 2013-09-13T07:37:52.343 回答