Gridview: howto check if value is empty (coming from SQL DB)?

Ask questions about creating Graphical User Interfaces (GUI) in PowerShell and using WinForms controls.
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
This topic is 3 years and 4 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked
User avatar
stevens
Posts: 493
Last visit: Mon Sep 19, 2022 12:23 am
Has voted: 2 times

Gridview: howto check if value is empty (coming from SQL DB)?

Post by stevens »

Hi,

I 'd need to check if a gridview entry has a value yes or no.
The value for $Result = $($datagridview1.SelectedRows[0].Cells['Myvalue'].Value) I have is System.DBNull.

But there is no way I found yet to check if the value is there
if (($Result -eq [DBNull]::Value or ([DBNull]::Value).Equals([DBNull]::Value)){dosomestuff} else {dootherstuff}
It always shows the value as true, filled in or not

Please advise.

$($datagridview1.SelectedRows[0].Cells['Myvalue'].Value) | gm


TypeName: System.DBNull

Name MemberType Definition
---- ---------- ----------
Equals Method bool Equals(System.Object obj)
GetHashCode Method int GetHashCode()
GetObjectData Method void GetObjectData(System.Runtime.Serialization.SerializationInfo info, System.Runtime.Serialization.StreamingContext context), voi...
GetType Method type GetType()
GetTypeCode Method System.TypeCode GetTypeCode(), System.TypeCode IConvertible.GetTypeCode()
ToBoolean Method bool IConvertible.ToBoolean(System.IFormatProvider provider)
ToByte Method byte IConvertible.ToByte(System.IFormatProvider provider)
ToChar Method char IConvertible.ToChar(System.IFormatProvider provider)
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Gridview: howto check if value is empty (coming from SQL DB)?

Post by jvierra »

DBNull is a type and not a variable. YOu cannot test a type that way. I field in SQL cannot have a scema type of DBNull. A null value in a column can be tested fro dbnull.

$sqlrow[$rowID].Column[0].Value.GetType()
User avatar
stevens
Posts: 493
Last visit: Mon Sep 19, 2022 12:23 am
Has voted: 2 times

Re: Gridview: howto check if value is empty (coming from SQL DB)?

Post by stevens »

Awesome, was at wrong path here. Works correctly, thanks!
This topic is 3 years and 4 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked