vb.net insert query error

variable Itemtext is already declared. Why this error??

Try
        conn = New SqlConnection(connString)
        conn.Open()
        Dim i As Integer = 0
        Using cmd As New SqlCommand()
            For Each c In item
                cmd.Connection = conn
                cmd.CommandType = CommandType.Text
                cmd.CommandText = "INSERT INTO tblItems(Item,Numberof) Values (@Itemtext,@Numberof)"
                cmd.Parameters.AddWithValue("@Itemtext", c)
                cmd.Parameters.AddWithValue("@Numberof", num(i))
                cmd.ExecuteNonQuery()
                i = i + 1
            Next
        End Using

Answers


Because you're adding the parameter each time in the loop

   Using cmd As New SqlCommand() 
      cmd.Connection = conn 
      cmd.CommandType = CommandType.Text 
      cmd.CommandText = "INSERT INTO tblItems(Item,Numberof) Values (@Itemtext,@Numberof)" 
      Dim ItemParameter As SqlParameter = cmd.Parameters.Add("@Itemtext",SqlDbType.VarChar)
      Dim NumberParameter As SqlParameter  = cmd.Parameters.Add("@Numberof", SqlDbType.Int) 
      For Each c In item 
            ItemParameter.Value = c
            NumberParameter.Value = num(i)
            cmd.ExecuteNonQuery() 
            i = i + 1 
      Next 
    End Using 

Need Your Help

javascript form action changesrc method post text input onsubmit

javascript html forms

i'm just trying to post a form's text input into an iframe with a return, no submit button.

Winform and threading (C#)

c# winforms multithreading

I have a problem with winform application which uses threads to update the UI. My application does upload and downloading of files from cloud. Same time I am also displaying Network speed details o...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.