Solved

vb.net SQL statement parameters using IBM.Data.DB2.iSeries to AS400

Posted on 2007-11-14
2
3,189 Views
Last Modified: 2013-11-26
Here is my code:

 Dim cn As iDB2Connection = New iDB2Connection("User ID=yeah;Password=yeah;Data Source=10.1.1.999;Connection Timeout = 0")
        Dim cmd As iDB2Command
        Dim sqlstring As String        

                sqlstring = "insert into weslib2.wp1401p (saacct, sanam1, sanam2, saadd1, sacity) values (@Name, @Address, @City, @State, @Zip)"

        cmd = New iDB2Command(sqlstring, cn)
        'cmd.Prepare()

        Dim da As New iDB2DataAdapter(sqlstring, cn)
     
        'Open connection
        cn.Open()
        cmd.Parameters("@Name").Value() = txtName.Text
        cmd.Parameters("@Address").Value() = txtAddress.Text
        cmd.Parameters("@city").Value() = txtCity.Text
        cmd.Parameters("@state").Value() = txtState.Text
        cmd.Parameters("@Zip").Value() = txtZip.Text



        cmd.ExecuteNonQuery()

        cn.Close()
        TextBox1.Text = sqlstring

When I run this I get error:
Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index

Any ideas on it?
0
Comment
Question by:bergertime
2 Comments
 
LVL 23

Accepted Solution

by:
Ashish Patel earned 500 total points
ID: 20282917
Try this.
Dim cn As iDB2Connection = New iDB2Connection("User ID=yeah;Password=yeah;Data Source=10.1.1.999;Connection Timeout = 0")
        Dim cmd As iDB2Command
        Dim sqlstring As String        

                sqlstring = "insert into weslib2.wp1401p (saacct, sanam1, sanam2, saadd1, sacity) values (@Name, @Address, @City, @State, @Zip)"

        cmd = New iDB2Command(sqlstring, cn)

        Dim da As New iDB2DataAdapter(sqlstring, cn)
With cmd.Parameters
            .Add(New SqlParameter("@Name", txtName.Text))
            .Add(New SqlParameter("@Address", txtAddress.Text))
            .Add(New SqlParameter("@City", txtCity.Text))
            .Add(New SqlParameter("@State", txtState.Text))
            .Add(New SqlParameter("@Zip", txtZip.Text))
        End With
        'Open connection
        cn.Open()
        cmd.ExecuteNonQuery()
        cn.Close()
        TextBox1.Text = sqlstring
0
 
LVL 3

Expert Comment

by:WaldenL
ID: 20283285
First, you've commented out the prepare, so the parameter collection will be empty. Second, I don't think you can have () at the end of  .Value, it's a property not a method. Actually I'm suprised that complies, as you can't assigned to a method, maybe VB ignores the ()?
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Suggested Solutions

Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/A_4334-Minify-and-Concatenate-Your-Scripts-and-Stylesheets.html)…
This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
This video discusses moving either the default database or any database to a new volume.
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now