Hi,@Puskas Filip, PravBcEx14. Welcome Microsoft Q&A.
The error message "Object reference not set to an instance of an object" typically occurs when you try to access a property or method on an object that is currently null
. In the context of your code, this error likely happens when dtgClients.SelectedItem
or (dtgClients.SelectedItem as Client)
is null
.
To fix this issue, you could add a null check to ensure that dtgClients.SelectedItem
is not null
before trying to access its properties.
if (dtgClients.SelectedItem != null)
{
if (dtgClients.SelectedItem is Client selectedClient)
{
int ID = Convert.ToInt32(selectedClient.id);
}
else
{
// Handle the case where the selected item is not a 'Client'
...
}
}
else
{
// Handle the case where nothing is selected in the dataGrid
}
If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.