|
|
|
Help me Plese !! ค่าที่เก็บใน Session ไม่ส่งค่าในหน้าถัดไป |
|
|
|
|
|
|
|
ผมมี file 4 file
form1-4.aspx
ทั้ง 4 form ข้อมูลเก็บลง datatable เเล้วค่อยเก็บใน sessiom เพื่อส่งค่าไปใหนform ที่ 4 คือหน้าสุดท้าย
..เพื่อทำการ บันทึกข้อมูลตอนสุดท้ายลง DB" ติดปัญหาตรงที่ พอส่งค่ามาเป็นค่าว่างทั้ง 3 form แรก
แล้วที่นี้ผมลองเทสดูว่าข้อมูลเเต่ละform บันทึกลง gridviews ของแต่หน้าหรือไม่ แต่ก็สามารถบันทึกลง gridviews ได้
------------------------------
พอมาหน้าสุดท้ายผมต้องเรียกใช้ session แต่ละ form ยังงัยบ้างอะ คับ ถึงจะบันทึกทุกฟอมรวมกันได้ทั้งหมด
ภาพแรก ผมใช้ชื่อ session ตัวเดียวกันลองเทสดูหน้าสุด้าย ดังภาพ
code ****************
form1.aspx.cs
public partial class Formsubmit1 : System.Web.UI.Page
{
DataTable myDataTable1 = new DataTable();
protected void Page_Load(object sender, EventArgs e)
{
if (!(Page.IsPostBack))
{
myDataTable1 = CreateColumnTable();
//Response.Write("New Teble");
Session["myData1"] = myDataTable1;
GridView1.DataBind();
}
else
myDataTable1 = (DataTable)Session["myData1"];
}
protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
{
DataRow dr = myDataTable1.NewRow();
dr["Name_th"] = TextBox1.Text.ToString();
dr["Name_eng"] = TextBox2.Text.ToString();
dr["Type"] = DropDownList1.SelectedValue.ToString();
dr["Type_edu"] = DropDownList2.SelectedValue.ToString();
dr["Activity"] = DropDownList3.SelectedValue.ToString();
dr["Major"] = DropDownList4.SelectedValue.ToString();
dr["Year"]= DropDownList5.SelectedValue.ToString();
myDataTable1.Rows.Add(dr);
GridView1.DataSource = myDataTable1;
GridView1.DataBind();
Session["myData1"] = myDataTable1;
//Response.Write(myDataTable.Rows.Count);
}
private DataTable CreateColumnTable()
{
DataTable dt;
if (Session["myData1"] != null)
{
dt = (DataTable)Session["myData1"];
}
else
dt = new DataTable();
DataColumn dc = new DataColumn();
dc.ColumnName = "Name_th";
dt.Columns.Add(dc);
dc = new DataColumn();
dc.ColumnName = "Name_eng";
dt.Columns.Add(dc);
dc = new DataColumn();
dc.ColumnName = "Type";
dt.Columns.Add(dc);
dc = new DataColumn();
dc.ColumnName = "Type_edu";
dt.Columns.Add(dc);
dc = new DataColumn();
dc.ColumnName = "Activity";
dt.Columns.Add(dc);
dc = new DataColumn();
dc.ColumnName = "Major";
dt.Columns.Add(dc);
dc = new DataColumn();
dc.ColumnName = "Year";
dt.Columns.Add(dc);
return dt;
}
}// End Class
********************
form2.aspx.cs
DataTable myDataTable2 = new DataTable();
protected void Page_Load(object sender, EventArgs e)
{
if (!(Page.IsPostBack))
{
myDataTable2 = CreateColumnTable();
Session["myData2"] = myDataTable2;
}
else
myDataTable2 = (DataTable)Session["myData2"];
}
protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
{
DataRow dr = myDataTable2.NewRow();
dr["publication_id"] = TextBox1.Text.ToString();
dr["journal_print"] = TextBox2.Text.ToString();
dr["research_publication"] = TextBox3.Text.ToString();
dr["importance"] = RadioButtonList1.SelectedValue.ToString();
dr["research_reference"] = TextBox4.Text.ToString();
dr["research_stap"] = TextBox5.Text.ToString();
myDataTable2.Rows.Add(dr);
GridView1.DataSource = myDataTable2;
GridView1.DataBind();
Session["myData2"] = myDataTable2;
}
private DataTable CreateColumnTable()
{
DataTable dt;
if (Session["myData2"] != null)
{
dt = (DataTable)Session["myData2"];
}
else
dt = new DataTable();
DataColumn dc = new DataColumn();
dc.ColumnName = "publication_id";
dt.Columns.Add(dc);
dc = new DataColumn();
dc.ColumnName = "journal_print";
dt.Columns.Add(dc);
dc = new DataColumn();
dc.ColumnName = "research_publication";
dt.Columns.Add(dc);
dc = new DataColumn();
dc.ColumnName = "importance";
dt.Columns.Add(dc);
dc = new DataColumn();
dc = new DataColumn();
dc.ColumnName = "research_reference";
dt.Columns.Add(dc);
dc = new DataColumn();
dc.ColumnName = "research_stap";
dt.Columns.Add(dc);
return dt;
}
}// End Class
****************************
form3.aspx.cs
DataTable myDataTable3 = new DataTable();
protected void Page_Load(object sender, EventArgs e)
{
if (!(Page.IsPostBack))
{
myDataTable3 = CreateColumnTable();
//Response.Write("New Teble");
Session["myData3"] = myDataTable3;
}
else
myDataTable3 = (DataTable)Session["myData3"];
GridView1.DataBind();
}
private DataTable CreateColumnTable()
{
DataTable dt;
if (Session["myData3"] != null)
{
dt = (DataTable)Session["myData3"];
}
else
dt = new DataTable();
DataColumn dc = new DataColumn();
dc.ColumnName = "Activity2";
dt.Columns.Add(dc);
dc = new DataColumn();
dc.ColumnName = "Start_Date";
dt.Columns.Add(dc);
dc = new DataColumn();
dc.ColumnName = "End_Date";
dt.Columns.Add(dc);
return dt;
}
protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
{
DataRow dr = myDataTable3.NewRow();
dr["Activity2"] = TextBox1.Text.ToString();
dr["Start_Date"] = TextBox2.Text.ToString();
dr["End_Date"] = TextBox3.Text.ToString();
myDataTable3.Rows.Add(dr);
GridView1.DataSource = myDataTable3;
GridView1.DataBind();
//Response.Write(myDataTable.Rows.Count);
}
}
*********************
form4.aspx.cs
public partial class Formsubmit4 : System.Web.UI.Page
{
string strConn = ConfigurationManager.ConnectionStrings["Research_DBConnectionString"].ToString();
DataTable myDataTable1;
DataTable myDataTable2;
DataTable myDataTable3;
DataTable myDataTable4 = new DataTable();
protected void Page_Load(object sender, EventArgs e)
{
if (!(Page.IsPostBack))
{
myDataTable4 = CreateColumnTable();
//Response.Write("New Teble");
Session["myData1"] = myDataTable1;
Session["myData2"] = myDataTable2;
Session["myData3"] = myDataTable3;
Session["myData4"] = myDataTable4;
}
else
myDataTable1 = (DataTable)Session["myData1"];
myDataTable2 = (DataTable)Session["myData2"];
myDataTable3 = (DataTable)Session["myData3"];
myDataTable4 = (DataTable)Session["myData4"];
GridView1.DataBind();
}
private DataTable CreateColumnTable()
{
DataTable dt;
if (Session["myData4"] != null)
{
dt = (DataTable)Session["myData4"];
}
else
dt = new DataTable();
DataColumn dc = new DataColumn();
dc.ColumnName = "Boss";
dt.Columns.Add(dc);
dc = new DataColumn();
dc.ColumnName = "Firstname";
dt.Columns.Add(dc);
dc = new DataColumn();
dc.ColumnName = "Lastname";
dt.Columns.Add(dc);
return dt;
}
protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
{
DataRow dr = myDataTable4.NewRow();
dr["Boss"] = DropDownList1.SelectedValue.ToString();
dr["Firstname"] = TextBox1.Text.ToString();
dr["Lastname"] = TextBox2.Text.ToString();
myDataTable4.Rows.Add(dr);
GridView1.DataSource = myDataTable4;
GridView1.DataBind();
Session["myData1"] = myDataTable1;
Session["myData2"] = myDataTable2;
Session["myData3"] = myDataTable3;
Session["myData4"] = myDataTable4;
//Response.Write(myDataTable.Rows.Count);
}
protected void ImageButton2_Click(object sender, ImageClickEventArgs e)
{
myDataTable1 = (DataTable)Session["myData1"];
myDataTable2 = (DataTable)Session["myData2"];
myDataTable3 = (DataTable)Session["myData3"];
SqlConnection conn = new SqlConnection(strConn);
conn.Open();
string ins = @"Insert in to tb_research(
research_id,
research_code,
name_t,
name_e,
start_date,
end_date,
budget_id,
type_id,
education_id,
activity_id,
@major_id,
status_id,
publication_id,
journal_print,
research_publication,
importance,
research_reference,
research_stap)
Values(@research_id,research_code,@name_t,@name_e,@start_date,@end_date,@budget_id,@type_id,@education_id,
@activity_id,@major_id,@status_id,@publication_id,@journal_print,@research_publication,
@importance,@research_reference,@research_stap)";
SqlCommand sqlcmd = new SqlCommand(ins, conn);
//string id_cars = CreateIDArticle();
int id_type = Convert.ToInt32(myDataTable1.Rows[0].ItemArray[3].ToString());
int id_education = Convert.ToInt32(myDataTable1.Rows[0].ItemArray[4].ToString());
int id_activity = Convert.ToInt32(myDataTable1.Rows[0].ItemArray[5].ToString());
int id_major = Convert.ToInt32(myDataTable1.Rows[0].ItemArray[6].ToString());
int id_budget = Convert.ToInt32(myDataTable1.Rows[0].ItemArray[7].ToString());
int id_pubblication = Convert.ToInt32(myDataTable2.Rows[0].ItemArray[0].ToString());
sqlcmd.Parameters.AddWithValue("@research_id",'1');
sqlcmd.Parameters.AddWithValue("@research_code",'1');
sqlcmd.Parameters.AddWithValue("@name_t",myDataTable1.Rows[0].ItemArray[0].ToString());
sqlcmd.Parameters.AddWithValue("@name_t",myDataTable1.Rows[0].ItemArray[1].ToString());
sqlcmd.Parameters.AddWithValue("@start_date",myDataTable3.Rows[0].ItemArray[1].ToString());
sqlcmd.Parameters.AddWithValue("@end_date",myDataTable3.Rows[0].ItemArray[2].ToString());
sqlcmd.Parameters.AddWithValue("@type_id",id_type);
sqlcmd.Parameters.AddWithValue("@education_id",id_education);
sqlcmd.Parameters.AddWithValue("@activity_id",id_activity);
sqlcmd.Parameters.AddWithValue("@major_id",id_major);
sqlcmd.Parameters.AddWithValue("@budget_id",id_budget);
sqlcmd.Parameters.AddWithValue("@status_id",'1');
sqlcmd.Parameters.AddWithValue("@publication_id",id_pubblication);
sqlcmd.Parameters.AddWithValue("@journal_print",myDataTable2.Rows[0].ItemArray[1].ToString());
sqlcmd.Parameters.AddWithValue("@research_publication",myDataTable2.Rows[0].ItemArray[2].ToString());
sqlcmd.Parameters.AddWithValue("@importance",myDataTable2.Rows[0].ItemArray[3].ToString());
sqlcmd.Parameters.AddWithValue("@research_reference",myDataTable2.Rows[0].ItemArray[4].ToString());
sqlcmd.Parameters.AddWithValue("@research_stap", myDataTable2.Rows[0].ItemArray[5].ToString());
//table1
//tb_activity
//tb_work
//sqlcmd.Parameters.AddWithValue("@ID", id_cars);
sqlcmd.ExecuteNonQuery();
conn.Close();
}
************************************************
'' งงมากมายอะคับทำมัยไม่ส่งค่ามาให้ ซะที หรือส่งมาเเต่ผมเรียกใช้ไม่ถูก ก็ไม่รู้ .... ผู้ไม่รู้ย่อมไม่ผิด ข้าน้อยขอคำชี้เเนะด้วยนพครับ ....ขอขอบพระคุณครับ
ปล. งงมาหลายวันเเล้วพี่น้อง ..
Tag : - - - -
|
|
|
|
|
|
Date :
2009-12-02 09:03:09 |
By :
Jormyuth |
View :
1745 |
Reply :
2 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ลองดูบทความ DataTable & Session นะครับ ปกติแล้ว Session ส่งได้ทุก Page และทุกส่วนของโปรแกรมครับ
Code (C#)
System.Data.OleDb.OleDbConnection objConn = new System.Data.OleDb.OleDbConnection();
System.Data.OleDb.OleDbCommand objCmd = new System.Data.OleDb.OleDbCommand();
System.Data.OleDb.OleDbDataAdapter dtAdapter = new System.Data.OleDb.OleDbDataAdapter();
DataSet ds = new DataSet();
DataTable dt;
String strConnString,strSQL;
strConnString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" +
Server.MapPath("database/mydatabase.mdb")+";Jet OLEDB:Database Password=;";
strSQL = "SELECT * FROM customer";
objConn.ConnectionString = strConnString;
objCmd.Connection = objConn;
objCmd.CommandText = strSQL;
objCmd.CommandType = CommandType.Text;
dtAdapter.SelectCommand = objCmd;
dtAdapter.Fill(ds);
dt = ds.Tables[0];
Session["mySession"] = dt;
dtAdapter = null;
objConn.Close();
objConn = null;
//*** BindData to Control ***//
DataTable dt2;
dt2 = Session["mySession"];
if(Session["mySession"] != null)
{
this.myDataGrid.DataSource = dt2;
this.myDataGrid.DataBind();
}
(C#) ASP.NET - Session (DataSet & DataTable)
ขอวิธีการเอาข้อมูลจาก gridview ลงไปอีกตารางหนึ่งหน่อยครับ
|
|
|
|
|
Date :
2009-12-02 17:24:11 |
By :
webmaster |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
อยากเก่งแบบพี่วินจังุว้ย
|
|
|
|
|
Date :
2009-12-02 17:26:40 |
By :
zero1150kfc |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Load balance : Server 04
|