2013-03-08 15 views
7

Quando provo a databiare dropdownlist, ho ottenuto questo: system.data.datarowview
cosa mi sbaglio?Databind a dropdownlist

string strQuery = "Select Item FROM Calendar Where UserD="Test"; 
SqlConnection myConn; 
SqlDataAdapter sqlDa = new SqlDataAdapter(strQuery,myConn); 
DataTable sqlTa = new DataTable("Test"); 
da.Fill(sqlTa); 
ddlList.DataSource = sqlTa; 
ddlList.DataBind(); 
+0

dove si effettua la rilegatura del menu a discesa? È questo in page_load? –

risposta

14
string strQuery = "Select Item FROM Calendar Where UserD='Test'"; 

Nota è necessario utilizzare singoli citazioni attorno alla stringa, come nel codice non hai finito la stringa iniziale che mai, in modo che il resto del codice è diventato solo una parte di strQuery.

Inoltre se si restituisce più di un campo in futuro, quando si associa un elenco a discesa è necessario specificare quale campo del database rappresenta il valore e quale è il testo visualizzato.

ddlList.DataSource = sqlTa; 
ddlList.DataValueField = "ValueFieldFromDatabaseResults"; 
ddlList.DataTextField = "ShownTextFieldFromDatabaseResults"; 
ddlList.DataBind(); 
2

È necessario indicare quali campi utilizzare come valore e testo.

ddlList.DataSource = sqlTa; 
ddlList.DataValueField = "ValueField"; 
ddlList.DataTextField = "TextField"; 
ddlList.DataBind(); 

E la vostra istruzione select manca un" Dovrebbe essere:.

"Select Item FROM Calendar Where UserD='Test'" 

un esempio è:

Come Ryan ha sottolineato, se si sta tirando indietro di un campo, allora si può solo fare:

 DataTable dtTable = new DataTable(); 

     try 
     { 
      using (SqlConnection sqlConnection = new SqlConnection("Your connection")) 
      { 
       using (SqlCommand sqlCommand = new SqlCommand("Select Item FROM Calendar Where UserD='Test'", sqlConnection)) 
       { 
        sqlConnection.Open(); 

        using (SqlDataReader sqlDataReader = sqlCommand.ExecuteReader()) 
        { 
         dtTable.Load(sqlDataReader); 
         sqlDataReader.Close(); 
        } 
       } 
      } 
     } 
     catch (Exception error) 
     { 
      throw error; 
     } 

     ddlList.DataSource = dtTable; 
     ddlList.DataBind(); 

Ma se si dispone di più di un campo, allora si può fare questo:

 DataTable dtTable = new DataTable(); 

     try 
     { 
      using (SqlConnection sqlConnection = new SqlConnection("Your connection")) 
      { 
       using (SqlCommand sqlCommand = new SqlCommand("Select Item, id FROM Calendar Where UserD='Test'", sqlConnection)) 
       { 
        sqlConnection.Open(); 

        using (SqlDataReader sqlDataReader = sqlCommand.ExecuteReader()) 
        { 
         dtTable.Load(sqlDataReader); 
         sqlDataReader.Close(); 
        } 
       } 
      } 
     } 
     catch (Exception error) 
     { 
      throw error; 
     } 

     ddlList.DataSource = dtTable; 
     ddlList.DataValueField = "id"; 
     ddlList.DataTextField = "item"; 
     ddlList.DataBind(); 
+0

Puoi farmi un esempio? –

+0

@Stephen poiché sta solo riportando un campo dal database che non è necessario specificare il valore o il campo di testo. –

+0

Voto positivo per i tuoi aggiornamenti! –

0

Prova questo ..

ddlList.DataSource = sqlTa;     
ddlList.DataTextField = "class"; 
ddlList.DataBind(); 

aggiungendo ddList.Value="somefield" è facoltativo

0

aggiungere in questo modo la linea

ddlList.DataSource = sqlTa; 
ddlList.DataValueField = "ValueFieldFromDatabaseResults"; 
ddlList.DataTextField = "ShownTextFieldFromDatabaseResults"; 
ddlList.DataBind(); 

poi u perdere stringa di connessione per

SqlConnection myConn="must add your connection string code here " 

Non hai aperto stringa di connessione in modo

aggiungere myconn.open()

per

SqlConnection myConn="must add your connection string code here " 
`myconn.open()` 
SqlDataAdapter sqlDa = new SqlDataAdapter(strQuery,myConn) 
Problemi correlati