|
|
|
ASP.Net ลบข้อมลใน Gridview SQL server ครับ จากโค้ดของผมถ้าจะเพิ่มในส่วนของปุ่ม ลบ จะต้องปรับอย่างไรบ้างครับ |
|
|
|
|
|
|
|
จากโค้ดของผมถ้าจะเพิ่มในส่วนของปุ่ม ลบ จะต้องปรับอย่างไรบ้างครับ
ลองศึกษาดูเเละทำ เเต่ว่ามัน error ครับ
Code (C#)
<script runat="server">
SqlConnection objConn = new SqlConnection();
SqlCommand objCmd = new SqlCommand();
SqlDataAdapter dtAdapter = new SqlDataAdapter();
DataTable dt = new DataTable();
String strConnString, strSQL;
void Page_Load(object sender,EventArgs e)
{
if (Convert.ToString(Session["strUser"]) == "")
{
Response.Redirect("index.aspx");
Response.End();
}
if(!Page.IsPostBack)
{
BindData();
}
}
void BindData()
{
strConnString = "Server=localhost;UID=sa;PASSWORD=12345;database=mydatabase;Max Pool Size=400;Connect Timeout=600;";
strSQL = "SELECT * FROM AddRoom";
objConn.ConnectionString = strConnString;
objCmd.Connection = objConn;
objCmd.CommandText = strSQL;
objCmd.CommandType = CommandType.Text;
dtAdapter.SelectCommand = objCmd;
dtAdapter.Fill(dt);
myGridView.DataSource = dt;
myGridView.DataBind();
dtAdapter = null;
objConn.Close();
objConn = null;
}
void myGridView_RowCommand(Object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "Delete")
{
Label lblRoomID = (Label)(e.Rows.FindControl("lblRoomID"));
strSQL = "DELETE FROM AddRoom WHERE RoomID = '" + lblRoomID.Text + "' ";
objCmd = new SqlCommand(strSQL, objConn);
objCmd.ExecuteNonQuery();
BindData();
}
}
void myGridView_RowDataBound(Object s, GridViewRowEventArgs e)
{
Label lblRoomID = (Label)(e.Row.FindControl("lblRoomID"));
if (lblRoomID != null)
{
lblRoomID.Text = (string)DataBinder.Eval(e.Row.DataItem, "RoomID", "{0}");
}
Label lblRoom = (Label)(e.Row.FindControl("lblRoom"));
if (lblRoom != null)
{
lblRoom.Text = (string)DataBinder.Eval(e.Row.DataItem, "RoomName","{0}");
}
Label lblSize = (Label)(e.Row.FindControl("lblSize"));
if (lblSize != null)
{
lblSize.Text = (string)DataBinder.Eval(e.Row.DataItem, "Size", "{0}");
}
Label lblLocation = (Label)(e.Row.FindControl("lblLocation"));
if (lblLocation != null)
{
lblLocation.Text = (string)DataBinder.Eval(e.Row.DataItem, "Location", "{0}");
}
HyperLink hplDetail = (HyperLink)e.Row.FindControl("hplDetail");
if ((hplDetail != null))
{
hplDetail.NavigateUrl = "JavaScript:void(0);";
hplDetail.Attributes.Add("OnClick", "JavaScript:OpenPopup('popupDetailSchedule.aspx?RoomID=" + e.Row.DataItemIndex.ToString("RoomID").ToString() + "');");
}
HyperLink hplEdit = (HyperLink)(e.Row.FindControl("hplEdit"));
if (hplEdit != null)
{
hplEdit.NavigateUrl = "editRoomAdmin.aspx?RoomMeetID=" + (string)DataBinder.Eval(e.Row.DataItem, "RoomMeetID", "{0}");
}
LinkButton lnkDelete = (LinkButton)(e.Row.FindControl("lnkDelete"));
if (lnkDelete != null)
{
lnkDelete.Attributes.Add("OnClick", "return confirm('คุณต้องการลบหรือไม่?');");
}
}
void ShowPageCommand(Object s, GridViewPageEventArgs e)
{
myGridView.PageIndex = e.NewPageIndex;
BindData();
}
</script>
Tag : .NET, Web (ASP.NET), C#
|
|
|
|
|
|
Date :
2013-08-06 22:07:03 |
By :
Kaen17 |
View :
1072 |
Reply :
5 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ASP.NET GridView Control - RowCommand
เขียนแบบ Code Behide จะดีกว่าเยอะเลยครับ
|
|
|
|
|
Date :
2013-08-07 05:36:38 |
By :
mr.win |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ลบไม่ได้ครับ ไม่ error เเต่ไมสามารถออกได้ครับ ไม่รู้ว่าทำถูกรึป่าวครับ ช่วยหน่อยครับ
ขอบคุณครับ
Code (C#)
void BindData()
{
strSQL = "SELECT * FROM AddRoom";
objConn.ConnectionString = strConnString;
objCmd.Connection = objConn;
objCmd.CommandText = strSQL;
objCmd.CommandType = CommandType.Text;
dtAdapter.SelectCommand = objCmd;
dtAdapter.Fill(dt);
myGridView.DataSource = dt;
myGridView.DataBind();
dtAdapter = null;
}
void Page_UnLoad()
{
objConn.Close();
objConn = null;
}
void modDeleteCommand(Object sender, GridViewDeleteEventArgs e)
{
Label lblRoomID = (Label)myGridView.Rows[e.RowIndex].FindControl("lblRoomID");
strSQL = "DELETE FROM AddRoom WHERE RoomID = '" + lblRoomID.Text + "'";
myGridView.EditIndex = -1;
BindData();
}
|
ประวัติการแก้ไข 2013-08-07 11:03:27
|
|
|
|
Date :
2013-08-07 11:02:55 |
By :
Kaen17 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
error : ExecuteNonQuery requires an open and available Connection. The connection's current state is closed จะเเก้ไขอย่างไรครับ
Code (C#)
<script runat="server">
SqlConnection objConn = new SqlConnection();
SqlCommand objCmd = new SqlCommand();
SqlDataAdapter dtAdapter = new SqlDataAdapter();
DataTable dt = new DataTable();
String strConnString, strSQL;
void Page_Load(object sender,EventArgs e)
{
strConnString = "Server=localhost;UID=sa;PASSWORD=12345;database=mydatabase;Max Pool Size=400;Connect Timeout=600;";
if (Convert.ToString(Session["strUser"]) == "")
{
Response.Redirect("index.aspx");
Response.End();
}
if(!Page.IsPostBack)
{
BindData();
}
}
void BindData()
{
strSQL = "SELECT * FROM AddRoom";
objConn.ConnectionString = strConnString;
objCmd.Connection = objConn;
objCmd.CommandText = strSQL;
objCmd.CommandType = CommandType.Text;
objConn.Open();
dtAdapter.SelectCommand = objCmd;
dtAdapter.Fill(dt);
myGridView.DataSource = dt;
myGridView.DataBind();
dtAdapter = null;
objConn.Close();
objConn = null;
}
void modDeleteCommand(Object sender, GridViewDeleteEventArgs e)
{
Label lblRoomID = (Label)myGridView.Rows[e.RowIndex].FindControl("lblRoomID");
strSQL = "DELETE FROM AddRoom WHERE RoomID = '" + lblRoomID.Text + "'";
objCmd.Connection = objConn;
objCmd.CommandText = strSQL;
objCmd.CommandType = CommandType.Text; ;
objCmd.CommandText = strSQL;
objCmd.ExecuteNonQuery();
myGridView.EditIndex = -1;
BindData();
}
void myGridView_RowDataBound(Object s, GridViewRowEventArgs e)
{
Label lblRoomID = (Label)(e.Row.FindControl("lblRoomID"));
if (lblRoomID != null)
{
lblRoomID.Text = (string)DataBinder.Eval(e.Row.DataItem, "RoomID", "{0}");
}
Label lblRoom = (Label)(e.Row.FindControl("lblRoom"));
if (lblRoom != null)
{
lblRoom.Text = (string)DataBinder.Eval(e.Row.DataItem, "RoomName","{0}");
}
Label lblSize = (Label)(e.Row.FindControl("lblSize"));
if (lblSize != null)
{
lblSize.Text = (string)DataBinder.Eval(e.Row.DataItem, "Size", "{0}");
}
Label lblLocation = (Label)(e.Row.FindControl("lblLocation"));
if (lblLocation != null)
{
lblLocation.Text = (string)DataBinder.Eval(e.Row.DataItem, "Location", "{0}");
}
HyperLink hplDetail = (HyperLink)e.Row.FindControl("hplDetail");
if ((hplDetail != null))
{
hplDetail.NavigateUrl = "JavaScript:void(0);";
hplDetail.Attributes.Add("OnClick", "JavaScript:OpenPopup('popupDetailSchedule.aspx?RoomID=" + e.Row.DataItemIndex.ToString("RoomID").ToString() + "');");
}
HyperLink hplEdit = (HyperLink)(e.Row.FindControl("hplEdit"));
if (hplEdit != null)
{
hplEdit.NavigateUrl = "editRoomAdmin.aspx?RoomMeetID=" + (string)DataBinder.Eval(e.Row.DataItem, "RoomMeetID", "{0}");
}
}
void ShowPageCommand(Object s, GridViewPageEventArgs e)
{
myGridView.PageIndex = e.NewPageIndex;
BindData();
}
</script>
|
ประวัติการแก้ไข 2013-08-08 19:44:44 2013-08-08 19:46:55 2013-08-08 19:52:18
|
|
|
|
Date :
2013-08-08 19:41:21 |
By :
Kaen17 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ได้ละครับ
|
|
|
|
|
Date :
2013-08-08 22:56:29 |
By :
Kaen17 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Date :
2013-08-09 06:02:01 |
By :
mr.win |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Load balance : Server 00
|