简体   繁体   中英

System.Data.DataRowView?

I have 7 comboboxes in my project i use comboboxs to selected data from database. The first combobox is working but the others show this:

System.Data.DataRowView

what can i do to solve this error this is the code of comboboxes ?

 private void client_Load(object sender, EventArgs e)
    {
        SqlConnection con = new SqlConnection();
        con.ConnectionString = "Data Source=.;Initial Catalog=SCP_DB;Integrated Security=True";
        con.Open();
        string query = "select  Operation_Type from Operations_Types; select Payment_Types from Payment_Type; select Property_Types from Property_Type; select City from Flats; select Section from Flats; select Block from Flats; select Street from Flats";
        SqlDataAdapter da = new SqlDataAdapter(query, con);
        da.TableMappings.Add("Table", "Operations_Types");
        da.TableMappings.Add("Table1", "Payment_Type");
        da.TableMappings.Add("Table2", "Property_Type");
        da.TableMappings.Add("Table3", "Flats");
        da.TableMappings.Add("Table4", "Flats");
        da.TableMappings.Add("Table5", "Flats");
        da.TableMappings.Add("Table6", "Flats");
        DataSet ds = new DataSet();

        da.Fill(ds, "Operations_Types");
        comboOpType.DisplayMember = "Operation_Type";
        comboOpType.ValueMember = "Operation_Type";
        comboOpType.DataSource = ds.Tables["Operations_Types"];

        da.Fill(ds, "Payment_Type");
        comboPayType.DisplayMember = "Payment_Types";
        comboPayType.ValueMember = "Payment_Types";
        comboPayType.DataSource = ds.Tables["Payment_Type"];

        da.Fill(ds, "Property_Type");
        comboProType.DisplayMember = "Property_Types";
        comboProType.ValueMember = "Property_Types";
        comboProType.DataSource = ds.Tables["Property_Type"];

        da.Fill(ds, "Flats");
        comboCity.DisplayMember = "City";
        comboCity.ValueMember = "City";
        comboCity.DataSource = ds.Tables["Flats"];

        da.Fill(ds, "Flats");
        comboSection.DisplayMember = "Section";
        comboSection.ValueMember = "Section";
        comboSection.DataSource = ds.Tables["Flats"];

        da.Fill(ds, "Flats");
        comboBlock.DisplayMember = "Block";
        comboBlock.ValueMember = "Block";
        comboBlock.DataSource = ds.Tables["Flats"];

        da.Fill(ds, "Flats");
        comboStreet.DisplayMember = "Street";
        comboStreet.ValueMember = "Street";
        comboStreet.DataSource = ds.Tables["Flats"];

        SqlCommand cmd = new SqlCommand(query, con);
        cmd.ExecuteNonQuery();
        con.Close();


    }

You should fill the DataSet only once, the SqlDataAdapter handles the case that it contains multiple result sets. You can give them names afterwards:

SqlDataAdapter da = new SqlDataAdapter(query, con);
DataSet ds = new DataSet();
da.Fill(ds);
ds.Tables[0].TableName = "Operations_Types";
ds.Tables[1].TableName = "Property_Type";
// ...
comboOpType.DisplayMember = "Operation_Type";
comboOpType.ValueMember = "Operation_Type";
comboOpType.DataSource = ds.Tables["Operations_Types"];
comboPayType.DisplayMember = "Payment_Types";
comboPayType.ValueMember = "Payment_Types";
comboPayType.DataSource = ds.Tables["Payment_Type"];
// ...

Otherwise Fill will append all records and tables on each consecutive call.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM