โค้ดที่เขียนจะประมาณว่า ให้ผู้ใช้กรอกหมายเลยID แล้วจะแสดงข้อมูลในgridviewค่ะ
สามารถเรียกข้อมูลขึ้นมาแสดงได้ แต่พอกด แก้ไข หรือ ลบ ก็จะขึ้นerror แบบนี้ค่ะ
Object reference not set to an instance of an object.
ตัวอย่างโค้ดค่ะ
Code (C#)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
using System.Configuration;
namespace Dentist
{
public partial class Staff : System.Web.UI.Page
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["Dentist_Pool"].ConnectionString);
SqlDataAdapter da = null;
DataTable dt = null;
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnStaffsubmit_Click(object sender, EventArgs e)
{
IsStaffdate(txtStaffid.Text);
}
protected void IsStaffdate(string staffid)
{
con.Open();
String sqlSelect = "select StaffId from Staffdate where StaffId = @staffid";
SqlCommand com = new SqlCommand(sqlSelect, con);
com.Parameters.AddWithValue("@staffid", txtStaffid.Text);
using (IDataReader reader = com.ExecuteReader())
{
while (reader.Read())
{
Session["sessionStaffId"] = reader.GetInt32(0);
break;
}
reader.Close();
con.Close();
con.Open();
String sqlSelect2 = "select StaffdateId, StaffLeaveStart, StaffLeaveFinish, StaffTimeStart, StaffTimeFinish, StaffLeaveNote from Staffdate where StaffId = '" + Session["sessionStaffId"] + "'";
da = new SqlDataAdapter(sqlSelect2, con);
con.Close();
}
//con.Open();
//String sqlSelect = "select StaffdateId, StaffLeaveStart, StaffLeaveFinish, StaffTimeStart, StaffTimeFinish, StaffLeaveNote from Staffdate where StaffId = @staffid";
////SqlCommand com = new SqlCommand();
////com.Parameters.AddWithValue("@staffid", txtStaffid.Text);
//da = new SqlDataAdapter(sqlSelect, con);
//con.Close();
BindData();
}
void BindData()
{
dt = new DataTable();
da.Fill(dt);
grdStaffdate.DataSource = dt;
grdStaffdate.DataBind();
dt = null;
con.Close();
}
void Page_Unload()
{
con.Close();
con = null;
}
protected void modEditCommand(object sender, GridViewEditEventArgs e)
{
Label lblStaffLeaveStart = (sender as GridView).Rows[e.NewEditIndex].FindControl("lbllblStaffLeaveStartt") as Label;
Label lblStaffLeaveFinish = (sender as GridView).Rows[e.NewEditIndex].FindControl("lblStaffLeaveFinish") as Label;
Label lblStaffTimeStart = (sender as GridView).Rows[e.NewEditIndex].FindControl("lblStaffTimeStart") as Label;
Label lblStaffTimeFinish = (sender as GridView).Rows[e.NewEditIndex].FindControl("lblStaffTimeFinish") as Label;
Label lblStaffLeaveNote = (sender as GridView).Rows[e.NewEditIndex].FindControl("lblStaffLeaveNote") as Label;
grdStaffdate.EditIndex = e.NewEditIndex;
grdStaffdate.ShowFooter = false;
BindData();
TextBox txtEditLeaveStart = (sender as GridView).Rows[e.NewEditIndex].FindControl("txtEditLeaveStart") as TextBox;
TextBox txtEditLeaveFinish = (sender as GridView).Rows[e.NewEditIndex].FindControl("txtEditLeaveFinish") as TextBox;
DropDownList ddlEditTimeStart = (sender as GridView).Rows[e.NewEditIndex].FindControl("ddlEditTimeStart") as DropDownList;
DropDownList ddlEditTimeFinish = (sender as GridView).Rows[e.NewEditIndex].FindControl("ddlEditTimeFinish") as DropDownList;
TextBox txtEditLeaveNote = (sender as GridView).Rows[e.NewEditIndex].FindControl("txtEditLeaveNote") as TextBox;
txtEditLeaveStart.Text = lblStaffLeaveStart.Text;
txtEditLeaveFinish.Text = lblStaffLeaveFinish.Text;
ddlEditTimeStart.Text = lblStaffTimeStart.Text;
ddlEditTimeStart.Text = lblStaffTimeFinish.Text;
txtEditLeaveNote.Text = lblStaffLeaveNote.Text;
}
protected void modCancelCommand(object sender, GridViewCancelEditEventArgs e)
{
grdStaffdate.EditIndex = -1;
grdStaffdate.ShowFooter = false;
BindData();
}
protected void modDeleteCommand(object sender, GridViewDeleteEventArgs e)
{
con.Open();
String sqlDelete = "delete from Staffdate where StaffdateId = '" + grdStaffdate.DataKeys[e.RowIndex].Value + "'";
SqlCommand com = new SqlCommand(sqlDelete, con);
com.ExecuteNonQuery();
con.Close();
grdStaffdate.EditIndex = -1;
BindData();
}
protected void modUpdateCommand(object sender, GridViewUpdateEventArgs e)
{
TextBox txtEditLeaveStart = (TextBox)grdStaffdate.Rows[e.RowIndex].FindControl("txtEditLeaveStart");
TextBox txtEditLeaveFinish = (TextBox)grdStaffdate.Rows[e.RowIndex].FindControl("txtEditLeaveFinish");
DropDownList ddlEditTimeStart = (DropDownList)grdStaffdate.Rows[e.RowIndex].FindControl("ddlEditTimeStart");
DropDownList ddlEditTimeFinish = (DropDownList)grdStaffdate.Rows[e.RowIndex].FindControl("ddlEditTimeFinish");
TextBox txtEditLeaveNote = (TextBox)grdStaffdate.Rows[e.RowIndex].FindControl("txtEditLeaveNote");
con.Open();
String sqlUpdate = "update Staffdate set StaffLeaveStart = '" + txtEditLeaveStart.Text + "'" + " , StaffLeaveFinish = '" + txtEditLeaveFinish.Text + "'" + " , StaffTimeStart = '" + ddlEditTimeStart.Text + "'"
+ " , StaffTimeFinish ='" + ddlEditTimeFinish.Text + "'" + " , StaffLeaveNote = '" + txtEditLeaveNote.Text + "'" + " where StaffId = '" + grdStaffdate.DataKeys[e.RowIndex].Value + "'";
SqlCommand com = new SqlCommand(sqlUpdate, con);
com.ExecuteNonQuery();
con.Close();
grdStaffdate.EditIndex = -1;
grdStaffdate.ShowFooter = true;
BindData();
}
protected void modRowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "Add")
{
TextBox txtAddLeaveStart = (TextBox)grdStaffdate.FooterRow.FindControl("txtAddLeaveStart");
TextBox txtAddLeaveFinish = (TextBox)grdStaffdate.FooterRow.FindControl("txtAddLeaveFinish");
DropDownList ddlAddTimeStart = (DropDownList)grdStaffdate.FooterRow.FindControl("ddlAddTimeStart");
DropDownList ddlAddTimeFinish = (DropDownList)grdStaffdate.FooterRow.FindControl("ddlAddTimeFinish");
TextBox txtAddLeaveNote = (TextBox)grdStaffdate.FooterRow.FindControl("txtAddLeaveNote");
con.Open();
String sqlInsert = "insert into Staffdate (StaffLeaveStart, StaffLeaveFinish, StaffTimeStart, StaffTimeFinish, StaffLeaveNote)" +
"values ('" + txtAddLeaveStart.Text + "','" + txtAddLeaveFinish.Text + "','"+ ddlAddTimeStart.Text + "','" + ddlAddTimeFinish.Text + "', '" + txtAddLeaveNote.Text + "')";
SqlCommand com = new SqlCommand(sqlInsert,con);
com.ExecuteNonQuery();
con.Close();
BindData();
}
}
protected void btnStaffcancel_Click(object sender, EventArgs e)
{
txtStaffid.Text = "";
}
protected void btnStaffAdd_Click(object sender, EventArgs e)
{
Response.Redirect("~/Clinic/StaffAdd.aspx");
}
}
}