Click here to Skip to main content

Pratik65 asked:

Open original thread
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
Tags: VB.NET

Preview



When answering a question please:
  1. Read the question carefully.
  2. Understand that English isn't everyone's first language so be lenient of bad spelling and grammar.
  3. If a question is poorly phrased then either ask for clarification, ignore it, or edit the question and fix the problem. Insults are not welcome.
Let's work to help developers, not make them feel stupid.
Please note that all posts will be submitted under the The Code Project Open License (CPOL).



Advertise | Privacy | Mobile
Web03 | 2.8.140721.1 | Last Updated 26 Mar 2009
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