2017-02-06 5 views
1

iがセッションに結果を代入したいセッションになり割り当てクエリは

C#コード

 protected void BindData() 
{ 
    SqlConnection con = new SqlConnection(@"Data Source=DESKTOP-677TN4G\SQLEXPRESS;Initial Catalog=homework;Persist Security Info=True;User ID=sa;Password=123456"); 

    DataSet ds = new DataSet(); 
    DataTable FromTable = new DataTable(); 
    con.Open(); 
    string cmdstr = "Select CourseName from Staff where [email protected]"; 
    SqlCommand cmd = new SqlCommand(cmdstr, con); 
    cmd.Parameters.AddWithValue("@idd", Session["id"].ToString()); 
    SqlDataAdapter adp = new SqlDataAdapter(cmd); 
    adp.Fill(ds); 
    DataList1.DataSource = ds.Tables[0]; 
    DataList1.DataBind(); 

} 

ので、私は

 Session["id"] = cmdstr.Text; 

をしようとそれを行うことができますが、それはうまくいきませんか

私はこれが好きだが仕事はしない。

私はこの

 protected void BindData() 
{ 
    SqlConnection con = new SqlConnection(@"Data Source=DESKTOP-677TN4G\SQLEXPRESS;Initial Catalog=homework;Persist Security Info=True;User ID=sa;Password=123456"); 

    DataSet ds = new DataSet(); 
    DataTable FromTable = new DataTable(); 
    con.Open(); 
    string cmdstr = "Select CourseName from Staff where [email protected]"; 

    SqlCommand cmd = new SqlCommand(cmdstr, con); 
    cmd.Parameters.AddWithValue("@idd", Session["id"].ToString()); 
    SqlDataAdapter adp = new SqlDataAdapter(cmd); 
    adp.Fill(ds); 
    DataList1.DataSource = ds.Tables[0]; 
    Session.Add("Staff", ds.Tables[0]); 
    DataList1.DataBind(); 
    Label1.Visible = true; 
    Label1.Text = "Course Name is : " + Session["Staff"].ToString(); 

} 

のようにそれを行うと出力がある

コース名は次のとおりです。表

ない結果があなたの場合は、選択した値

+0

があなたの要件に関する詳細情報を追加してもらえ、 'CMDSTRは'それは私が見たようText' –

+0

'というプロパティが含まれていない文字列変数は' cmdstr'は、文字列ではなく、テキストボックスコントロールであります。あなたの 'Session [" id "]'値はどこから来ますか? –

+0

テーブルからの結果からセッションが来て、それをラベルに表示します。 – Sulaiman

答えて

0

DataTableこのDataTableをセッションにも格納できます。 staffDataあなたがデータセットとして使用されているdsを使用していて、グリッドをバインドするds.Tables[0]にアクセスしているあなたのケースでは、データベースから移入されたDataTable、

Session.Add("Staff", staffData); // Adding datatable to the session 

とします。この場合は、Session.Add("Staff", ds.Tables[0]);のようなものを使用できます。

後でこのセッションからこのDataTableを取得する方法についてもう一つの質問が出るかもしれません。ここではその回答も追加しています。

DataList1.DataSource = (DataTable)Session["Staff"]; 
+0

私はそれを変更し、いくつかの問題はコードを確認してください – Sulaiman