使用新添加的代码

时间:2018-03-08 07:24:23

标签: c# arrays methods

为什么我收到此错误...

我有一个名为Product的类,这是代码的一部分:

public void insertProduct(int cid, string name, string price, byte[] image, int quantity, string desc, int company, int compid)
{

    DB db = new DB();
    db.openConnection();
    SqlParameter[] parameters = new SqlParameter[8];

    parameters[0] = new SqlParameter("@c_id", SqlDbType.Int);
    parameters[0].Value = cid;

    parameters[1] = new SqlParameter("@p_name", SqlDbType.VarChar, 50);
    parameters[1].Value = name;

    parameters[2] = new SqlParameter("@s_qty", SqlDbType.Int);
    parameters[2].Value = quantity;

    parameters[3] = new SqlParameter("@price", SqlDbType.VarChar, 50);
    parameters[3].Value = price;

    parameters[4] = new SqlParameter("@desc", SqlDbType.VarChar);
    parameters[4].Value = desc;

    parameters[5] = new SqlParameter("@img", SqlDbType.Image);
    parameters[5].Value = image;

    parameters[6] = new SqlParameter("@comp", SqlDbType.Int);
    parameters[6].Value = company;

    parameters[7] = new SqlParameter("@comp_id", SqlDbType.Int);
    parameters[7].Value = compid;

    db.setData("spr_insert_product", parameters);
    db.closeConnection();

}

我从表格“添加”按钮

中调用此类
private void BTN_ADD_Click(object sender, EventArgs e)
{
    if (TB_NAME.Text == string.Empty)
    {
        MessageBox.Show("Enter The Product Name", "Empty Data", MessageBoxButtons.OK, MessageBoxIcon.Error);
    }

    if (TB_PRICE.Text == string.Empty && TB_QUANTITY.Text == string.Empty)
    {
        MessageBox.Show("Quantity and Price Can't Be Empty | But Can Be Equal To 0", "Empty Data", MessageBoxButtons.OK, MessageBoxIcon.Error);
    }
    else if (PB_BROWSE_IMAGE.Image == null)
    {
        MessageBox.Show("No Image Selected", "No Image", MessageBoxButtons.OK, MessageBoxIcon.Error);
    }

    else
    {
        MemoryStream ms = new MemoryStream();
        PB_BROWSE_IMAGE.Image.Save(ms, PB_BROWSE_IMAGE.Image.RawFormat);
        byte[] image = ms.ToArray();

            product.insertProduct(Convert.ToInt32(COMBO_CATEGORIES.SelectedValue), TB_NAME.Text,
                                 TB_PRICE.Text, image, Convert.ToInt32(TB_QUANTITY.Text), TB_DESCRIPTION.Text);
        MessageBox.Show("New Product Inserted Successfully", "New Product", MessageBoxButtons.OK, MessageBoxIcon.Information);
    }
}

根据此代码在最后一行:

product.insertProduct(

是我收到此错误的地方,我无法弄清楚原因? 仅供参考......一切正常,直到我将代码添加到方法中:

int company, int compid

与另外两个添加到数据库的列以及代码相关联:

parameters[6] = new SqlParameter("@comp", SqlDbType.Int);
parameters[6].Value = company;

parameters[7] = new SqlParameter("@comp_id", SqlDbType.Int);
parameters[7].Value = compid;

如果我采用这个新添加的代码,我没有收到错误。 谁能发现这里有什么问题以及如何解决它?

1 个答案:

答案 0 :(得分:0)

感谢Piro的评论......深夜,我没有看到它: 这是执行它的代码:

 product.insertProduct(Convert.ToInt32(COMBO_CATEGORIES.SelectedValue), TB_NAME.Text,
                                 TB_PRICE.Text, image, Convert.ToInt32(TB_QUANTITY.Text), TB_DESCRIPTION.Text, TB_COMPANY.Text, TB_COMP_ID.Text);
            MessageBox.Show("New Product Inserted Successfully", "New Product", MessageBoxButtons.OK, MessageBoxIcon.Information);