1

我在 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();
            }
        }
    }
}
4

1 回答 1

1

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

cmbrequisitionname.ValueMember = "fname";
cmbpreparedname.ValueMember = "fname";
cmbauthorizedname.ValueMember = fname";
于 2013-04-23T04:39:11.323 回答