tutorials.de Buch-Aktion 05/2012
ERLEDIGT
JA
ANTWORTEN
2
ZUGRIFFE
718
EMPFEHLEN
  • An Twitter übertragen
  • An Facebook übertragen
AUF DIESES THEMA
ANTWORTEN
  1. #1
    Registriert seit
    Feb 2005
    Ort
    Bad Dürkheim
    Beiträge
    5
    Hallo zusammen,
    ich versuche, mir Visual Basic mit Visual Studio 2005 anzueignen. Ich habe mir eine Testtabelle im SqlServer 2005 angelegt. Nun will ich Daten mit sqlReader auslesen und anzeigen. Einzelne Werte mit Scalar funktionieren.
    Code :
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    
    [SIZE=2][COLOR=#008000][SIZE=2][COLOR=#008000]'Parametrisierte Abfrage der Testtabelle[/COLOR][/SIZE]
    [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]Private[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] Button2_Click([/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] sender [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] System.Object, [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] e [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] System.EventArgs) [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]Handles[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] Button2.Click[/SIZE]
    [SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] con [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] SqlConnection = GetConnection()[/SIZE]
    [SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] sql [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]String[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] = [/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]"select NID,Name,Passwort from dbo.tbl_01 where Gruppe = @Gruppe"[/COLOR][/SIZE]
    [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] cmd [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] SqlCommand = [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] SqlCommand(sql, con)[/SIZE]
    [SIZE=2][COLOR=#008000][SIZE=2][COLOR=#008000]'Parameter deffinieren[/COLOR][/SIZE]
    [/COLOR][/SIZE][SIZE=2]cmd.Parameters.Add([/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]"@NID"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2], SqlDbType.Int, 7)[/SIZE]
    [SIZE=2]cmd.Parameters.Add([/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]"@Name"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2], SqlDbType.NChar, 20)[/SIZE]
    [SIZE=2]cmd.Parameters.Add([/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]"@Passwort"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2], SqlDbType.NChar, 7)[/SIZE]
    [SIZE=2]cmd.Parameters.Add([/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]"@Gruppe"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2], SqlDbType.NChar, 10)[/SIZE]
    [SIZE=2][COLOR=#008000][SIZE=2][COLOR=#008000]'Parameter füllen[/COLOR][/SIZE]
    [/COLOR][/SIZE][SIZE=2]cmd.Parameters([/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]"@Gruppe"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]).Value = [/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]"FA"[/COLOR][/SIZE]
    [/COLOR][/SIZE][SIZE=2]cmd.Parameters([/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]"@NID"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]).Value = [/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]"NID"[/COLOR][/SIZE]
    [/COLOR][/SIZE][SIZE=2]cmd.Parameters([/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]"@Name"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]).Value = [/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]"Name"[/COLOR][/SIZE]
    [/COLOR][/SIZE][SIZE=2]cmd.Parameters([/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]"@Passwort"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]).Value = [/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]"Passwort"[/COLOR][/SIZE]
    [/COLOR][/SIZE][SIZE=2][COLOR=#008000][SIZE=2][COLOR=#008000]'Komando ausführen[/COLOR][/SIZE]
    [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] sdr [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] SqlDataReader = cmd.ExecuteReader[/SIZE]
    [SIZE=2][COLOR=#008000][SIZE=2][COLOR=#008000]'Datensätze anzeigen[/COLOR][/SIZE]
    [/COLOR][/SIZE][SIZE=2][COLOR=#008000][SIZE=2][COLOR=#008000]'Do While (sdr.Read())[/COLOR][/SIZE]
    [/COLOR][/SIZE][SIZE=2][COLOR=#008000][SIZE=2][COLOR=#008000]'Loop[/COLOR][/SIZE]
    [/COLOR][/SIZE][SIZE=2]sdr.Close()[/SIZE]
    [SIZE=2]con.Close()[/SIZE]
     
     
    [SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE]
    [/COLOR][/SIZE]

    Was ist an meinem Code falsch, da ich folgende Fehlermeldung bekomme:
    Programmzeile
    Dim sdr As SqlDataReader = cmd.ExecuteReader wird gelbhinterlegt
    Meldung: Konvertierung des Parameterwertes von String in Int32 fehlgeschlagen.

    Außerdem, wie zeige ich die ausgelesenen Werte in 3 Textboxen an, nicht mit den vorgefertigten MS-Teilen sonder per Code.

    Für Eure Hilfe sage ich jetzt schon mal Danke.

    Gruß Peter
     

  2. #2
    Avatar von Nico Graichen
    Nico Graichen Nico Graichen ist offline aka gemballa
    tutorials.de Moderator
    Registriert seit
    Dec 2003
    Ort
    Pulheim (NRW)
    Beiträge
    3.898
    Blog-Einträge
    34
    Hi

    Du legst beim Erstellen der Parameter fest, dass @NID vom Typ Integer ist.
    Code vbnet:
    1
    
    cmd.Parameters("@NID").Value = "NID"
    hier übergibst du jedoch einen String.

    Edit:
    Was wahrscheinlich noch zu einem Fehler führen wird:
    Dein Command hat nur einen Parameter, du fügst jedoch 4 hinzu.
    Geändert von Nico Graichen (12.09.08 um 20:59 Uhr) Grund: Nachtrag
     
    Grüße Nico
    ----------------------
    Xing
    ----------------------
    Zitat Zitat von Mark Twain (1835-1910)
    Es gibt drei Dinge, die eine Frau aus dem Nichts hervorzaubern kann: einen Hut, einen Salat und einen Ehekrach.
    Zitat Zitat von Mike Wilson - Biographie über Larry Ellison (CEO Oracle)
    The Difference Between God and Larry Ellison: God Doesn't Think He's Larry Ellison

  3. #3
    Registriert seit
    Feb 2005
    Ort
    Bad Dürkheim
    Beiträge
    5
    Danke Nico!
    Tip war okay und ich konnte weitermachen und jetzt gehts.
    Die Anzeige klappt auch fast.
    Gruß Peter
     

Ähnliche Themen

  1. SQLDataReader Cursor? Zeilenanzahl?
    Von Steve222 im Forum .NET Datenverwaltung
    Antworten: 1
    Letzter Beitrag: 23.11.07, 10:59
  2. [C#]SQLDataReader / Zeilenumbruch unter ASP.Net
    Von Noxxxon im Forum .NET Web und Kommunikation
    Antworten: 1
    Letzter Beitrag: 09.10.07, 11:13
  3. Anfängerproblem
    Von VirusX im Forum VisualStudio & MFC
    Antworten: 1
    Letzter Beitrag: 10.01.07, 11:02
  4. Anfängerproblem
    Von pgd im Forum Cinema 4D
    Antworten: 12
    Letzter Beitrag: 30.01.06, 15:22
  5. SqlDatareader und Transaktionen mit einer connection
    Von harlequinn im Forum .NET Archiv
    Antworten: 1
    Letzter Beitrag: 04.05.05, 15:45