|
|
|
update ข้อมูลในgridviewไม่ได้ค่ะ แต่สามารถลบหรือเพิ่มข้อมูลได้ตามปกติ |
|
|
|
|
|
|
|
พอแก้ไขข้อมูลเสร็จ เมื่อกด บันทึก มันก็ไม่ยอมบันทึกให้ค่ะ จะเด้งกลับมาเป็นค่าเดิม
แต่สามารถลบ หรือ เพิ่มข้อมูลได้ตามปกติค่ะ
ลองใส่ if(!Page.IsPostBack) แล้วแต่ทำไมยังupdateไม่ได้อีกล่ะค่ะ
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 StaffDate : 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)
{
con.Open();
String sqlSelect2 = "select StaffdateId, StaffLeaveStart, StaffLeaveFinish, StaffTimeStart, StaffTimeFinish, StaffLeaveNote from Staffdate where StaffId = '" + Session["sessionStaffId"] + "'";
da = new SqlDataAdapter(sqlSelect2, con);
con.Close();
if (!Page.IsPostBack)
{
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("lblStaffLeaveStart") 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;
ddlEditTimeFinish.Text = lblStaffTimeFinish.Text;
txtEditLeaveNote.Text = lblStaffLeaveNote.Text;
}
protected void modCancelCommand(object sender, GridViewCancelEditEventArgs e)
{
grdStaffdate.EditIndex = -1;
grdStaffdate.ShowFooter = true;
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);
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 (StaffId, StaffLeaveStart, StaffLeaveFinish, StaffTimeStart, StaffTimeFinish, StaffLeaveNote)" +
"values ('" + Session["sessionStaffId"] + "','" + txtAddLeaveStart.Text + "','" + txtAddLeaveFinish.Text + "','" + ddlAddTimeStart.Text + "','" + ddlAddTimeFinish.Text + "', '" + txtAddLeaveNote.Text + "')";
SqlCommand com = new SqlCommand(sqlInsert, con);
com.ExecuteNonQuery();
con.Close();
BindData();
}
}
}
}
Tag : .NET, Ms SQL Server 2008, Web (ASP.NET), C#
|
|
|
|
|
|
Date :
2011-01-26 12:58:33 |
By :
... |
View :
1666 |
Reply :
1 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Load balance : Server 01
|