2010-11-25 6 views
0

J'ai une fonction pour remplir la liste de toutes les tables avec des clés primaires dans un db sélectionné dans un menu déroulant:texte sélectionné dans zone de liste déroulante ne retourne pas une valeur

public void PrimaryKeyTable() 
{ 

    //An instance of the connection string is created to manage the contents of the connection string. 

    var sqlConnection = new SqlConnectionStringBuilder(); 
    sqlConnection.DataSource = "192.168.10.3"; 
    sqlConnection.UserID = "gp"; 
    sqlConnection.Password = "gp"; 
    sqlConnection.InitialCatalog = Convert.ToString(cmbDatabases.SelectedValue); 
    string connectionString = sqlConnection.ConnectionString; 

    SqlConnection sConnection = new SqlConnection(connectionString); 

    //To Open the connection. 
    sConnection.Open(); 

    string selectPrimaryKeys = @"SELECT 
             TABLE_NAME 
            FROM 
             INFORMATION_SCHEMA.TABLE_CONSTRAINTS 
            WHERE 
             CONSTRAINT_TYPE = 'PRIMARY KEY' 
           ORDER BY 
             TABLE_NAME"; 

    //Create the command object 
    SqlCommand sCommand = new SqlCommand(selectPrimaryKeys, sConnection); 

    try 
     { 
     //Create the dataset 
     DataSet dsListOfPrimaryKeys = new DataSet("INFORMATION_SCHEMA.TABLE_CONSTRAINTS"); 

     //Create the dataadapter object 
     SqlDataAdapter sDataAdapter = new SqlDataAdapter(selectPrimaryKeys, sConnection); 

     //Provides the master mapping between the sourcr table and system.data.datatable 
     sDataAdapter.TableMappings.Add("Table", "INFORMATION_SCHEMA.TABLE_CONSTRAINTS"); 

     //Fill the dataset 
     sDataAdapter.Fill(dsListOfPrimaryKeys); 

     //Bind the result combobox with primary key tables 
     DataViewManager dvmListOfPrimaryKeys = dsListOfPrimaryKeys.DefaultViewManager; 
     cmbResults.DataSource = dsListOfPrimaryKeys.Tables["INFORMATION_SCHEMA.TABLE_CONSTRAINTS"]; 
     cmbResults.DisplayMember = "TABLE_NAME"; 
     cmbResults.ValueMember = "TABLE_NAME"; 
     } 
    catch(Exception ex) 
     { 
     //All the exceptions are handled and written in the EventLog. 
     EventLog log = new EventLog("Application"); 
     log.Source = "MFDBAnalyser"; 
     log.WriteEntry(ex.Message); 
     } 
    finally 
     { 
     //If connection is not closed then close the connection 
     if(sConnection.State != ConnectionState.Closed) 
      { 
      sConnection.Close(); 
      } 
     } 
} 

Je souhaite appeler cette fonction une cliquage comme ceci:

private void btnStartAnalysis_Click(object sender, EventArgs e) 
    { 
     //This is the function call for the primary key checking in DB 
     PrimaryKeyTable(); 
    } 

Son fonctionne bien, mais quand je veux l'appeler sur le bouton, cliquez lorsqu'un texte particulier est sélectionné dans le menu déroulant comme celui-ci:

private void btnStartAnalysis_Click(object sender, EventArgs e) 
    { 
     //This is the function call for the primary key checking in DB 
     if(cmbOperations.SelectedText == "PrimaryKeyTables") 
      { 
      PrimaryKeyTable(); 
      } 
    } 

Ensuite, il ne donne aucun résultat ...

Quelqu'un peut-il dire où je vais mal?

Répondre

3

Je pense que vous devez remplacer

if(cmbOperations.SelectedText == "PrimaryKeyTables") 

avec

if((string)cmbOperations.SelectedValue == "PrimaryKeyTables") 
+0

+1 Ceci est correct. La propriété 'SelectedText' représente le texte sélectionné dans la partie modifiable de la liste déroulante, comme dans une zone de texte. Si 'DropDownStyle' est défini sur' DropDownList', il n'y aura * * aucun texte à sélectionner. 'SelectedValue' renverra la chaîne que vous voulez. –

+0

IL FONCTION FINE DUDE – Srivastava

4

Utilisez SelectedItem au lieu de SelectedText. Et puis utilisez ToString() pour obtenir la valeur de chaîne.

Questions connexes