Hi Folks.  I am trying to evaluate the contents of my datagridview cells and I'm hitting some difficulties.
My Datagridview is populated on the fly, its AutoGenerateColumns property set to TRUE. Its datasource is a SQl table with four columns of index info and three columns of data. One column (NVAL) is for numeric data, CVAL for alphanumeric, and DVAL for dates. Each record of the table represents a different measurement so only one data column will be populated for a given row. After the user has edited the grid, they can save, which writes the values back to the table using this code (note: clsGenericQuery packages up the SQL call):
	
	
	
	
	
	
	
	
	
		
			
			
			
			
			
		
	
	
	
		
	
	
		
	
The problem arises when a table value behind the first data cell -- the NVAL on the first row -- is null. It keeps erroring out on the line marked with ***, the message complaining about casting from dbnull. I have tried numerous syntaxes here, such as
When I debug and look at the value of row.cells(4) the value appears to be {}. A null in row.cells(6) [date] in debug, on the other hand, shows as #12:00AM#.
Data-wise, I'm perfectly content for the unused data values to be null. I'd much rather see blanks in my datagridview than zeroes and 1/1/1900s in empty fields. But how can I 'read' a null value in a cell with a decimal field behind it?
Thanks for any help...
	
		
			
		
		
	
				
			My Datagridview is populated on the fly, its AutoGenerateColumns property set to TRUE. Its datasource is a SQl table with four columns of index info and three columns of data. One column (NVAL) is for numeric data, CVAL for alphanumeric, and DVAL for dates. Each record of the table represents a different measurement so only one data column will be populated for a given row. After the user has edited the grid, they can save, which writes the values back to the table using this code (note: clsGenericQuery packages up the SQL call):
			
				VB.NET:
			
		
		
		Private Sub btnSave_Click(sender As System.Object, e As System.EventArgs) Handles btnSave.Click
        Dim cmd As String
        Dim AC As String = ""   'Attribute Code
        Dim NV As Decimal = 0  'Numeric Value
        Dim CV As String = ""    'Character Value
        Dim DV As Date            'Date Value
        Dim VALtext As String = ""
        For Each row As DataGridViewRow In Me.DataGridView1.Rows
            'remember, cell numbers are zero-based
            AC = row.Cells(3).Value
            If row.Cells(4).Value = "" Then      '***
                NV = 0
            Else
                NV = row.Cells(4).Value
                VALtext = " NVAL = '" & NV & "'"
            End If
            If row.Cells(5).Value.ToString = vbNullString Then
                CV = ""
            Else
                CV = row.Cells(5).Value
                VALtext = " CVAL = '" & CV & "'"
            End If
            If row.Cells(6).Value.ToString = vbNullString Then
            Else
                DV = System.Convert.ToDateTime(row.Cells(6).Value)
                VALtext = "DVAL = '" & DV & "'"
            End If
            If NV = 0 And CV = "" And DV = "12:00AM" Then
            Else
                cmd = "update Attributes set " & VALtext _
                    & "where DeviceSource = '" & My.Settings.GDeviceSource & "' and " _
                    & "DeviceID = '" & My.Settings.GDeviceID & "' and " _
                    & "AttributeCode = '" & AC & "'"
                Dim gqupd As New clsGenericQuery(cmd, My.Settings.Connstring)
            End If
        Next
    End SubThe problem arises when a table value behind the first data cell -- the NVAL on the first row -- is null. It keeps erroring out on the line marked with ***, the message complaining about casting from dbnull. I have tried numerous syntaxes here, such as
If row.Cells(4).Value = "" 
If row.Cells(4).Value > 0
If row.Cells(4).Value = vbNull
If row.Cells(4).Value is vbnull
If row.Cells(4).Value = {}
If system.convert.todecimal(row.Cells(4).Value) is vbNull, > 0, etc.
If row.Cells(4).Value > 0
If row.Cells(4).Value = vbNull
If row.Cells(4).Value is vbnull
If row.Cells(4).Value = {}
If system.convert.todecimal(row.Cells(4).Value) is vbNull, > 0, etc.
When I debug and look at the value of row.cells(4) the value appears to be {}. A null in row.cells(6) [date] in debug, on the other hand, shows as #12:00AM#.
Data-wise, I'm perfectly content for the unused data values to be null. I'd much rather see blanks in my datagridview than zeroes and 1/1/1900s in empty fields. But how can I 'read' a null value in a cell with a decimal field behind it?
Thanks for any help...
 
	 
 
		 
 
		 
 
		 
 
		 
 
		