Question table and database connection through textbox and add save edit command not working

krishanusen

New member
Joined
Apr 14, 2021
Messages
4
Programming Experience
1-3
i have getting error when i was writing update command.please help.in this programme database path show textbox3 and table name textbox4.when i add any data in access database then it was show in datagridview.
VB.NET:
Imports System.Data.OleDb
Imports System.Data.SqlClient
Public Class Form9

    Dim connstring As String
    Dim command As String
    Public dts As DataTable
 
    Dim datatable As New DataTable
    Dim cmd As New OleDbCommand
 
    Dim objdatatable As DataTable
    Dim da As New OleDb.OleDbCommandBuilder
    Dim Myconnection As OleDbConnection
    Dim dbda As OleDbDataAdapter
    Dim dbds As DataSet
    Dim dt As DataTable
    Dim tables As DataTableCollection
    Dim source As New BindingSource
    Dim dsNewRow As DataRow
    Dim dbconn As New OleDbConnection
 

    Private Sub Form9_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        dbconn.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0; Data Source= F:\KRISHANU PROJECT\WindowsApplication1\WindowsApplication1\bin\Debug\maindata.accdb"

        showData() 'show database values in datagridview
        Dim SchemaTable As DataTable
        Dim FILE_NAME As String = My.Application.Info.DirectoryPath.ToString() & "\INFO.TXT"

        If System.IO.File.Exists(FILE_NAME) = True Then

            Dim objReader As New System.IO.StreamReader(FILE_NAME)
            TextBox3.Text = objReader.ReadToEnd
            objReader.Close()

        Else

            MessageBox.Show("File Does Not Exist")

        End If

        ListBox1.Items.Clear()


        Dim con As New OleDb.OleDbConnection("PROVIDER=Microsoft.ACE.OLEDB.12.0;Data Source =" & TextBox3.Text)

        con.Open()
        Dim dt As DataTable = con.GetSchema("TABLES", {Nothing, Nothing, Nothing, "TABLE"})
        Dim int As Integer
        SchemaTable = con.GetOleDbSchemaTable(System.Data.OleDb.OleDbSchemaGuid.Tables, New Object() {Nothing, Nothing, Nothing, Nothing})
        For int = 0 To SchemaTable.Rows.Count - 1
            If SchemaTable.Rows(int)!TABLE_TYPE.ToString = "TABLE" Then
                'Add items to list box
                ListBox1.Items.Add(SchemaTable.Rows(int)!TABLE_NAME.ToString())
            End If
        Next

    End Sub

    Private Sub ListBox1_MouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles ListBox1.MouseClick
        TextBox4.Text = ListBox1.Text
        Dim query As String = "SELECT * FROM " & TextBox4.Text

        ' Create a DataAdapter to load the data.
        Dim data_adapter As New OleDbDataAdapter(query, dbconn)

        ' Create a DataTable.
        Dim data_table As New DataTable()
        Try
            data_adapter.Fill(data_table)
        Catch ex As Exception
            MessageBox.Show("Error executing query " & query & vbCrLf & ex.Message)
        End Try

        ' Bind the DataGridView to the DataTable.
        DataGridView1.DataSource = data_table
        DataGridView1.ReadOnly = True

    End Sub

vb.jpg
 
Last edited by a moderator:
This is a very poorly written question I'm afraid. There's way too much code there - no way all of that is relevant - and there's no clear explanation of the problem. There's also talk of an error but no indication of where it occurs and what the error message is. You need to provide a FULL and CLEAR explanation of the problem, i.e. exactly what you're trying to achieve, exactly how you're trying to achieve it (including RELEVANT code) and exactly what happens when you try (including error locations and messages).
 
i have getting error following code.
error massage:
database path show in textbox3 & table name show in textbox4.
database records show in datagridview1.
when i select table name from listbox then table name show in textbox4 but database records not showing in datagridview & show error massage.
eror.png



Dim dbconn As New OleDbConnection


Private Sub Form9_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
dbconn.ConnectionString = ("Provider=Microsoft.ACE.OLEDB.12.0; Data Source= " & TextBox3.Text)


Dim SchemaTable As DataTable
----------------------------------------------------------------------------------------------------------------
database path read from textfile and show in textbox3
Dim FILE_NAME As String = My.Application.Info.DirectoryPath.ToString() & "\INFO.TXT"

If System.IO.File.Exists(FILE_NAME) = True Then

Dim objReader As New System.IO.StreamReader(FILE_NAME)
TextBox3.Text = objReader.ReadToEnd
objReader.Close()

Else

MessageBox.Show("File Does Not Exist")

End If
-------------------------------------------------------------------------------------------------------------------
ListBox1.Items.Clear()


Dim con As New OleDb.OleDbConnection("PROVIDER=Microsoft.ACE.OLEDB.12.0;Data Source =" & TextBox3.Text)

con.Open()
Dim dt As DataTable = con.GetSchema("TABLES", {Nothing, Nothing, Nothing, "TABLE"})
Dim int As Integer
SchemaTable = con.GetOleDbSchemaTable(System.Data.OleDb.OleDbSchemaGuid.Tables, New Object() {Nothing, Nothing, Nothing, Nothing})
For int = 0 To SchemaTable.Rows.Count - 1
If SchemaTable.Rows(int)!TABLE_TYPE.ToString = "TABLE" Then
'Add items to list box
ListBox1.Items.Add(SchemaTable.Rows(int)!TABLE_NAME.ToString())
End If
Next

End Sub

Private Sub ListBox1_MouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles ListBox1.MouseClick
TextBox4.Text = ListBox1.Text
Dim query As String = "SELECT * FROM " & TextBox4.Text

' Create a DataAdapter to load the data.
Dim data_adapter As New OleDbDataAdapter(query, dbconn)

' Create a DataTable.
Dim data_table As New DataTable()
Try
data_adapter.Fill(data_table)
Catch ex As Exception
MessageBox.Show("Error executing query " & query & vbCrLf & ex.Message)
End Try

' Bind the DataGridView to the DataTable.
DataGridView1.DataSource = data_table
DataGridView1.ReadOnly = True

i also attached project file below
 

Attachments

  • WindowsApplication1.zip
    877.7 KB · Views: 22
Is this posted to the wrong subforum????? As far as I know, no version of .NET "Core" is supported on Windows Vista? Additionally, which version of Visual Studio? I'm not sure that what I'm seeing in the screen shots are a version of Visual Studio *new enough* to support launching a .NET "Core" application...
 
The attachment says WindowsApplication the solution file says VS 2010. I still have that, I'll take a crack LOL

Basically the dbconn isn't opened, but conn was created and used to get the list of tables. I removed dbconn and made conn the form level connection var.

This works:
    Dim conn As OleDbConnection

    Private Sub Form9_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'dbconn.ConnectionString = ("Provider=Microsoft.ACE.OLEDB.12.0; Data Source= " & TextBox3.Text)


        Dim SchemaTable As DataTable
        Dim FILE_NAME As String = My.Application.Info.DirectoryPath.ToString() & "\INFO.TXT"

        If System.IO.File.Exists(FILE_NAME) = True Then

            Dim objReader As New System.IO.StreamReader(FILE_NAME)
            TextBox3.Text = objReader.ReadToEnd
            objReader.Close()

        Else

            MessageBox.Show("File Does Not Exist")

        End If

        ListBox1.Items.Clear()


        conn = New OleDb.OleDbConnection("PROVIDER=Microsoft.ACE.OLEDB.12.0;Data Source =" & TextBox3.Text)

        conn.Open()
        Dim dt As DataTable = conn.GetSchema("TABLES", {Nothing, Nothing, Nothing, "TABLE"})
        Dim int As Integer
        SchemaTable = conn.GetOleDbSchemaTable(System.Data.OleDb.OleDbSchemaGuid.Tables, New Object() {Nothing, Nothing, Nothing, Nothing})
        For int = 0 To SchemaTable.Rows.Count - 1
            If SchemaTable.Rows(int)!TABLE_TYPE.ToString = "TABLE" Then
                'Add items to list box
                ListBox1.Items.Add(SchemaTable.Rows(int)!TABLE_NAME.ToString())
            End If
        Next

    End Sub

    Private Sub ListBox1_MouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles ListBox1.MouseClick
        TextBox4.Text = ListBox1.Text
        Dim query As String = "SELECT * FROM " & TextBox4.Text

        ' Create a DataAdapter to load the data.
        Dim data_adapter As New OleDbDataAdapter(query, conn)

        ' Create a DataTable.
        Dim data_table As New DataTable()
        Try
            data_adapter.Fill(data_table)
        Catch ex As Exception
            MessageBox.Show("Error executing query " & query & vbCrLf & ex.Message)
        End Try

        ' Bind the DataGridView to the DataTable.
        DataGridView1.DataSource = data_table
        DataGridView1.ReadOnly = True

    End Sub
 
Last edited:
Back
Top