Nicho247
Nicho247

Reputation: 212

System.IndexOutOfRangeException due to parameter & variables

With C# & ASP.net, I have a master page and a content page. On a content page, I have a text box, where the user can input a value and then hit a button. This button runs a stored SQL procedure, and uses the user input as a parameter, then puts the results into an html table to display.

My webpage will launch, but the error is thrown after hitting the button with a user input. I think my issue is related to the parameter (@PN), the parameter variable (param), user input to variable (inputX). Error message reads

System.IndexOutOfRangeException was unhandled by user code
Message=An SqlParameter with ParameterName '@PN' is not contained by this SqlParameterCollection.

C# Code as follows

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Windows.Forms;
using System.Text;
using System.Configuration;

namespace NSV3
{
    public partial class Fancy : System.Web.UI.Page
    {
        private string inputX = string.Empty;

        protected void Page_Load(object sender, EventArgs e)
        {
            Label1.Attributes.Add("onClick", "CallMe();");            
        }

        public void btnTemp_Click(object sender, EventArgs e)
        {
            string inputX = TextBox1.Text.ToString();
            Label2.Text = "You've selected " + inputX;

            SqlParameter param = new SqlParameter();
            param.ParameterName = "@PN";
            param.Value = inputX;


            DataTable dt = this.GetData();
            StringBuilder html = new StringBuilder();
            html.Append("<table border = '1'>");
            html.Append("<tr>");

            foreach (DataColumn column in dt.Columns)
            {
                html.Append("<th>");
                html.Append(column.ColumnName);
                html.Append("</th>");
            }
            html.Append("</tr>");

            foreach (DataRow row in dt.Rows)
            {
                html.Append("<tr>");
                foreach (DataColumn column in dt.Columns)
                {
                    html.Append("<td>");
                    html.Append(row[column.ColumnName]);
                    html.Append("</td>");
                }
                html.Append("</tr>");
            }
            html.Append("</table>");

            PlaceHolder1.Controls.Add(new Literal { Text = html.ToString() });
        }


        public DataTable GetData()
        {
            string constr = "editted";
            string sql = @"EXEC     [ERP_Reports].[dbo].[udsp_WhereUsed]
                                    @IMA_ItemID_INPUT = @PN,
                                    @IMA_RecordID_INPUT = NULL,
                                    @RecursionLevel = NULL,
                                    @FetchSequence = NULL,
                                    @QtyPerAssy_PrevLevel = NULL";

            using (SqlConnection con = new SqlConnection(constr))
            {
                using (SqlCommand cmd = new SqlCommand(sql))
                {
                    using (SqlDataAdapter sda = new SqlDataAdapter())
                    {
                        cmd.Connection = con;
                        cmd.Parameters["@PN"].Value = inputX;
                        sda.SelectCommand = cmd;
                        using (DataTable dt = new DataTable())
                        {
                            sda.Fill(dt);
                            return dt;
                        }
                    }
                }
            }
        }
    }
}

Upvotes: 0

Views: 1171

Answers (2)

Jay
Jay

Reputation: 2129

You need to add the Param to the sqlCommand

cmd.Parameters.AddWithValue("@PN", inputx);

You also don't need this code in your btnTemp_Click method

SqlParameter param = new SqlParameter();
param.ParameterName = "@PN";
param.Value = inputX;

Upvotes: 2

SashaDu
SashaDu

Reputation: 376

In GetData method, inside the SqlDataAdapter 'using', try the following:

...                        
cmd.Connection = con;
cmd.Parameters.AddWithValue("@PN", inputX);
sda.SelectCommand = cmd;
...

The problem is that cmd.Parameters["@PN"] does not exist when you set its Value so you get the exception.

Upvotes: 3

Related Questions