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.
Post Reply
User avatar
stevens
Posts: 440
Joined: Wed Jun 15, 2016 2:26 am

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: 14672
Joined: Tue May 22, 2007 9:57 am
Answers: 6
Has voted: 1 time
Been upvoted: 5 times
Contact:

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: 440
Joined: Wed Jun 15, 2016 2:26 am

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

Post by stevens »

Awesome, was at wrong path here. Works correctly, thanks!

Post Reply