Click here to Skip to main content
15,894,740 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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.
VB
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
Comments
Jameel VM 28-Jan-13 4:24am    
After getting the Datatable results. what you want to do?

Please change your code like below
VB
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
 
Share this answer
 
Comments
suhan 2012 28-Jan-13 10:30am    
Working fine. Thanks.
Jameel VM 29-Jan-13 1:56am    
Please put a condition before iterating the rows.
like if(dt.rows.count!=0)
{
//loop here
}
suhan 2012 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?
Jameel VM 29-Jan-13 2:05am    
Please put the code that you got exception
suhan 2012 29-Jan-13 2:12am    
Dim reg_id As Double = 0
reg_id = ds.Tables(0).Compute("Max(registrationID)", Nothing)
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.
 
Share this answer
 
VB
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
 
Share this answer
 
Comments
suhan 2012 28-Jan-13 10:34am    
Having some problem with BooleanIsNum..
codejet 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)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900