andrewmanuja
Well-known member
- Joined
- May 30, 2019
- Messages
- 75
- Programming Experience
- Beginner
Hi All,
I got a dataGridView and the first column's column type is " DataGridViewCheckBoxColumn".
My requirement is, I want to pass the data from the dataGridView based on the checked item rows to the database.
My code looks like below and when running the application, I am getting the "Object Reference not set to an Instance of an Object".
The error I am getting at the "if" condition.
	
	
	
	
	
	
	
	
	
		
			
			
			
			
			
		
	
	
	
		
	
	
		
	
Appreciate your valuable feedback.
Thank you in advance.
Kind regards,
Andrew
	
		
			
		
		
	
				
			I got a dataGridView and the first column's column type is " DataGridViewCheckBoxColumn".
My requirement is, I want to pass the data from the dataGridView based on the checked item rows to the database.
My code looks like below and when running the application, I am getting the "Object Reference not set to an Instance of an Object".
The error I am getting at the "if" condition.
			
				C#:
			
		
		
		if (ReturnQty > 0)
{
    foreach (DataGridViewRow row in dataGridViewDrugReturn.Rows)
    {
        if ((bool)row.Cells["textselectItem"].Value ==true)
        {
            cmd.Parameters.Clear();
            cmd = new SqlCommand(@"UPDATE tbl_CostCentreDrugStock SET availableQuantity += @areturnQuantity WHERE costCentreID = @acostCentreID AND drugID = @adrugID", con);
            cmd.Parameters.AddWithValue("@acostCentreID", txtCostCentreID.Text);
            cmd.Parameters.AddWithValue("@adrugID", Convert.ToInt32(row.Cells["textdrugID"].Value == DBNull.Value ? "0" : row.Cells["textdrugID"].Value.ToString()));
            cmd.Parameters.AddWithValue("@areturnQuantity", Convert.ToInt32(row.Cells["textreturnQuantity"].Value == DBNull.Value ? "0" : row.Cells["textreturnQuantity"].Value.ToString()));
            cmd.ExecuteNonQuery();
            cmd.Parameters.Clear();
        }
    }
}Thank you in advance.
Kind regards,
Andrew
			
				Last edited by a moderator: 
			
		
	
								
								
									
	
		
			
		
		
	
	
	
		
			
		
		
	
								
							
							 
	 
 
		 
 
		 
 
		 
 
		