Click here to Skip to main content
15,886,199 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
this always gives me an error like above please help me :(
VB
Imports System.Data.OleDb
Imports System.String
' Form Load
Public Class StudInfos
    Private Sub StudInfos_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        CurrentRow = 0
        Con.Open()
        Dad = New OleDbDataAdapter("SELECT * FROM Students ORDER BY NickName", Con)
        Dad.Fill(Dst, "StudsInfo")
        ShowData(CurrentRow)
        Con.Close()
    End Sub
    ' To display data on form
    Private Sub ShowData(ByVal CurrentRow)
        Try
            txtNick.Text = Dst.Tables("StudsInfo").Rows(CurrentRow)("NickName")
            txtFirst.Text = Dst.Tables("StudsInfo").Rows(CurrentRow)("FirstName")
            txtLast.Text = Dst.Tables("StudsInfo").Rows(CurrentRow)("LastName")
            txtAge.Text = Dst.Tables("StudsInfo").Rows(CurrentRow)("Age")
            txtGender.Text = Dst.Tables("StudsInfo").Rows(CurrentRow)("Gender")
            txtAddress.Text = Dst.Tables("StudsInfo").Rows(CurrentRow)("Address")
            txtContact.Text = Dst.Tables("StudsInfo").Rows(CurrentRow)("ContactNo")
        Catch ex As Exception
        End Try
    End Sub

    'To select the data in NickName field
    Private Sub Nick_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtNick.GotFocus
        txtNick.SelectAll()
    End Sub

    ' To insert the record in database
    Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click

        Dim Str As String
        Try
            Str = "insert into Students values("
            Str += txtNick.Text.Trim()
            Str += ","
            Str += """" & txtFirst.Text.Trim() & """"
            Str += ","
            Str += """" & txtLast.Text.Trim() & """"
            Str += ","
            Str += """" & txtAge.Text.Trim() & """"
            Str += ","
            Str += """" & txtGender.Text.Trim() & """"
            Str += ","
            Str += """" & txtAddress.Text.Trim() & """"
            Str += ","
            Str += txtContact.Text.Trim()
            Str += ")"
            Con.Open()
            Cmd = New OleDbCommand(Str, Con)
            Cmd.ExecuteNonQuery()
            Dst.Clear()
            Dad = New OleDbDataAdapter("SELECT * FROM Students ORDER BY NickName", Con)
            Dad.Fill(Dst, "StudsInfo")
            MsgBox("Record inserted successfully...")
            Con.Close()
        Catch ex As Exception
            MessageBox.Show("Could Not Insert Record!!!")
            MsgBox(ex.Message & " -  " & ex.Source)
            Con.Close()
        End Try
    End Sub
    ' To delete the record from database
    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDelete.Click
        Dim Str As String
        If CheckNickName() = False Then
            MsgBox("NickName : Integer Value Required!!!")
            Exit Sub
        End If
        Try
            Str = "DELETE from Students where NickName="
            Str += txtNick.Text.Trim
            Con.Open()
            Cmd = New OleDbCommand(Str, Con)
            Cmd.ExecuteNonQuery()
            Dst.clear()
            Dad = New OleDbDataAdapter("SELECT * FROM StudsInfo ORDER BY NickName", Con)
            Dad.Fill(Dst, "StudsInfo")
            MsgBox("Record deleted successfully...")
            If CurrentRow > 0 Then
                CurrentRow -= 1
                ShowData(CurrentRow)
            End If
            Con.Close()
        Catch ex As Exception
            MessageBox.Show("Could Not delete Record!!!")
            MsgBox(ex.Message & " -  " & ex.Source)
        End Try
        Con.Close()
    End Sub
    ' To select the data in First Name Field
    Private Sub FName_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtLast.GotFocus
        txtFirst.SelectAll()
    End Sub
    ' To select the data in Last Name Field
    Private Sub LName_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtFirst.GotFocus
        txtLast.SelectAll()
    End Sub
    ' To select the data in Designation Field
    Private Sub Designation_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtAddress.GotFocus
        txtAddress.SelectAll()
    End Sub
    ' To select the data in Salary Field
    Private Sub Salary_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs)
        txtAge.SelectAll()
    End Sub
    Private Sub Names_keypress(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtAddress.KeyPress, txtAge.KeyPress, txtContact.KeyPress, txtFirst.KeyPress, txtGender.KeyPress, txtLast.KeyPress, txtNick.KeyPress
        If CheckNickName() = False Then
            MsgBox("NickName : Integer Value Required !!!")
            Exit Sub
        ElseIf CheckFName() = False Then
            MsgBox("FName : Only Characters Allowed!!!")
            Exit Sub
        ElseIf CheckLName() = False Then
            MsgBox("LName : Only Characters Allowed!!!")
            Exit Sub
        ElseIf CheckAge() = False Then
            MsgBox("age : Integer Value Required !!!")
            Exit Sub
        ElseIf CheckGender() = False Then
            MsgBox("gender : Integer  Allowed!!!")
            Exit Sub
        ElseIf CheckAddress() = False Then
            MsgBox("Address : Only Characters Allowed!!!")
            Exit Sub
        ElseIf CheckContactNo() = False Then
            MsgBox("contactno : Integer  Allowed!!!")
            Exit Sub
        End If
    End Sub
    ' To update the records in database
    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click

        Try
            Dim Str As String
            Str = "Update Students set NickName="
            Str += """" & txtNick.Text & """"
            Str += " where NickName="
            Str += txtNick.Text.Trim()
            Con.Open()
            Cmd = New OleDbCommand(Str, Con)
            Cmd.ExecuteNonQuery()
            Con.Close()

            Con.Open()
            Str = "update StudsInfo set FirstName="
            Str += """" & txtFirst.Text & """"
            Str += " where NickName="
            Str += txtNick.Text.Trim()
            Cmd = New OleDbCommand(Str, Con)
            Cmd.ExecuteNonQuery()
            Con.Close()

            Con.Open()
            Str = "update Students set LastName="
            Str += """" & txtLast.Text & """"
            Str += " where NickName="
            Str += txtNick.Text.Trim()
            Cmd = New OleDbCommand(Str, Con)
            Cmd.ExecuteNonQuery()
            Con.Close()

            Con.Open()
            Str = "update StudsInfo set Age="
            Str += """" & txtAge.Text & """"
            Str += " where NickName="
            Str += txtNick.Text.Trim()
            Cmd = New OleDbCommand(Str, Con)
            Cmd.ExecuteNonQuery()
            Con.Close()

            Con.Open()
            Str = "update StudsInfo set Gender="
            Str += """" & txtGender.Text & """"
            Str += " where NickName="
            Str += txtNick.Text.Trim()
            Cmd = New OleDbCommand(Str, Con)
            Cmd.ExecuteNonQuery()
            Con.Close()

            Con.Open()
            Str = "update StudsInfo set Address="
            Str += """" & txtAddress.Text & """"
            Str += " where NickName="
            Str += txtNick.Text.Trim()
            Cmd = New OleDbCommand(Str, Con)
            Cmd.ExecuteNonQuery()
            Con.Close()


            Con.Open()
            Str = "update StudsInfos set ContactNo="
            Str += """" & txtContact.Text & """"
            Str += " where NickName="
            Str += txtNick.Text.Trim()
            Cmd = New OleDbCommand(Str, Con)
            Cmd.ExecuteNonQuery()
            Con.Close()


            Dst.Clear()
            Dad = New OleDbDataAdapter("SELECT * FROM Students ORDER BY NickName", Con)
            Dad.Fill(Dst, "StudsInfo")
            MsgBox("Updated Successfully...")
        Catch ex As Exception
            MsgBox(ex.Message & "," & ex.Source)
        End Try
    End Sub
    ' To Navigate to First Record
    Private Sub First_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles First.Click
        CurrentRow = 0
        ShowData(CurrentRow)
    End Sub
    ' To Navigate to Last Record
    Private Sub Previous_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Previous.Click
        If CurrentRow <> 0 Then
            CurrentRow -= 1
            ShowData(CurrentRow)
        Else
            MsgBox("First Record is Reached!!!")
        End If
    End Sub
    ' To Navigate to Next Record
    Private Sub Forward_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Forward.Click
        If CurrentRow = Dst.Tables("StudsInfo").Rows.Count - 1 Then
            MsgBox("Last Record is Reached!!!")
        Else
            CurrentRow += 1
            ShowData(CurrentRow)
        End If
    End Sub
    ' To Navigate to Last Record
    Private Sub Last_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Last.Click
        CurrentRow = Dst.Tables("StudsInfo").Rows.Count - 1
        ShowData(CurrentRow)
    End Sub
    ' To check the data in NickName field : whether String or not 
    Private Function CheckNickName()
        Try
            If txtNick.Text = "" Or ValNickNameateString(txtNick.Text) = False Then
                ShowData(CurrentRow)
                txtNick.Focus()
                Return False
            End If
        Catch ex As Exception
        End Try
        Return True
    End Function

    ' To check the data in Age field : whether numeric or not 
    Private Function CheckAge()
        Try
            If IsNumeric(txtAge.Text) = False Then
                ShowData(CurrentRow)
                txtAge.Focus()
                Return False
            End If
        Catch ex As Exception
        End Try
        Return True
    End Function
    ' To check the data in Contactno field : whether numeric or not 
    Private Function CheckContactNo()
        Try
            If IsNumeric(txtContact.Text) = False Then
                ShowData(CurrentRow)
                txtContact.Focus()
                Return False
            End If
        Catch ex As Exception
        End Try
        Return True
    End Function

    ' To check the data in First Name Field : whether a string or not

    Private Function CheckFName()
        Try
            If txtFirst.Text = "" Or ValNickNameateString(txtFirst.Text) = False Then
                ShowData(CurrentRow)
                txtFirst.Focus()
                Return False
            End If
        Catch ex As Exception
        End Try
        Return True
    End Function
    ' To check the data in Last Name Field : whether a string or not
    Private Function CheckLName()
        Try
            If txtLast.Text = "" Or ValNickNameateString(txtLast.Text) = False Then
                ShowData(CurrentRow)
                txtLast.Focus()
                Return False
            End If
        Catch ex As Exception
        End Try
        Return True
    End Function
    ' To check the data in address Field : whether a string or not
    Private Function CheckAddress()
        Try
            If txtAddress.Text = "" Or ValNickNameateString(txtAddress.Text) = False Then
                ShowData(CurrentRow)
                txtAddress.Focus()
                Return False
            End If
        Catch ex As Exception
        End Try
        Return True
    End Function
    Private Function CheckGender()
        Try
            If txtGender.Text = "" Or ValNickNameateString(txtGender.Text) = False Then
                ShowData(CurrentRow)
                txtGender.Focus()
                Return False
            End If
        Catch ex As Exception
        End Try
        Return True
    End Function
    ' To check the string for numeric values
    Private Function ValNickNameateString(ByVal Str)
        Dim i As Integer
        Dim ch As Char
        i = 0
        While i < Str.Length()
            ch = Str.Chars(i)
            If IsNumeric(ch) = True Then
                Return False
            End If
            i += 1
        End While
        Return True
    End Function

    ' To check whether NickName exist in database
    Private Function IsNickNameExist()
        Dim Str, Str1 As String
        Dim i As Integer
        Str = txtNick.Text
        i = 0
        While i <> Dst.Tables("StudsInfo").rows.count
            Str1 = Dst.Tables("StudsInfo").Rows(i)("NickName")
            If Str = Str1 Then
                Return True
            End If
            i += 1
        End While
        Return False
    End Function

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        ViewStudents.Show()
    End Sub

    Private Sub btnClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClear.Click
        ' To clear all fields : NickName, First Name, Last Name
        txtNick.Text = ""
        txtFirst.Text = ""
        txtLast.Text = ""
        txtAge.Text = ""
        txtAddress.Text = ""
        txtGender.Text = ""
        txtContact.Text = ""
    End Sub
End Class
Posted
Updated 2-Sep-14 3:24am
v2
Comments
OriginalGriff 17-Aug-14 4:11am    
That is a code dump.
It's also an unformatted code dump, so it's difficult to read.
Edit your question, cut that down to just the relevant part of your code that gives the problem, use pre tags to preserve the formatting, and we'll see.
Would you want to wade through that lot looking for what *might* be causing your problem? Because I don't!
Help us to help you!

Use the "Improve question" widget to edit your question and provide better information.
PhilLenoir 18-Aug-14 11:03am    
What OriginalGriff said, but the error looks like an INSERT statement is missing a value in a required column.
Richard Deeming 2-Sep-14 9:56am    
Your code is susceptible to SQL Injection[^]. Use parameterized queries instead of string concatenation.

1 solution

Many possibilities. Make sure you're passing right values to query.
Case 1
VB
Str = "insert into Students values("
Str += txtNick.Text.Trim()
Str += ","
Str += """" & txtFirst.Text.Trim() & """"
Str += ","
Str += """" & txtLast.Text.Trim() & """"
Str += ","
Str += """" & txtAge.Text.Trim() & """"
Str += ","
Str += """" & txtGender.Text.Trim() & """"
Str += ","
Str += """" & txtAddress.Text.Trim() & """"
Str += ","
Str += txtContact.Text.Trim()
Str += ")"
If you miss any value, this query'd fail. Say you're passing blank values, you'll get below query which is bad one.
SQL
insert into Students values(,'','','','','')
Case 2
VB
Str = "DELETE from Students where NickName="
Str += txtNick.Text.Trim
Same as above, if you pass blank value, you'll get below query which won't work.
SQL
DELETE from Students where NickName=

Case N
Samething goes for all other UPDATE queries. If you pass blankstring('') when your validation fails, that query won't work.
 
Share this answer
 

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