使用C#& ASP.net,我有一個母版頁和一個內容頁。在內容頁面上,我有一個文本框,用戶可以在其中輸入一個值,然後點擊按鈕。該按鈕運行存儲的SQL過程,並將用戶輸入用作參數,然後將結果放入html表中進行顯示。System.IndexOutOfRangeException由於參數和變量
我的網頁將會啓動,但在用戶輸入按下按鈕後會拋出錯誤。我認爲我的問題與參數(@PN
),參數變量(param
),用戶輸入變量(inputX
)有關。錯誤消息讀取
System.IndexOutOfRangeException是未處理由用戶代碼
消息=與不是由本 包含SqlParameterCollection參數名稱「@PN」的的SqlParameter。
C#代碼如下
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;
}
}
}
}
}
}
}
認真我這個昨晚看了,但沒有時間然後編輯它,但它現在已經在這裏17個小時了,而不是一個人想要刪除[tag:asp-classic]。這很明顯是一個標記爲[標籤:asp.net]的問題,如果你不知道,那麼你不應該回答這個問題。 – Lankymart