从对象类型System.Data.DataRowView到已知的托管提供程序本机类型不存在映射

时间:2013-04-23 04:17:14

标签: c# winforms

我在c#中有一个窗体,有3个组合框。现在的问题是,当我通过插入的SQL查询时,它给了我这个错误。请帮我解决错误。这是代码。

 namespace login
{
    public partial class samplerequisition : Form
    {
        SqlConnection con = new SqlConnection("Data Source=TH07L019;Initial Catalog=procurement1;Integrated Security=True");
        SqlCommand cmd = new SqlCommand();
        SqlDataReader rdr;
        DataSet dsreqname = new DataSet();
        DataSet dsprepname = new DataSet();
        DataSet dsauthorizedname = new DataSet();

        public samplerequisition()
        {
            InitializeComponent();
        }

        bool IsAllValid()
        {
            if (String.IsNullOrEmpty(txtreqno.Text))
            {
                return false;
            }

            else if (String.IsNullOrEmpty(txtexpectedate.Text))
            {
                return false;
            }
            else if (String.IsNullOrEmpty(txtcc.Text))
            {
                return false;
            }
            else if (String.IsNullOrEmpty(txtbrand.Text))
            {
                return false;
            }
            /*else if (String.IsNullOrEmpty(txtprepname.Text))
            {
                return false;
            }
            else if (String.IsNullOrEmpty(txtauthorizedname.Text))
            {
                return false;
            }*/
            else if (cmbrequisitionname.SelectedItem==null)
            {
                return false;
            }
            else if (cmbpreparedname.SelectedItem == null)
            {
                return false;
            }
            else if (cmbauthorizedname.SelectedItem == null)
            {
                return false;
            }
            else if (Convert.ToString(dtreqdate.Value) == "")
            {
                return false;
            }
            else if (Convert.ToString(dtprepdate.Value) == "")
            {
                return false;
            }
            else if (Convert.ToString(dtauthorizedate.Value) == "")
            {
                return false;
            }

            else
            {
                return true;
            }
        }
        private void samplerequisition_Load(object sender, EventArgs e)
        {

            SqlDataAdapter adp = new SqlDataAdapter(cmd);
            cmd.Connection = con;
            cmd.CommandText = "select * from employee";

            adp.Fill(dsreqname,"employee");
            cmbrequisitionname.DataSource = dsreqname.Tables["employee"];
            cmbrequisitionname.DisplayMember = "fname";
            cmbrequisitionname.SelectedIndex = -1;

            adp.Fill(dsprepname, "employees");
            cmbpreparedname.DataSource = dsprepname.Tables["employees"];
            cmbpreparedname.DisplayMember = "fname";
            cmbpreparedname.SelectedIndex = -1;

            adp.Fill(dsauthorizedname, "employees");
            cmbauthorizedname.DataSource = dsauthorizedname.Tables["employees"];
            cmbauthorizedname.DisplayMember = "fname";
            cmbauthorizedname.SelectedIndex = -1;
        }

        private void btnsave_Click(object sender, EventArgs e)
        {
            con.Open();
            if (IsAllValid())
            {

                cmd.CommandText = "insert into samplerequisition(req_no,reqemployee_id,charges,expected_date,reqdate,costcenter_id,specific_brand,preparedemployee_id,prepared_date,authorizedemployee_id,auhtorized_date) values(@req_no,@reqemployee_id,@expected_date,@reqdate,@charges,@costcenter_id,@specific_brand,@preparedemployee_id,@prepared_date,@authorizedemployee_id,@auhtorized_date)";
                cmd.CommandType = CommandType.Text;
                cmd.Parameters.AddWithValue("@req_no",txtreqno.Text);
                cmd.Parameters.AddWithValue("@reqemployee_id",cmbrequisitionname.SelectedValue);
                cmd.Parameters.AddWithValue("@expected_date",txtexpectedate.Text );
                cmd.Parameters.AddWithValue("@reqdate", dtreqdate.Value);
                cmd.Parameters.AddWithValue("@costcenter_id",txtcc.Text);
                cmd.Parameters.AddWithValue("@specific_brand",txtbrand.Text);
                cmd.Parameters.AddWithValue("@preparedemployee_id",cmbpreparedname.SelectedValue);
                cmd.Parameters.AddWithValue("@prepared_date", dtprepdate.Value);
                cmd.Parameters.AddWithValue("@authorizedemployee_id",cmbauthorizedname.SelectedValue);
                cmd.Parameters.AddWithValue("@auhtorized_date", dtauthorizedate.Value);
                if (rdcapex.Checked)
                {
                    cmd.Parameters.AddWithValue("@charges", "Capex");

                }
                else
                {
                    cmd.Parameters.AddWithValue("@charges", "revenue");
                }
                cmd.ExecuteNonQuery();
                MessageBox.Show("record saved","requisition",MessageBoxButtons.OKCancel,MessageBoxIcon.Information);
            }
            else
            {
                MessageBox.Show("error","requisition",MessageBoxButtons.OKCancel,MessageBoxIcon.Error);
                con.Close();
            }
        }
    }
}

1 个答案:

答案 0 :(得分:1)

你永远不会设置"value" to use for your combo boxes。因此,默认情况下,comboboxName.SelectedValue返回整行而不是行的一列。如果您只想要行的一列(我假设fname与显示的值一样),那么只需在代码中添加以下3行。

cmbrequisitionname.ValueMember = "fname";
cmbpreparedname.ValueMember = "fname";
cmbauthorizedname.ValueMember = fname";