Results 1 to 4 of 4

Thread: [RESOLVED] [2005] SQL Update Question - with List variable

  1. #1

    Thread Starter
    Frenzied Member stimbo's Avatar
    Join Date
    Jun 2006
    Location
    UK
    Posts
    1,739

    Resolved [RESOLVED] [2005] SQL Update Question - with List variable

    I have a list(of String) variable that has 150 items in it. I basically just want to add those values to a column in my table and was wondering how to do it?

    Here's the relevant code. Obviously this doesn't work and from the error message I got regarding parameter already existing (on the 2nd loop no doubt) I can see why. I've just included it to demonstrate what I want. Everything is connecting okay etc... so it's just the best way to add the range. I saw the Parameters.AddRange but can't find a decent example of what it's about so don't know if that's relevant.

    vb Code:
    1. Dim cmd As String = "INSERT INTO Pathology (PathologyValue) VALUES (@Path)"
    2.         Dim insrt As New SqlCommand(cmd, conn)
    3.  
    4.         Try
    5.             conn.Open()
    6.  
    7.             For i As Integer = 0 To myList.Count - 1
    8.  
    9.                 insrt.Parameters.AddWithValue("@path", myList(i))
    10.  
    11.                 insrt.ExecuteNonQuery()
    12.  
    13.             Next
    14.  
    15.             conn.Close()
    16.  
    17.         Catch ex As SqlException
    18.             MessageBox.Show(ex.Message)
    19.         Catch ex As Exception
    20.             MessageBox.Show(ex.Message)
    21.         End Try
    Stim

    Free VB.NET Book Chapter
    Visual Basic 2005 Cookbook Sample Chapter

  2. #2
    Frenzied Member bmahler's Avatar
    Join Date
    Oct 2005
    Location
    Somewhere just west of the Atlantic
    Posts
    1,568

    Re: [2005] SQL Update Question - with List variable

    Try something like this

    vb.net Code:
    1. Dim insrt As SqlCommand
    2.         Try
    3.             For i As Integer = 0 To myList.Count - 1
    4.                 insrt = New SqlCommand(cmd, conn)
    5.                 conn.Open()
    6.                 insrt.Parameters.AddWithValue("@path", myList(i))
    7.                 insrt.ExecuteNonQuery()
    8.                 conn.Close()
    9.                 insrt = Nothing
    10.             Next
    Last edited by bmahler; May 11th, 2007 at 08:24 AM.
    Boooya
    • Visual Studio 2008 Professional
    • Don't forget to use [CODE]your code here[/CODE] when posting code
    • Don't forget to rate helpful posts!
    • If you're question was answered please mark your thread [Resolved]


    Code Contributions:
    PHP
    PHP Image Gallery v1.0PHP Image Gallery v2.0
    VB 2005
    Find Computers on a networkSimple License EncryptionSQL Server Database Access dllUse Reflection to Return Crystal ReportDocumentSilently Print PDFGeneric Xml Serailizer


    Useful Links: (more to come)
    MSDN (The first and foremost)MSDN Design Guidelines API Reference • Inno Setup CompilerInno Setup PreprocessorISTool - Fairly easy to use GUI for creating inno setup projects • Connection StringsNAnt -Automated BuildsCruise Control .NET - Frontend for automated builds

  3. #3

    Thread Starter
    Frenzied Member stimbo's Avatar
    Join Date
    Jun 2006
    Location
    UK
    Posts
    1,739

    Re: [2005] SQL Update Question - with List variable

    That's just a type0, here's the error message I get:

    The variable name '@Path' has already been declared. Variable names must be unique within a query batch or stored procedure.
    I took that to mean I can't use the Parameter like that, i.e. dupicate 'declaration' or addition of it in the same loop..

    Taking out the For loop as a test and just having it add one item to the DB works fine (as you would expect):

    vb Code:
    1. insrt.Parameters.AddWithValue("@Path", myList(0))
    Stim

    Free VB.NET Book Chapter
    Visual Basic 2005 Cookbook Sample Chapter

  4. #4

    Thread Starter
    Frenzied Member stimbo's Avatar
    Join Date
    Jun 2006
    Location
    UK
    Posts
    1,739

    Re: [2005] SQL Update Question - with List variable

    That's done the trick. It makes sense too. I should have thought of it.

    Thanks very much.
    Stim

    Free VB.NET Book Chapter
    Visual Basic 2005 Cookbook Sample Chapter

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width