我使用此代码将gridview的值插入数据库,但它给出了一个错误 [英] i use this code to insert value of gridview to database but it give an error

查看:55
本文介绍了我使用此代码将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";
     
        }

推荐答案

希望这可以帮助您理解问题,尝试执行以下代码并查看会发生什么。



Hope this helps you understand the problem, try executing below code and see what happens.

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的值插入数据库,但它给出了一个错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆