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

本文关键字:类型 程序 本机 映射 System 对象 Data DataRowView 不存在 | 更新日期: 2023-09-27 18:10:21

我有一个c#的Windows窗体,它有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();
            }
        }
    }
}

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

您从未为您的组合框设置什么"值"。因此,默认情况下,comboboxName.SelectedValue返回整行,而不是该行的一列。如果您只想要一行中的一列(我假设fname像显示的值一样),那么只需将以下3行添加到您的代码中。

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