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 to auto generate an id column in my project database I am using the following code, but it is not working.
I have Set "Id" column as "Int" in my Database.
It is giving me an exception as "Conversion From String To Double is Not Valid"
Private Sub BtnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        conn.Open()
        Dim query As String = "insert into Academic(Id,Session)values(@Id,@Session)"
        If TxtSession.Text = "" Then
            MsgBox("Please Insert the Session ", MsgBoxStyle.Information, "Grading System")
            TxtSession.Text = ""
            txtSessionId.Text = ""
            TxtSession.Focus()
        Else
 
            Dim query1 As String = "select Id,Session from Academic Where Session='" & TxtSession.Text & "'"
            cmd1 = New SqlCommand(query1, conn)
            dr = cmd1.ExecuteReader()
            If dr.Read = False Then
                dr.Close()
                Try
                    
                    Try
                        Dim no As integer
                        Dim k as integer
                        cmd = New SqlCommand("select count(*) from Academic", conn)
                        no = cmd.ExecuteScalar
                        
                        no = CInt(no) + 1
 
                       
                        If no > 0 And no <= 9 Then
                            k = ("A000" + no)
                        ElseIf no > 9 And no <= 99 Then
                            k = ("A00" + no)
                        ElseIf no > 99 And no <= 999 Then
                            k = ("A0" + no)
                        Else
                            k = ("A" + no)
                        End If
                    Catch ex As Exception
                        MsgBox(ex.Message)

                    End Try
 
                    cmd = New SqlCommand(query, conn)
                    dr.Close()
                    cmd.Parameters.AddWithValue("@Id", (k))
                    cmd.Parameters.AddWithValue("@Session", (TxtSession.Text))
                    cmd.ExecuteNonQuery()
                    MsgBox("Session Inserted", MsgBoxStyle.Information, "Grading System")
                    ds.Clear()
                    conn.Close()
                    LoadDataInDataGrid()
                    TxtSession.Text = ""
                    TxtSession.Focus()
 
                Catch ex As Exception
                    MessageBox.Show(ex.ToString())
                Finally
                    If conn.State = ConnectionState.Open Then
                        conn.Close()
                    End If
                End Try
            Else
                MsgBox("Record Exists", MsgBoxStyle.Information, "Grading System")
                TxtSession.Focus()
                txtSessionId.Text = ""
 
            End If
        End If
        conn.Close()
    End Sub
Posted 20-Nov-12 10:30am
Edited 20-Nov-12 10:36am
v2

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Is it as the exception is telling you.
You are trying to store a string (k) into a int field (Id).
Either change the field to a string or change the code to generate id of type int.
  Permalink  
Comments
Pratik65 at 20-Nov-12 15:48pm
   
ya now i changed the ID field to text and now it is giving me startindex error
André Kraak at 20-Nov-12 15:54pm
   
You need to check if the index returned is valid before using it.
Pratik65 at 20-Nov-12 15:54pm
   
now my code is :
but now it is giving me exception about the "startIndex Cannot be Larger Than Length of String , Parameter Name: startIndex "
 
Private Sub AutoGenerateNo()

Dim no As String
Try
cmd = New SqlCommand("select count(*) from Academic", conn)
no = cmd.ExecuteScalar
no = no.Substring(2)
no = CInt(no) + 1
 
If no > 0 And no <= 9 Then
txtSessionId.Text = ("A000" + no)
ElseIf no > 9 And no <= 99 Then
txtSessionId.Text = ("A00" + no)
ElseIf no > 99 And no <= 999 Then
txtSessionId.Text = ("A0" + no)
Else
txtSessionId.Text = ("A" + no)
End If
Catch ex As Exception
MsgBox(ex.Message)
Finally
conn.Close()
End Try
 
End Sub
André Kraak at 20-Nov-12 15:59pm
   
It is telling you that the length of the no string is smaller than 2.
Why are you using substring anyway, just convert the result to an integer.
Pratik65 at 20-Nov-12 15:56pm
   
how should i check the idex returned is valid or not ?
Pratik65 at 20-Nov-12 16:03pm
   
i got it thank u very very much ..andre
André Kraak at 20-Nov-12 16:05pm
   
You are welcome and happy coding :).

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

  Print Answers RSS
0 OriginalGriff 5,170
1 DamithSL 4,357
2 Maciej Los 3,750
3 Kornfeld Eliyahu Peter 3,470
4 Sergey Alexandrovich Kryukov 2,851


Advertise | Privacy | Mobile
Web01 | 2.8.141216.1 | Last Updated 20 Nov 2012
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