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