12 Replies - 2519 Views - Last Post: 09 November 2011 - 10:12 AM Rate Topic: -----

#1 naveed_786  Icon User is offline

  • D.I.C Regular

Reputation: 0
  • View blog
  • Posts: 306
  • Joined: 13-October 09

DBNULL Error in Data Grid

Posted 09 November 2011 - 04:37 AM

Dear All,

I am recieving this error on this line

ctl.Value = CType(Me.Value, DateTime)

Conversion from type 'DBNull' to type 'Date' is not valid.

I am using Data Grid View

and This is a calendar Type Column

This post has been edited by naveed_786: 09 November 2011 - 05:02 AM

Is This A Good Question/Topic? 0
  • +

Replies To: DBNULL Error in Data Grid

#2 smohd  Icon User is offline

  • Critical Section
  • member icon


Reputation: 1817
  • View blog
  • Posts: 4,625
  • Joined: 14-March 10

Re: DBNULL Error in Data Grid

Posted 09 November 2011 - 05:02 AM

What is Me.Value? The error tell that the value you are trying to convert to dateTime is DbNull, so it cant convert to date because it is null value. So first check if the value from the db is not null before trying to convert it to the date...
Was This Post Helpful? 0
  • +
  • -

#3 naveed_786  Icon User is offline

  • D.I.C Regular

Reputation: 0
  • View blog
  • Posts: 306
  • Joined: 13-October 09

Re: DBNULL Error in Data Grid

Posted 09 November 2011 - 06:20 AM

When i change code to now it produces this error

Conversion from string "" to type 'Date' is not valid.

Public Overrides Sub InitializeEditingControl(ByVal rowIndex As Integer, _
        ByVal initialFormattedValue As Object, _
        ByVal dataGridViewCellStyle As DataGridViewCellStyle)

        ' Set the value of the editing control to the current cell value.
        MyBase.InitializeEditingControl(rowIndex, initialFormattedValue, _
            dataGridViewCellStyle)

        Dim ctl As CalendarEditingControl = CType(DataGridView.EditingControl, CalendarEditingControl)
        If ctl.Value = "" Then
            ctl.Value = CType(Me.Value, DateTime)
        End If

    End Sub

Was This Post Helpful? 0
  • +
  • -

#4 naveed_786  Icon User is offline

  • D.I.C Regular

Reputation: 0
  • View blog
  • Posts: 306
  • Joined: 13-October 09

Re: DBNULL Error in Data Grid

Posted 09 November 2011 - 06:37 AM

Problem has been Resolved when i modify code as

Public Overrides Sub InitializeEditingControl(ByVal rowIndex As Integer, _
        ByVal initialFormattedValue As Object, _
        ByVal dataGridViewCellStyle As DataGridViewCellStyle)
        Try
            MyBase.InitializeEditingControl(rowIndex, initialFormattedValue, _
                dataGridViewCellStyle)

            Dim ctl As CalendarEditingControl = CType(DataGridView.EditingControl, CalendarEditingControl)
            ctl.Value = CType(Me.Value, DateTime).ToString.Trim
        Catch ex As Exception
        End Try



Any way Thanks
Was This Post Helpful? 0
  • +
  • -

#5 naveed_786  Icon User is offline

  • D.I.C Regular

Reputation: 0
  • View blog
  • Posts: 306
  • Joined: 13-October 09

Re: DBNULL Error in Data Grid

Posted 09 November 2011 - 08:47 AM

An other Problem with ExecuteScalar

It throws exception on "ExecuteScalar"

Error is

"Operator '+' is not defined for type 'DBNull' and type 'Integer'."


Private Sub Auto_no()
        Dim con As New OracleConnection(ConnectionString)
        Dim com As OracleCommand = Nothing
        Try
            con.Open()
            com = New OracleCommand("SELECT MAX(ISSUENACE_ID) FROM (SELECT * FROM ISSUANCE ORDER BY ISSUENACE_ID DESC)", con)
            txtiiid.Text = com.ExecuteScalar + 1
            con.Close()
        Catch ex As OracleException
            If con.State = ConnectionState.Open Then
                con.Close()
            End If
        End Try
    End Sub



What could be the problem
Was This Post Helpful? 0
  • +
  • -

#6 Tryparse  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 80
  • View blog
  • Posts: 193
  • Joined: 20-April 10

Re: DBNULL Error in Data Grid

Posted 09 November 2011 - 09:13 AM

It appears that ExecuteScalar isn't returning any results. Check your query; I notice you have the table name and column name ("issuenace") spelled different -- is this correct?
Was This Post Helpful? 0
  • +
  • -

#7 naveed_786  Icon User is offline

  • D.I.C Regular

Reputation: 0
  • View blog
  • Posts: 306
  • Joined: 13-October 09

Re: DBNULL Error in Data Grid

Posted 09 November 2011 - 09:16 AM

Yes it is different and both are correct no problem with there names actually there is no record in the table.

If i insert records manually from SQL Plus* it works fine.

What should i do to remove this error.
Was This Post Helpful? 0
  • +
  • -

#8 Tryparse  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 80
  • View blog
  • Posts: 193
  • Joined: 20-April 10

Re: DBNULL Error in Data Grid

Posted 09 November 2011 - 09:33 AM

Ok, so you only get the error when there are no records in the table -- that means that everything is functioning properly. The problem is that when there's no records in the table, there is nothing for ExecuteScalar to return, so it returns a DBNull value, which you then try to add 1 to. Now, VB will let you add 1 to Nothing, but DBNull is actually a special class. The error is telling you that the operator + doesn't know how to add an integer to a DBNull.
What you need to do is convert the results of ExecuteScalar to an integer before trying to add 1 to it. The Convert.ToInt32 function knows to turn a DBNull into 0.
txtiiid.Text = Convert.ToInt32(com.ExecuteScalar) + 1

This post has been edited by Tryparse: 09 November 2011 - 09:34 AM

Was This Post Helpful? 1
  • +
  • -

#9 naveed_786  Icon User is offline

  • D.I.C Regular

Reputation: 0
  • View blog
  • Posts: 306
  • Joined: 13-October 09

Re: DBNULL Error in Data Grid

Posted 09 November 2011 - 09:43 AM

No it was also producing error but now it has been resolve thanks you gave me idea

txtiiid.Text = Convert.IsDBNull(com.ExecuteScalar) + 1

Was This Post Helpful? 0
  • +
  • -

#10 Tryparse  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 80
  • View blog
  • Posts: 193
  • Joined: 20-April 10

Re: DBNULL Error in Data Grid

Posted 09 November 2011 - 09:52 AM

View Postnaveed_786, on 09 November 2011 - 10:43 AM, said:

No it was also producing error but now it has been resolve thanks you gave me idea

txtiiid.Text = Convert.IsDBNull(com.ExecuteScalar) + 1

The only problem there is that IsDBNull returns a boolean. So now you're only ever adding 1 to True or False, so the only values that will ever get put into txtiiid.Text is 1 or 2, as True converts to 1 and False converts to 0.
Was This Post Helpful? 0
  • +
  • -

#11 naveed_786  Icon User is offline

  • D.I.C Regular

Reputation: 0
  • View blog
  • Posts: 306
  • Joined: 13-October 09

Re: DBNULL Error in Data Grid

Posted 09 November 2011 - 09:59 AM

Yes You are Write what should i do i have done as you advise me in your previous post but it is producing this error

on this line

txtiiid.Text = Convert.IsDBNull(com.ExecuteScalar) + 1

error is:

Object cannot be cast from DBNull to other types
Was This Post Helpful? 0
  • +
  • -

#12 Tryparse  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 80
  • View blog
  • Posts: 193
  • Joined: 20-April 10

Re: DBNULL Error in Data Grid

Posted 09 November 2011 - 10:03 AM

Let's try combining your solution and mine. Run the IsDBNull check on it, then try to convert:
Dim objES As Object = com.ExecuteScalar
If Convert.IsDBNull(objES) Then
    txtiiid.Text = 1
Else
    txtiiid.Text = Convert.ToInt32(objES) + 1
End If

See if that works ...

This post has been edited by Tryparse: 09 November 2011 - 10:05 AM

Was This Post Helpful? 1
  • +
  • -

#13 naveed_786  Icon User is offline

  • D.I.C Regular

Reputation: 0
  • View blog
  • Posts: 306
  • Joined: 13-October 09

Re: DBNULL Error in Data Grid

Posted 09 November 2011 - 10:12 AM

Now both Solution are working one provided by you and one from my site


txtiiid.Text = IIf(IsDBNull(com.ExecuteScalar), 0, com.ExecuteScalar) + 1


Thanks you so much.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1