Srivastava
Srivastava

Reputation: 3578

Method is malfunctioning

I have created a method that would bind all the tables with primary keys in a drop down like this

public void PrimaryKeyTable()
        {
 SqlConnection sConnection = new SqlConnection("Server=192.168.10.3;DataBase=GoalPlanNew;User Id=gp;Password=gp");

            //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";

            SqlCommand sCommand = new SqlCommand(selectPrimaryKeys, sConnection);

            try
            {
                DataSet dsListOfPrimaryKeys = new DataSet("INFORMATION_SCHEMA.TABLE_CONSTRAINTS");
                SqlDataAdapter dass = new SqlDataAdapter(selectPrimaryKeys, sConnection);
                dass.TableMappings.Add("Table", "INFORMATION_SCHEMA.TABLE_CONSTRAINTS");
                dass.Fill(dsListOfPrimaryKeys);

                DataViewManager dsvaa = dsListOfPrimaryKeys.DefaultViewManager;
                cmbResults.DataSource = dsListOfPrimaryKeys.Tables["INFORMATION_SCHEMA.TABLE_CONSTRAINTS"];
                cmbResults.DisplayMember = "NAME";
                cmbResults.ValueMember = ("");
            }
            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 (sConnection.State != ConnectionState.Closed)
                {
                    sConnection.Close();
                }
            }
        }

but instead of binding the tables with primary keys it is giving something like this in the dropdown

System.Data.DataRowView
System.Data.DataRowView...and so on

Can you plz point out where I went wrong?

Upvotes: 0

Views: 97

Answers (2)

Adam Houldsworth
Adam Houldsworth

Reputation: 64467

There is not a property on the DataRowView called "NAME". The DisplayMember property takes a string name of a property on the object you are binding and uses reflection to get the actual value. What it looks like you are trying to do is provide "NAME" as the name of a column returned and thus present in the DataRowView if you indexed it out.

That unfortunately won't work. An example of a working bound list:

http://msdn.microsoft.com/en-us/library/system.windows.forms.listcontrol.displaymember.aspx

I'm not sure if you can bind your DataRowView without wrapping it inside another class, which is not too onerous a solution in itself.

When the DisplayMember cannot be resolved, it defaults to running ToString() on the bound object: DataRowView.ToString() returns the type name because it doesn't override ToString() - this is what is happening in your case.

Update: apologies, it also works on column names for data tables. I cannot find any documentation on this at the moment, but if you set DisplayMember to "TABLE_NAME" it will also work. My explanation talks about class properties, this is the case with bound objects, but it is also the case that it can take indexer values though I don't know how this works under the hood.

Update 2: here is a nice overview, the CurrencyManager will handle the column name:

http://www.akadia.com/services/dotnet_databinding.html

Upvotes: 2

Shiv Kumar
Shiv Kumar

Reputation: 9799

Shouldn't the DisplayMember be "TABLE_NAME"

Upvotes: 4

Related Questions