2013-06-02 19 views
7

Sto utilizzando this video per provare e popolare i risultati da un DataGridView e sto ricevendo l'errore precedente. Il codice riportato di seguito riguarda questo errore: trasferisco i valori in una stored procedure, quindi l'ultimo SELECT restituisce i valori nella tabella in DataGridView.Errore C#: "Riempimento: la proprietà SelectCommand.Connection non è stata inizializzata."

 SqlConnection con = new SqlConnection(); 
     con.ConnectionString = "integrated security=SSPI;data source=SERV;" + "persist security info=False;initial catalog=DB"; 

     con.Open(); 
     SqlCommand select = new SqlCommand("SELECT * FROM Table"); 
     SqlCommand enter = new SqlCommand("sp_Proc", con); 

     // Stored Procedure 
     enter.CommandType = CommandType.StoredProcedure; 
     enter.Parameters.Add(new SqlParameter("@vvalue", SqlDbType.VarChar)).Value = Convert.ToString(txt1.Text); 
     enter.Parameters.Add(new SqlParameter("@dvalue", SqlDbType.Decimal)).Value = Convert.ToDecimal(txt2.Text); 
     enter.ExecuteNonQuery(); 

     // DataGrid returns the SELECT 

     SqlDataAdapter sadapt = new SqlDataAdapter(select); 
     sadapt.SelectCommand = select; 
     DataTable dtab = new DataTable(); 
     sadapt.Fill(dtab); // generates the error 
     BindingSource b = new BindingSource(); 
     b.DataSource = dtab; 
     dGrid.DataSource = b; 
     sadapt.Update(dtab); 

     con.Close(); 
+0

Qual è l'errore che ti dà? –

risposta

13

Lei non ha superato connection oggetto sul command. Prova questo oggetto di connessione, invece,

SqlCommand select = new SqlCommand("SELECT * FROM Table", con); 
+0

Grazie; il tuo insieme di occhi ha colto il mio errore. – user123

+0

prego: ')' –

+0

@JohnWoo: Mi ha aiutato anche .. !! +1 :) – BNN

1

si passa al tuo entrare comando, ma non ha ancora passare l'oggetto connessione al selezionare comando

SqlCommand select = new SqlCommand("SELECT * FROM Table"); 
    SqlCommand enter = new SqlCommand("sp_Proc", con); 

Utilizzare questa

SqlCommand select = new SqlCommand("SELECT * FROM Table",con); 
Problemi correlati