但它给出了一个错误

但它给出了一个错误

本文介绍了我使用此代码将gridview的值插入数据库,但它给出了一个错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

private void btnsave_Click(object sender, EventArgs e)
        {
             strconn.Open();
            SqlCommand cmdd = new SqlCommand("insert into AS_OrderDetails(proMRP,proQty,proFree,proRate)VALUES(@proMRP1,@proQty1,@proFree1,@proRate1", strconn);
           int i = GridViewSales.Rows.Count; 
           foreach (DataGridViewRow row in GridViewSales.Rows)
            {
                if (i != 0)
                {
                    cmdd.Parameters.Add("@proMRP", SqlDbType.BigInt).Value =  row.Cells["MRP"].Value;
                    cmdd.Parameters.Add("@proQty", SqlDbType.BigInt).Value = row.Cells["Quantity"].Value;
                    cmdd.Parameters.Add("@proFree", SqlDbType.BigInt).Value = row.Cells["Free"].Value;
                    cmdd.Parameters.Add("@proRate", SqlDbType.BigInt).Value = row.Cells["Rate"].Value;
                }
            }

            cmdd.CommandType = System.Data.CommandType.Text;

            cmdd.Parameters.AddWithValue("@proMRP1", txtmrp.Text);
            cmdd.Parameters.AddWithValue("@proQty1", txtqty.Text);
            cmdd.Parameters.AddWithValue("@proFree1", txtfree.Text);
            cmdd.Parameters.AddWithValue("@proRate1", txtrate.Text);
            
            cmdd.ExecuteNonQuery();
            MessageBox.Show("save");
                btnsave.Text = "  Save";
     
        }

推荐答案

private void btnsave_Click(object sender, EventArgs e)
        {
             strconn.Open();
            SqlCommand cmdd = new SqlCommand("insert into AS_OrderDetails(proMRP,proQty,proFree,proRate)VALUES(@proMRP1,@proQty1,@proFree1,@proRate1", strconn);
           /* int i = GridViewSales.Rows.Count; 
           foreach (DataGridViewRow row in GridViewSales.Rows)
            {
                if (i != 0)
                {
                    cmdd.Parameters.Add("@proMRP", SqlDbType.BigInt).Value =  row.Cells["MRP"].Value;
                    cmdd.Parameters.Add("@proQty", SqlDbType.BigInt).Value = row.Cells["Quantity"].Value;
                    cmdd.Parameters.Add("@proFree", SqlDbType.BigInt).Value = row.Cells["Free"].Value;
                    cmdd.Parameters.Add("@proRate", SqlDbType.BigInt).Value = row.Cells["Rate"].Value;
                }
            } */
 
            cmdd.CommandType = System.Data.CommandType.Text;
 
            cmdd.Parameters.AddWithValue("@proMRP1", txtmrp.Text);
            cmdd.Parameters.AddWithValue("@proQty1", txtqty.Text);
            cmdd.Parameters.AddWithValue("@proFree1", txtfree.Text);
            cmdd.Parameters.AddWithValue("@proRate1", txtrate.Text);
            
            cmdd.ExecuteNonQuery();
            MessageBox.Show("save");
                btnsave.Text = "  Save";
     
        }


这篇关于我使用此代码将gridview的值插入数据库,但它给出了一个错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-24 23:02