|
|
|
จะใช้ Eval("ProductId") ใน ItemCommand เพื่อที่จะ insert ค่าลง database ยังไงครับ |
|
|
|
|
|
|
|
Code (C#)
protected void dlProduct_ItemCommand(object source, DataListCommandEventArgs e)
{
TextBox txt = new TextBox();
txt = (TextBox)e.Item.FindControl("txtqty");
Label num = new Label();
num = (Label)e.Item.FindControl("ProductPriceLabel");
if (txt != null)
{
string strconn = System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionString"].ToString();
SqlConnection conn = new SqlConnection(strconn);
conn.Open();
string commandtext = @"INSERT INTO cart(Username,ProductId,ProductName,OrderDate,Qty,TotalPrice) VALUES (@Username,@ProductId,@ProductName,@OrderDate,@Qty,@TotalPrice)";
SqlCommand com = new SqlCommand(commandtext, conn);
com.Parameters.Add("@Username", SqlDbType.NVarChar).Value = Session["strUsername"];
com.Parameters.Add("@ProductId", SqlDbType.NVarChar).Value = Eval("ProductId");
com.Parameters.Add("@ProductName", SqlDbType.NVarChar).Value = Eval("ProductName");
com.Parameters.Add("@OrderDate", SqlDbType.DateTime).Value = DateTime.Now;
com.Parameters.Add("@Qty", SqlDbType.Image).Value = txt.Text.Trim();
com.Parameters.Add("@TotalPrice", SqlDbType.Image).Value = ;
if (conn.State == ConnectionState.Open)
{ conn.Close(); }
conn.Open();
com.ExecuteNonQuery();
conn.Close();
Response.Redirect("index.aspx");
}
}
Tag : ASP
|
ประวัติการแก้ไข 2012-08-31 11:33:14
|
|
|
|
|
Date :
2012-08-31 11:32:09 |
By :
thewinner55 |
View :
1136 |
Reply :
2 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ประมาณนี้ครับ
Code (C#)
void myDataList_UpdateCommand(Object sender, DataListCommandEventArgs e)
{
//*** CategoryID ***//
Label lblCateID = (Label)(e.Item.FindControl("lblCateID"));
//*** Category ***//
TextBox txtCategory = (TextBox)(e.Item.FindControl("txtCategory"));
strSQL = "UPDATE category SET CategoryName = '" + txtCategory.Text + "' " +
" WHERE CategoryID = " + lblCateID.Text + " ";
objCmd = new OleDbCommand(strSQL, objConn);
objCmd.ExecuteNonQuery();
//*** If Select File Upload ***//
HtmlInputFile filPicture = (HtmlInputFile)(e.Item.FindControl("filPicture"));
String strFileName;
if(filPicture.PostedFile.FileName != "")
{
strFileName = System.IO.Path.GetFileName(filPicture.Value);
filPicture.PostedFile.SaveAs(Server.MapPath("images/" + strFileName));
strSQL = "UPDATE category SET Picture = 'images/" + strFileName + "' " +
" WHERE CategoryID = " + lblCateID.Text + " ";
objCmd = new OleDbCommand(strSQL, objConn);
objCmd.ExecuteNonQuery();
}
myDataList.EditItemIndex = -1;
BindData();
}
Go to : (C#) ASP.NET DataList Control - ItemCommand
|
|
|
|
|
Date :
2012-08-31 13:22:17 |
By :
mr.win |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ถ้า ผม ทำแบบนี้ จะได้ไหม ครับ แต่ มัน error อะครับ
Code (C#)
protected void dlProduct_ItemCommand(object source, DataListCommandEventArgs e)
{
TextBox Qty = new TextBox();
Qty = (TextBox)e.Item.FindControl("txtqty");
Label ProductIdLabel = new Label();
ProductIdLabel = (Label)e.Item.FindControl("ProductIdLabel");
Label ProductName = new Label();
ProductName = (Label)e.Item.FindControl("ProductName");
Label TotalPrice = new Label();
TotalPrice = (Label)e.Item.FindControl("ProductPriceLabel");
if (Qty != null) // มี Textbox ชื่อนี้หรือไม่?
{
// ทำงานต่อไปตามต้องการ
// ค่าของ TextBox ใช้ตามปกติ txt.Text
string strconn = System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionString"].ToString();
SqlConnection conn = new SqlConnection(strconn);
conn.Open();
string commandtext = @"INSERT INTO Table1(Username,ProductId,ProductName,OrderDate,Qty,TotalPrice) VALUES (@Username,@ProductId,@ProductName,@OrderDate,@Qty,@TotalPrice)";
SqlCommand com = new SqlCommand(commandtext, conn);
com.Parameters.Add("@Username", SqlDbType.NVarChar).Value = Session["strUsername"];
com.Parameters.Add("@ProductId", SqlDbType.Int).Value = ProductIdLabel;
com.Parameters.Add("@ProductName", SqlDbType.NVarChar).Value = ProductName;
com.Parameters.Add("@OrderDate", SqlDbType.DateTime).Value = DateTime.Now;
com.Parameters.Add("@Qty", SqlDbType.Int).Value = Qty.Text.Trim();
com.Parameters.Add("@TotalPrice", SqlDbType.Int).Value = TotalPrice;
if (conn.State == ConnectionState.Open)
{ conn.Close(); }
conn.Open();
com.ExecuteNonQuery();
conn.Close();
Response.Redirect("index.aspx");
}
}
|
|
|
|
|
Date :
2012-08-31 13:41:59 |
By :
thewinner55 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Load balance : Server 05
|