2012-09-01 58 views
1

我想將相同的數據讀取器對象綁定到2個控件.one是gridview,第二個是formview。GridView獲取綁定,但它的formview沒有。即使我綁定兩個對象後關閉。 任何人都可以讓我知道,如果有可能或不。如果是的話,那麼如何?我可以將相同的數據讀取器對象綁定到2個控件嗎?

這是我的代碼: -

SqlConnection con = new SqlConnection(getconnectionstring()); 
    SqlCommand cmd = new SqlCommand(); 
    SqlCommand cmd1 = new SqlCommand(); 

    //cmd.CommandText = "SELECT firstname,lastname FROM crudtable"; 
    cmd.CommandText = "SELECT firstname,lastname FROM crudtable"; 
    cmd1.CommandText = "SELECT firstname FROM crudtable"; 

    cmd.Connection = con; 
    cmd1.Connection = con; 

    con.Open(); 

    SqlDataReader reader ; 
    SqlDataReader reader1; 

    reader = cmd.ExecuteReader(); 
    GridView1.DataSource = reader; 
    GridView1.DataBind(); 

    FormView1.DataSource = reader; 
    FormView1.DataBind(); 
    reader.Close(); 
    reader1 = cmd1.ExecuteReader(); 
    ddl.DataSource = reader1; 
    ddl.DataTextField = "firstname"; 
    ddl.DataBind(); 

在此先感謝。

+0

請參閱更新code.The讀者對象必須將它綁定到兩個控件後關閉一次。 – user1627138

+0

當然可以,但是現在有效嗎? –

+0

nope.it沒有。 – user1627138

回答

4

SqlDataReader是一個只有前瞻性的讀者。

提供了一種從SQL 服務器數據庫中讀取只向前流的流的方法。

一旦讀取了數據,就不能再回頭閱讀它。 這就是爲什麼你不能再次調用ExecuteReader就不能使用同一個閱讀器作爲另一個控件的數據源。

如果您獲得的行數很少,您可以將數據提取到DataSet並將其綁定到兩者。

SqlConnection conn = new SqlConnection(ConnectionString); 
SqlDataAdapter da = new SqlDataAdapter(); 
SqlCommand cmd = conn.CreateCommand(); 
cmd.CommandText = "SELECT firstname,lastname FROM crudtable"; 
da.SelectCommand = cmd; 
DataSet ds = new DataSet(); 
conn.Open(); 
da.Fill(ds); 
conn.Close(); 

GridView1.DataSource = ds; 
GridView1.DataBind(); 

FormView1.DataSource = ds; 
FormView1.DataBind(); 
ddl.DataSource = ds; 
ddl.DataTextField = "firstname"; 
ddl.DataBind(); 

一旦你在你的DataSet的數據,你可以決定哪些列Bind和表演。

+0

它沒有綁定formview和dd1。 – user1627138

3

對於在這種情況下,結合最精簡版本,我會修改,像這樣的代碼:

DataTable results = new DataTable(); 
using (SqlConnection connection = new SqlConnection(getconnectionstring())) 
{ 
    connection.Open(); 
    using (SqlCommand command = new SqlCommand("SELECT firstname,lastname FROM crudtable",connection)) 
    { 
     results.Load(command.ExecuteReader()); 
    } 
} 

GridView1.DataSource = results; 
GridView1.DataBind(); 

FormView1.DataSource = results; 
FormView1.DataBind(); 

ddl.DataSource = results; 
ddl.DataTextField = "firstname"; 
ddl.DataBind(); 
0

最後我花了一個新的讀者對象綁定FormView控件,但仍沒有work.Am我某處出錯或缺少某物。 獲取不異常沒有錯誤。 這是更新的代碼。

SqlConnection con = new SqlConnection(getconnectionstring()); 
    SqlCommand cmd = new SqlCommand(); 
    SqlCommand cmd1 = new SqlCommand(); 
    SqlCommand cmd2 = new SqlCommand(); 

    //cmd.CommandText = "SELECT firstname,lastname FROM crudtable"; 
    cmd.CommandText = "SELECT firstname,lastname FROM crudtable"; 
    cmd2.CommandText = "SELECT firstname,lastname FROM crudtable"; 
    cmd1.CommandText = "SELECT firstname FROM crudtable"; 

    cmd.Connection = con; 
    cmd1.Connection = con; 
    cmd2.Connection=con; 
    con.Open(); 

    SqlDataReader reader ; 
    SqlDataReader reader1; 
    SqlDataReader reader2; 
    reader = cmd.ExecuteReader(); 


    GridView1.DataSource = reader; 
    GridView1.DataBind(); 
    reader.Close(); 
    //reader.Close(); 
    reader2 = cmd2.ExecuteReader(); 
    FormView1.DataSource = reader2; 
    FormView1.DataBind(); 
    reader2.Close(); 
    reader1 = cmd1.ExecuteReader(); 
    ddl.DataSource = reader1; 
    ddl.DataTextField = "firstname"; 
    ddl.DataBind(); 
    reader1.Close(); 
1

如果2個控件不結合到1個數據源再取該數據源,並將其綁定到表,則該表 和它的數據複製到另一個表和seperatley約束力。

DataTable.clone()將獲取結構。 DatTable.Copy()將獲取模式和記錄

Clone reference

Copy Reference

相關問題