System.Data.DataRowView?

时间:2016-02-10 13:16:25

标签: c# sql ado.net

我的项目中有7 comboboxes我使用comboboxs从数据库中选择数据。第一个combobox正在运作,但其他人表明了这一点:

System.Data.DataRowView

如何解决此错误,这是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();


    }

1 个答案:

答案 0 :(得分:2)

您应该只填充DataSet一次,SqlDataAdapter处理包含多个结果集的情况。你可以在之后给他们起名字:

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

否则Fill将在每次连续呼叫时附加所有记录和表格。