1
string IdDB = "select transid from transtbl where fullname = '"+textBox6.Text+"'";
                con.SelectRec(IdDB, dt);
                string IdDB2 = dt.Rows[0].ItemArray[0].ToString();

                for (int rows = 0; rows < dataGridView2.Rows.Count; rows++)
                {
                    int pProdID = int.Parse(dataGridView2.Rows[rows].Cells[0].Value.ToString());
                    string pProdName = dataGridView2.Rows[rows].Cells[1].Value.ToString();
                    double pPrice = double.Parse(dataGridView2.Rows[rows].Cells[2].Value.ToString());
                    int qQty = int.Parse(dataGridView2.Rows[rows].Cells[3].Value.ToString());
                    double tttTotal = double.Parse(dataGridView2.Rows[rows].Cells[4].Value.ToString());

                    string transdltdSql = "Insert into transdetailedtbl (transID, ProdID, ProdPrice, Qty, Total) value ("+int.Parse(IdDB2)+","+pProdID+","+pPrice+","+qQty+","+tttTotal+")";
                    con.ModRec(transdltdSql);
                } 

I get an error

System.NullReferenceException was unhandled HResult=-2147467261 Message=Object reference not set to an instance of an object.

anyone help me please. i was trying to execute query in a for loop but the value im getting is from the gridview.

Ibn Injal
  • 11
  • 2
  • Possible duplicate of [What is a NullReferenceException and how do I fix it?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – Backs Oct 11 '15 at 02:46
  • You have a SQL injection vulnerability. – SLaks Oct 11 '15 at 02:55
  • Any reason why you don't use Data Binding ? – Luc Morin Oct 11 '15 at 03:03
  • @Backs if i try to show it using message box it has value but when execute in with the sql its says the error – Ibn Injal Oct 11 '15 at 04:22
  • @LucMorin As far as i know databinding is if you are getting data from database. for what i will do is to get the data from datagridview to database – Ibn Injal Oct 11 '15 at 04:23
  • thank you guys for the concern :). i just solve the problem :)) i just use this int pProdID = Convert.ToInt32(dataGridView2.Rows[rows].Cells[0].Value); rather than this int pProdID = int.Parse(dataGridView2.Rows[rows].Cells[0].Value.ToString()); s – Ibn Injal Oct 11 '15 at 05:03
  • @IbnInjal Actually, databinding is good for both directions. This means you can still use it for scenarios where the data is filled by the user and then sent to DB. Cheers – Luc Morin Oct 11 '15 at 19:13

1 Answers1

0

thank you guys for the concern :). i just solve the problem :)) i just use this

int pProdID = Convert.ToInt32(dataGridView2.Rows[rows].Cells[0].Value); 

rather than this

int pProdID = int.Parse(dataGridView2.Rows[rows].Cells[0].Value.ToString());
Backs
  • 22,763
  • 4
  • 44
  • 72
Ibn Injal
  • 11
  • 2