Vb +sql server database

Fermé
Signaler
Messages postés
113
Date d'inscription
mercredi 5 mars 2014
Statut
Membre
Dernière intervention
7 février 2016
-
bonsoir,

j'ai crée un projet visual basic et une base de données .je tente d'insérer des données à cette base .voici mon code

Imports System.Data.SqlClient

Public Class Form1
    Dim cn As New SqlConnection("Data Source=C:\Users\DELL\AppData\Local\Temporary Projects\WindowsApplication1\Database1.sdf")
    Dim cmd As New SqlCommand
    Dim dr As SqlDataReader

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        cmd.Connection = cn
        loadlistbox()



    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        If TextBox1.Text <> "" And TextBox2.Text <> "" Then
            Try
                cn.Open()
            Catch ex As Exception
                MessageBox.Show("Failed to connect to data source")


                cmd.CommandText = "insert into info (nom,id) values ( TextBox1.Text,TextBox2.Text )  "
                cmd.ExecuteNonQuery()

                cn.Close()
            End Try
            TextBox1.Text = ""
            TextBox2.Text = ""
            loadlistbox()
        End If
    End Sub
    Private Sub loadlistbox()
        ListBox1.Items.Clear()
        ListBox1.Items.Clear()
        Try
            cn.Open()
        Catch ex As Exception
            MessageBox.Show("Failed to connect to data source")


            cmd.CommandText = " select nom , id  from info "
            dr = cmd.ExecuteReader()
            If dr.HasRows Then
                While (dr.Read())
                    ListBox1.Items.Add(dr("nom"))
                    ListBox2.Items.Add(dr(1))

                End While

            End If
            cn.Close()
        End Try



    End Sub
    Private Sub ListBox2_MouseClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles ListBox2.MouseClick, ListBox1.MouseClick
        Dim lb As New ListBox
        lb = sender
        If lb.SelectedIndex <> -1 Then
            ListBox1.SelectedIndex = lb.SelectedIndex
            ListBox2.SelectedIndex = lb.SelectedIndex



        End If
    End Sub
End Class


Lorsque j'exécute le prog se bloque au niveau de la 2 eme cn.Open()
Pouvez-vous m'aider SVP

EDIT: Spécification du langage dans la coloration syntaxique.