|
|
|
ASP.Net หน้า Update และ Edit ขึ้น Error ตรง myRepeater.DataBind(); คือไรอ่ะครับ |
|
|
|
|
|
|
|
Code (C#)
<%@ Import Namespace="System.Data"%>
<%@ Import Namespace="System.Data.SqlClient"%>
<%@ Page Language="C#" Debug="true" %>
<script runat="server">
SqlConnection objConn = new SqlConnection();
SqlCommand objCmd = new SqlCommand();
SqlDataReader dtReader;
String strSQL;
void Page_Load(object sender,EventArgs e)
{
String strConnString;
strConnString = "Data Source=NEWPROGRAMMER;Initial Catalog=customer;Integrated Security=True";
objConn = new SqlConnection(strConnString);
objConn.Open();
if(!Page.IsPostBack)
{
BindData();
}
}
void BindData()
{
String strSQL;
strSQL = "SELECT * FROM custest";
SqlDataReader dtReader;
objCmd = new SqlCommand(strSQL, objConn);
dtReader = objCmd.ExecuteReader();
//*** BindData to Repeater ***//
myRepeater.DataSource = dtReader;
myRepeater.DataBind(); <======================== Error ขึ้นตรงนี้คืออะไรอะครับ รบกวนหน่อย
dtReader.Close();
dtReader = null;
}
void Page_UnLoad()
{
objConn.Close();
objConn = null;
}
void myRepeater_ItemCommand(Object source, RepeaterCommandEventArgs e)
{
if(e.CommandName == "Delete")
{
//*** CustomerID ***//
Label lblCustomerID = (Label)(e.Item.FindControl("lblCustomerID"));
strSQL = "DELETE FROM custest WHERE CustomerID = '"+ lblCustomerID.Text +"' ";
objCmd = new SqlCommand(strSQL, objConn);
objCmd.ExecuteNonQuery();
BindData();
}
}
protected void myRepeater_ItemDataBound(Object sender, RepeaterItemEventArgs e)
{
//*** CustomerID ***//
Label lblCustomerID = (Label)(e.Item.FindControl("lblCustomerID"));
if (lblCustomerID != null)
{
lblCustomerID.Text = (string)DataBinder.Eval(e.Item.DataItem, "CustomerID");
}
//*** Email ***//
Label lblName = (Label)(e.Item.FindControl("lblName"));
if (lblName != null)
{
lblName.Text = (string)DataBinder.Eval(e.Item.DataItem, "Name");
}
//*** Name ***//
Label lblEmail = (Label)(e.Item.FindControl("lblEmail"));
if (lblEmail != null)
{
lblEmail.Text = (string)DataBinder.Eval(e.Item.DataItem, "Email");
}
//*** CountryCode ***//
Label lblCountryCode = (Label)(e.Item.FindControl("lblCountryCode"));
if (lblCountryCode != null)
{
lblCountryCode.Text = (string)DataBinder.Eval(e.Item.DataItem, "CountryCode");
}
//*** Budget ***//
Label lblBudget = (Label)(e.Item.FindControl("lblBudget"));
if (lblBudget != null)
{
lblBudget.Text = DataBinder.Eval(e.Item.DataItem, "Budget").ToString();
}
//*** Used ***//
Label lblUsed = (Label)(e.Item.FindControl("lblUsed"));
if (lblUsed != null)
{
lblUsed.Text = DataBinder.Eval(e.Item.DataItem, "Used").ToString();
}
//*** Delete ***//
LinkButton lnkDelete = (LinkButton)(e.Item.FindControl("lnkDelete"));
if (lnkDelete != null)
{
lnkDelete.Attributes.Add("OnClick", "return confirm('Delete Record?');");
}
}
</script>
แล้วถ้าเขียน โค้ด ในหน้า .aspx.cs ไม่ได้เขียนแบบ inline ใน .aspx โดยใช้ <script runat="server"> แนะนำหน่อยได้ไหมครับ ว่าไปเริ่มหน้า .aspx.cs ยังไง ย้ายโค้ดไปแล้วไปติดตรง 4 บรรทัดนี้อ่ะคับ ต้องเอาไว้ตรงไหนของ .aspx.cs และประกาศไร เพิ่มรึป่าว
Code (C#)
SqlConnection objConn = new SqlConnection();
SqlCommand objCmd = new SqlCommand();
SqlDataReader dtReader;
String strSQL;
Tag : .NET, Ms SQL Server 2008, C#, VS 2010 (.NET 4.x)
|
|
|
|
|
|
Date :
2013-04-10 19:56:45 |
By :
offonepoint |
View :
874 |
Reply :
4 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Load balance : Server 05
|