none
get erroes from cn.Open() RRS feed

  • Question

  • Imports System.Data.OleDb
    Imports System.IO

    Public Class Form1
        Dim cn As New OleDbConnection
        Dim cmd As New OleDbCommand
        Dim da As New OleDbDataAdapter
        Dim ds As New DataSet
        Dim dt As New DataTable
        Dim i As Integer

        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            showdata()
        End Sub

        Private Sub showdata()

            cn.Open()
            DataGridView1.RowTemplate.Height = 100
            With cmd
                .Connection = cn
                .CommandText = "SELECT * FROM product"
            End With
            da.SelectCommand = cmd
            dt.Clear()
            da.Fill(dt)
            DataGridView1.DataSource = dt
            cn.Close()
            For i As Integer = 0 To DataGridView1.Columns.Count - 1
                If TypeOf DataGridView1.Columns(i) Is DataGridViewImageColumn Then
                    DirectCast(DataGridView1.Columns(i), DataGridViewImageColumn).ImageLayout = DataGridViewImageCellLayout.Stretch
                End If
            Next
        End Sub

    End Class

    errors La propriété ConnectionString n'a pas été initialisée.

    ========  cn.Open()

    Sunday, February 21, 2016 9:44 PM

Answers

  • Hi Ianouar3G,

    Based on your code snippet, it seems that you miss connection string which caused the issue. Try to modify you code snippet like below and check if it works for you.

    Imports System.Data.OleDb
    Imports System.IO
    
    Public Class Form1
    
        Dim connectionString = “your oledbconnection connection string”
        Dim cn As New OleDbConnection(connectionString)
        Dim cmd As New OleDbCommand
        Dim da As New OleDbDataAdapter
        Dim ds As New DataSet
        Dim dt As New DataTable
        Dim i As Integer
    
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            showdata()
        End Sub
    
        Private Sub showdata()
    
            cn.Open()
            DataGridView1.RowTemplate.Height = 100
            With cmd
                .Connection = cn
                .CommandText = "SELECT * FROM product"
            End With
            da.SelectCommand = cmd
            dt.Clear()
            da.Fill(dt)
            DataGridView1.DataSource = dt
            cn.Close()
            For i As Integer = 0 To DataGridView1.Columns.Count - 1
                If TypeOf DataGridView1.Columns(i) Is DataGridViewImageColumn Then
                    DirectCast(DataGridView1.Columns(i), DataGridViewImageColumn).ImageLayout = DataGridViewImageCellLayout.Stretch
                End If
            Next
        End Sub
    
    End Class
    

    Best regards,

    Cole Wu

    Monday, February 22, 2016 7:55 AM
    Moderator