I wrote the following code for inserting the value in table company_db & database is Company_DB but the value are not inserted into the table table shows null values the code is below:

Imports System.Data.OleDb
Imports System.Data.OleDb.OleDbConnection
Imports System.Data.OleDb.OleDbCommand
Public Class Newcompany
    Inherits System.Windows.Forms.Form
    
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        Dim con As OleDbConnection = New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=E:\New Folder\WindowsApplication1\WindowsApplication1\CompanyDB.accdb")
        con.Open()
        Dim cmd As OleDbCommand = New OleDbCommand("INSERT INTO company_db(comp_name, own_name, comp_add, telno, mo_no) VALUES (' " & Me.TextBox1.Text & " ',' " & Me.TextBox2.Text & " ',' " & Me.TextBox3.Text & " '," & Me.TextBox4.Text & "," & Me.TextBox5.Text & ") ", con)

        If String.IsNullOrEmpty(Me.TextBox1.Text.ToString()) = True Then
            MessageBox.Show("Enter company name")
        ElseIf String.IsNullOrEmpty(Me.TextBox2.Text.ToString()) = True Then
            MessageBox.Show("Enter owner name")
        ElseIf String.IsNullOrEmpty(Me.TextBox3.Text.ToString()) = True Then
            MessageBox.Show("Enter company address")
        ElseIf String.IsNullOrEmpty(Me.TextBox5.Text.ToString()) = True Then
            MessageBox.Show("Enter Mob.")
            cmd.ExecuteNonQuery()
            MessageBox.Show("Company Details Entered")
        End If

        TextBox1.Clear()
        TextBox2.Clear()
        TextBox3.Clear()
        TextBox4.Clear()
        TextBox5.Clear()
        con.Close()
    End Sub

Recommended Answers

All 6 Replies

I can't find any code where you are executing the OleDbCommand object.

Use :

OleDbCommand.executeNonquery()

OK!!!

Dim cmd As OleDbCommand = New OleDbCommand("INSERT INTO company_db(comp_name, own_name, comp_add, telno, mo_no) VALUES (' " & Me.TextBox1.Text & " ',' " & Me.TextBox2.Text & " ',' " & Me.TextBox3.Text & " '," & Me.TextBox4.Text & "," & Me.TextBox5.Text & ") ", con)

take a look at your code....

ElseIf String.IsNullOrEmpty(Me.TextBox5.Text.ToString()) = True Then
            MessageBox.Show("Enter Mob.")
            cmd.ExecuteNonQuery()

You only execute that query if Me.TextBox5.Text is empty....

You should return after each string.empty check.
and after the If statement executing that query.

I wrote the following code for inserting the value in table company_db & database is Company_DB but the value are not inserted into the table table shows null values the code is below:

Imports System.Data.OleDb
Imports System.Data.OleDb.OleDbConnection
Imports System.Data.OleDb.OleDbCommand
Public Class Newcompany
    Inherits System.Windows.Forms.Form
    
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        Dim con As OleDbConnection = New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=E:\New Folder\WindowsApplication1\WindowsApplication1\CompanyDB.accdb")
        con.Open()
        Dim cmd As OleDbCommand = New OleDbCommand("INSERT INTO company_db(comp_name, own_name, comp_add, telno, mo_no) VALUES (' " & Me.TextBox1.Text & " ',' " & Me.TextBox2.Text & " ',' " & Me.TextBox3.Text & " '," & Me.TextBox4.Text & "," & Me.TextBox5.Text & ") ", con)

        If String.IsNullOrEmpty(Me.TextBox1.Text.ToString()) = True Then
            MessageBox.Show("Enter company name")
        ElseIf String.IsNullOrEmpty(Me.TextBox2.Text.ToString()) = True Then
            MessageBox.Show("Enter owner name")
        ElseIf String.IsNullOrEmpty(Me.TextBox3.Text.ToString()) = True Then
            MessageBox.Show("Enter company address")
        ElseIf String.IsNullOrEmpty(Me.TextBox5.Text.ToString()) = True Then
            MessageBox.Show("Enter Mob.")
            cmd.ExecuteNonQuery()
            MessageBox.Show("Company Details Entered")
        End If

        TextBox1.Clear()
        TextBox2.Clear()
        TextBox3.Clear()
        TextBox4.Clear()
        TextBox5.Clear()
        con.Close()
    End Sub

when you open the connection, write
object of class.object of connection.open()

take a object of class like
'dim c as new class1'

take object of connection like
'dim con as new connection'

and then use this code to open connection
c.con.open()

use cmd.ExecuteNonQuery() outside the IF ..END IF block.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.