Dataset et SqlDataAdapter

Résolu
dp_favresa Messages postés 132 Date d'inscription vendredi 23 juillet 2004 Statut Membre Dernière intervention 11 mai 2010 - 28 nov. 2005 à 10:40
dp_favresa Messages postés 132 Date d'inscription vendredi 23 juillet 2004 Statut Membre Dernière intervention 11 mai 2010 - 28 nov. 2005 à 13:58
Bonjour,



Je commence l'apprentissage de VB.net avec Visual Studio Express 2005
et je désire créer une petite appli d'interrogation de notre base SQL,
le résultat des requêtes est chargé dans un DataGridView.

Dans le 1er cas, je choisis une date par un DataTimePicker, tout se passe bien.

Dans le 2ème cas, je remplis 2 TextBox et active le prog par un bouton, le prog se plante avec l'erreur :






Voici le code utilisé (inspiré de ce que j'ai trouvé ici) :

Imports System

Imports System.Data

Imports System.Data.SqlClient

Public Class Form1

Inherits System.Windows.Forms.Form



Public conn As SqlConnection

Public CmdS As SqlCommand

Public da As SqlDataAdapter

Public ds As New DataSet()

Public strSql As String

Public strConn As String = "Initial Catalog=FAVRE;Data Source=Serveur-corc;User ID=sa;Password=;"

Public Poids As Integer

Public DateForm As String

Public JJ As String

Public MM As String

Public AA As String

Public NomCol(6) As String

Public i As Short

Public Dossier As String



Private Sub
DateTimePicker1_ValueChanged(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles
DateTimePicker1.ValueChanged

Dim Annee As String

NomCol(0) = "Dossier"

NomCol(1) = "N° client"

NomCol(2) = "Séquence"

NomCol(3) = "N° liste ingénieur"

NomCol(4) = "Date"

NomCol(5) = "Poids (kg)"



Annee = DateTimePicker1.Value

JJ = Annee.Substring(0, 2)

MM = Annee.Substring(3, 2)

AA = Annee.Substring(6, 4)

Annee = AA & MM & JJ



strSql = "SELECT ESRC_FILE,
RC_NUM, PS_CODE, PS_TITLE, DELIVASKED, INVWEIGHT FROM
REF_PS WHERE DELIVASKED = " &
Annee



Connexion(strConn, strSql)

MiseForme()

End Sub



Private Sub Connexion(ByVal strConn As String, ByVal strSql As String)

conn = New SqlConnection(strConn)

conn.Open()

CmdS = New SqlCommand(strSql)

da = New SqlDataAdapter(CmdS)

CmdS.Connection() = conn

da.Fill(ds, "REF_PS")

End Sub



Private Sub MiseForme()

Poids = 0

With ds.Tables("REF_PS")

For i = 0 To .Rows.Count - 1


DateForm = .Rows(i).Item("DELIVASKED")

JJ = DateForm.Substring(6, 2)

MM = DateForm.Substring(4, 2)

AA = DateForm.Substring(0, 4)


DateForm = JJ & "/" & MM & "/" & AA


.Rows(i).Item("DELIVASKED") = DateForm


Poids = Poids + .Rows(i).Item("INVWEIGHT")

Next

End With



With ds.Tables("REF_PS")

For i = 0 To .Columns.Count - 1

.Columns(i).ColumnName = NomCol(i)

Next

End With



Dim Vue As New DataView(ds.Tables("REF_PS"))

DataGridView1.DataSource = Vue

Poids = Poids / 1000

TextBox4.Text = Poids

End Sub



Private Sub Button1_Click(ByVal sender As
System.Object, ByVal e As System.EventArgs) Handles

Button1.Click

Dossier = "CHT" & TextBox1.Text



strSql = "SELECT ESRC_FILE,
RC_NUM, PS_CODE, PS_TITLE, DELIVASKED, INVWEIGHT FROM
REF_PS WHERE ESRC_FILE = " &
Dossier & " AND PS_TITLE LIKE " & TextBox2.Text



Connexion(strConn, strSql)

MiseForme()

End Sub

End Class


Merci d'avance a qui peut me dépanner.



Bon(nes) courage, développement, salutations...
(choisissez ce qui vous convient) dp

2 réponses

olixelle Messages postés 506 Date d'inscription vendredi 30 juillet 2004 Statut Membre Dernière intervention 3 mars 2008 2
28 nov. 2005 à 13:20
il maque les ' autour de dossier et autour de testbox2:
strSql "SELECT ESRC_FILE, RC_NUM, PS_CODE, PS_TITLE, DELIVASKED, INVWEIGHT FROM REF_PS WHERE ESRC_FILE '" & Dossier & "' AND PS_TITLE LIKE '" & TextBox2.Text & "'"

Rollerman
3
dp_favresa Messages postés 132 Date d'inscription vendredi 23 juillet 2004 Statut Membre Dernière intervention 11 mai 2010
28 nov. 2005 à 13:58
Bieeeeeeeeeeeeeeeen sûr !!!

J'ai pourtant déjà fait des requêtes SQL.

Encore merci.

Bon(nes) courage, développement, salutations...
(choisissez ce qui vous convient) dp
0
Rejoignez-nous