2016-02-25 19 views
0

使用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; 
         } 
        } 
       } 
      } 
     } 
    } 
} 
+0

認真我這個昨晚看了,但沒有時間然後編輯它,但它現在已經在這裏17個小時了,而不是一個人想要刪除[tag:asp-classic]。這很明顯是一個標記爲[標籤:asp.net]的問題,如果你不知道,那麼你不應該回答這個問題。 – Lankymart

回答

3

在GetData方法, '使用' 的內部的SqlDataAdapter,嘗試以下方法:

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

的問題是,cmd.Parameters [「@ PN 「]在設置其值時不存在,因此您會得到異常。

+0

非常感謝您的幫助。這似乎解決了我的問題。我是C#的新手,並且從未猜到過它。我認爲調用變量的函數是如何的,這是錯誤的路徑。 – Nicho247

2

您需要的帕拉姆添加給SqlCommand

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

你也不需要在你的btnTemp_Click方法驗證碼

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

也謝謝你的幫助。 SashaDu第一名,所以她得到了答案。我認爲你的回答也很有幫助。 – Nicho247

+0

只有剛剛注意到其他人的回答。很高興你把事情解決了。 – Jay