从gridview插入mysql获取null异常

时间:2012-03-27 21:46:51

标签: c#

我在c#中有一个datagridview,我从mysql数据库填充。用户查看信息,然后将其保存到不同的mysql数据库。我知道两个工作的连接和我的标题之间的连接说我在for循环的第一个字符串上得到一个空例外。

行中只有6列,我唯一能想到的是我正在查看行而不是列,但不确定代码应该是什么编辑我将其更改为大部分曾对<而不是<=说过,我知道为什么我会收到错误,但不知道如何获取我需要的信息。错误来自于即使单元格中存在值,单元格显示为null的事实。

string ConnectionString2 = ConfigurationSettings.AppSettings["ConnectionString2"];
MySqlConnection connection2;
connection2 = new MySqlConnection(ConnectionString2);

int i;
for (i = 0; i < dataGridView1.Rows.Count; i++)
{
    string day = dataGridView1.Rows[i].Cells[0].Value.ToString();
    string Desc = dataGridView1.Rows[i].Cells[1].Value.ToString();
    string item = dataGridView1.Rows[i].Cells[2].Value.ToString();
    string prod = dataGridView1.Rows[i].Cells[3].Value.ToString();
    string vol = dataGridView1.Rows[i].Cells[4].Value.ToString();
    string qty = dataGridView1.Rows[i].Cells[5].Value.ToString();

    MySqlCommand cmdwk = new MySqlCommand("INSERT INTO spt_proposal_line_lab (proposal_Id,day_Name,proposal_Desc,proposal_Vol,product_Id,proposal_Qty,item_Id) VALUES (@propid,@day,@desc,@vol,@prod,@qty,@item)", connection2);
    MySqlParameter propid = new MySqlParameter("@propid", b);
    MySqlParameter day1 = new MySqlParameter("@day", day);
    MySqlParameter Desc1 = new MySqlParameter("@desc", Desc);
    MySqlParameter vol1 = new MySqlParameter("@vol", vol);
    MySqlParameter prod1 = new MySqlParameter("@prod", prod);
    MySqlParameter qty1 = new MySqlParameter("@qty", qty);
    MySqlParameter item1 = new MySqlParameter("@item", item);

    cmdwk.Parameters.Add(day1);
    cmdwk.Parameters.Add(propid);
    cmdwk.Parameters.Add(Desc1);
    cmdwk.Parameters.Add(prod1);
    cmdwk.Parameters.Add(vol1);
    cmdwk.Parameters.Add(qty1);
    cmdwk.Parameters.Add(item1); 
}

调用datagrid并在按钮中填充此负载 编辑看到我离我的img post能力有一点点我会尝试更多地解释这一点 单击该按钮并运行此代码时,错误出现在

string day = dataGridView1.Rows[i].Cells[0].Value.ToString();

错误是

Object reference not set to an instance of an object.

System.NullReferenceException was unhandled
  HResult=-2147467261
   Message=Object reference not set to an instance of an object.
    Source=SpectLabRemake2
    StackTrace:
   at SpectLabRemake2.Form6.btn_Save_Click(Object sender, EventArgs e) 

2 个答案:

答案 0 :(得分:1)

我认为就是这条线:

for (i = 0; i <= dataGridView1.Rows.Count; i++)

你走得太远了。尝试:

for (i = 0; i < dataGridView1.Rows.Count; i++)

答案 1 :(得分:0)

对于遇到此问题的每个人,我会发布修复此问题的代码,并且非常感谢那些对此问题很有帮助的人。

  for (i = 0; i < dataGridView1.Rows.Count-1; i++)
        {

            string ConnectionString2 = ConfigurationSettings.AppSettings["ConnectionString2"];
            MySqlConnection connection2;








            connection2 = new MySqlConnection(ConnectionString2);
            connection2.Open();


                string day = dataGridView1.Rows[i].Cells[0].Value.ToString();
                string Desc = dataGridView1.Rows[i].Cells[1].Value.ToString();
                string item = dataGridView1.Rows[i].Cells[2].Value.ToString();
                string prod = dataGridView1.Rows[i].Cells[3].Value.ToString();
                string vol = dataGridView1.Rows[i].Cells[4].Value.ToString();
                string qty = dataGridView1.Rows[i].Cells[5].Value.ToString();


                MySqlCommand cmdwk = new MySqlCommand("INSERT INTO spt_proposal_line_lab (proposal_Id,day_Name,proposal_Desc,proposal_Vol,product_Id,proposal_Qty,item_Id) VALUES (@propid,@day,@desc,@vol,@prod,@qty,@item)", connection2);
                MySqlParameter propid = new MySqlParameter("@propid", b);
                MySqlParameter day1 = new MySqlParameter("@day", day);
                MySqlParameter Desc1 = new MySqlParameter("@desc", Desc);
                MySqlParameter vol1 = new MySqlParameter("@vol", vol);
                MySqlParameter prod1 = new MySqlParameter("@prod", prod);
                MySqlParameter qty1 = new MySqlParameter("@qty", qty);
                MySqlParameter item1 = new MySqlParameter("@item", item);

                cmdwk.Parameters.Add(day1);
                cmdwk.Parameters.Add(propid);
                cmdwk.Parameters.Add(Desc1);
                cmdwk.Parameters.Add(prod1);
                cmdwk.Parameters.Add(vol1);
                cmdwk.Parameters.Add(qty1);
                cmdwk.Parameters.Add(item1);
                cmdwk.ExecuteNonQuery();



            }
        }

    }
}

布伦特