for (int j = 0; j < dgvcheckin.Rows.Count; j++)
{
if (Convert.ToBoolean(dgvcheckin.Rows[j].Cells["RoomNo"].Value) == null)
{
dgvcheckin.Rows.Remove(dgvcheckin.CurrentRow);
}
}
int NumberofRooms=1;
for (int j = 0; j < dgvcheckin.Rows.Count;j++ )
{
if (dgvcheckin.Rows[j].Cells["RoomNo"].Value.ToString() != "" || dgvcheckin.Rows[j].Cells["RoomNo"].Value != null)
{
NumberofRooms=+NumberofRooms;
}
}
This is my code when compiler check the datagridview room cell it give back null then it going to exception and skip the remaining the code any body have any idea....
This is my code when compiler check the datagridview room cell it give back null then it going to exception and skip the remaining the code any body have any idea....


Michal HabalcikRank #902 · 2.3K reputationPosted Dec 5, 2014, 1:41 AM EST
This condition
dgvcheckin.Rows[j].Cells["RoomNo"].Value.ToString() != "" || gvcheckin.Rows[j].Cells["RoomNo"].Value != null
can be written as String.IsNullOrEmpty(dgvcheckin.Rows[j].Cells["RoomNo"].Value)
Kunal VaishyaRank #990 · 2K reputationPosted Dec 5, 2014, 12:44 AM EST
Check null value before Convert the boolean value,because null value can not be converted in any type.
Regards,
kingskunal.
Michal HabalcikRank #902 · 2.3K reputationPosted Dec 5, 2014, 1:18 AM EST
for (int j = 0; j < dgvcheckin.Rows.Count; j++)
{
if (dgvcheckin.Rows[j].Cells["RoomNo"].Value != null && Convert.ToBoolean(dgvcheckin.Rows[j].Cells["RoomNo"].Value) == null)
{
dgvcheckin.Rows.Remove(dgvcheckin.CurrentRow);
}
}
int NumberofRooms=1;
for (int j = 0; j < dgvcheckin.Rows.Count;j++ )
{
if (dgvcheckin.Rows[j].Cells["RoomNo"].Value != null || dgvcheckin.Rows[j].Cells["RoomNo"].Value.ToString() != "" )
{
NumberofRooms=+NumberofRooms;
}
}
Michal HabalcikRank #902 · 2.3K reputationPosted Dec 5, 2014, 1:28 AM EST
Here is the documentation for Convert.ToBoolean:
http://msdn.microsoft.com/en-us/library/wh2c31dd%28v=vs.110%29.aspx?cs-save-lang=1&cs-lang=csharp#code-snippet-1
mohammed shamsheerRank #4,152 · 361 reputationPosted Dec 5, 2014, 1:37 AM EST
mohammed shamsheerRank #4,152 · 361 reputationPosted Dec 5, 2014, 1:53 AM EST
mohammed shamsheerRank #4,152 · 361 reputationPosted Dec 5, 2014, 1:55 AM EST