Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: VB.NET
I want the value of row(0) to be start from 100000 if the table is null, and that's why I want to check the row(0) and if the value is null, textbox14 should be filled by 100000 so that I can save it to the table. Here's what I've done so far for this task.
Private Sub autoincrement()
        getconnect()
        If con.State = ConnectionState.Closed Then
            con.Open()
        End If
        ds.Tables.Add(dt)
        Dim reg_id As Double
        reg_id = ds.Tables(0).Compute("Max(registrationID)", Nothing)
        If reg_id = vbNull Then
            TextBox14.Text = "100000"
        Else
            TextBox14.Text = reg_id + 1
        End If
    End Sub
When I start debugging, I see this message on the screen--
Conversion from type 'DBNull' to type 'Double' is not valid.
Posted 27-Jan-13 22:55pm
Comments
JemiM at 28-Jan-13 4:24am
   
After getting the Datatable results. what you want to do?
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

Please change your code like below
Private Sub autoincrement()
    getconnect()
    If con.State = ConnectionState.Closed Then
        con.Open()
    End If
    ds.Tables.Add(dt)
    If ds.Tables.Rows.Count <> 0 Then
        Dim reg_id As Double = 0
        reg_id = ds.Tables(0).Compute("Max(registrationID)", Nothing)
        TextBox14.Text = reg_id + 1
    Else
        TextBox14.Text = "100000"
    End If
 

End Sub
  Permalink  
Comments
suhan 2012 at 28-Jan-13 10:30am
   
Working fine. Thanks.
JemiM at 29-Jan-13 1:56am
   
Please put a condition before iterating the rows.
like if(dt.rows.count!=0)
{
//loop here
}
suhan 2012 at 29-Jan-13 2:03am
   
Sir, Sorry for that, actually it is my login form's code.
 
Actually I wanted to tell you that your code is working fine when I open that form as startup form. But when I try to open that form after logging in I'm getting the same error. "Conversion from type 'DBNull' to type 'Double' is not valid." I think there is a conflict between the login form and user form. Can you please tell me how to solve this?
JemiM at 29-Jan-13 2:05am
   
Please put the code that you got exception
suhan 2012 at 29-Jan-13 2:12am
   
Dim reg_id As Double = 0
reg_id = ds.Tables(0).Compute("Max(registrationID)", Nothing)
suhan 2012 at 29-Jan-13 2:16am
   
Actually I'm getting the exception if the table is completely blank and I open that user form after log in form. But when I open it as startup form, it works.
JemiM at 29-Jan-13 2:19am
   
Please put the condition by checking whether collection is empty or not wherever you have write such codes.You can't done any operation(like compute) on that table(dataTable) when the collection is empty
suhan 2012 at 29-Jan-13 2:24am
   
ok, thanks
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Before assigning value to reg_id, check if the value in the table is null or not. Note that you need to check for DbNull.Value and not for nothing.
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

Private Sub autoincrement()
        getconnect()
        If con.State = ConnectionState.Closed Then
            con.Open()
        End If
        ds.Tables.Add(dt)
        Dim reg_id As Double
        reg_id = ds.Tables(0).Compute("Max(registrationID)", Nothing)
'------------- Take note here-------------------------------
         'Get the max row index
         Dim lastRowIndex As Integer = ds.Tables(0).Rows.Count - 1 
         If lastRowIndex < 0 Then 
            ' zero rows
              TextBox14.Text = "100000"
          Else
              Dim IsNum As BooleanIsNum = Double.TryParse(ds.Tables(0).Row(lastRowIndex) & _("registrationID").ToString ,regid)
           TextBox14.Text = reg_id + 1
 
          End If     
'-----------------------------------------------------------
       
            
        
    End Sub
  Permalink  
Comments
suhan 2012 at 28-Jan-13 10:34am
   
Having some problem with BooleanIsNum..
codejet at 29-Jan-13 2:25am
   
Dim IsNum As Boolean

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 195
1 ProgramFOX 130
2 Maciej Los 105
3 Sergey Alexandrovich Kryukov 105
4 Afzaal Ahmad Zeeshan 82
0 OriginalGriff 6,564
1 Sergey Alexandrovich Kryukov 6,048
2 DamithSL 5,228
3 Manas Bhardwaj 4,717
4 Maciej Los 4,150


Advertise | Privacy | Mobile
Web03 | 2.8.1411022.1 | Last Updated 28 Jan 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100