2017-05-24 5 views
0

sqlparameterコレクションを実装しようとしたときにこの例外が発生しました。例外は言う:ここasp.netを使用してsqlparameterコレクションを作成しようとした際に例外エラーが発生しました

An exception of type 'System.InvalidCastException' occurred in System.Data.dll but was not handled in user code Additional information: The SqlParameterCollection only accepts non-null SqlParameter type objects, not SqlParameter[] objects.

は、ソースのエラーです:

Server Error in '/' Application.

The SqlParameterCollection only accepts non-null SqlParameter type objects, not SqlParameter[] objects.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.InvalidCastException: The SqlParameterCollection only accepts non-null SqlParameter type objects, not SqlParameter[] objects.

Source Error:

Line 197: if (p != null) Line 198: if (p.Any()) Line 199: command.Parameters.Add(p); Line 200: } Line 201:

Source File: c:\Users\User1\Documents\Visual Studio
2015\WebSites\MusicStore\Pages\OverviewGuitarData.aspx.cs Line: 199

Stack Trace:

[InvalidCastException: The SqlParameterCollection only accepts non-null SqlParameter type objects, not SqlParameter[] objects.] System.Data.SqlClient.SqlParameterCollection.ValidateType(Object value) +5734517 System.Data.SqlClient.SqlParameterCollection.Add(Object value) +23 Pages_OverviewData.GetExample(SqlCommand command, SqlParameter[] p) in c:\Users\User1\Documents\Visual Studio 2015\WebSites\MusicStore\Pages\OverviewGuitarData.aspx.cs:199 Pages_OverviewData.GuitarBrandsGridView_RowUpdating(Object sender, GridViewUpdateEventArgs e) in c:\Users\User1\Documents\Visual Studio 2015\WebSites\MusicStore\Pages\OverviewGuitarData.aspx.cs:157 System.Web.UI.WebControls.GridView.OnRowUpdating(GridViewUpdateEventArgs e) +122 System.Web.UI.WebControls.GridView.HandleUpdate(GridViewRow row, Int32 rowIndex, Boolean causesValidation) +792 System.Web.UI.WebControls.GridView.HandleEvent(EventArgs e, Boolean causesValidation, String validationGroup) +877 System.Web.UI.WebControls.GridView.OnBubbleEvent(Object source, EventArgs e) +89 System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args) +37 System.Web.UI.WebControls.GridViewRow.OnBubbleEvent(Object source, EventArgs e) +90 System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args) +37 System.Web.UI.WebControls.Button.OnCommand(CommandEventArgs e) +114 System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +260 System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler. RaisePostBackEvent(String eventArgument) +12 System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +15 System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +35 System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1639

そして、ここでsqlparameterが発祥私のコードの一部:

using (SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["brandsConnection"].ToString())) { 
     string query = "UPDATE [guitarBrands] SET type = @type, name = @name, image = @image WHERE id = @id"; 
     SqlParameter[] p = new SqlParameter[4]; 
     p[0] = new SqlParameter("type", newType.Text); 
     p[1] = new SqlParameter("name", newName.Text); 
     p[2] = new SqlParameter("image", newImage.Text); 
     p[3] = new SqlParameter("id", id); 

     connection.Open(); 
     using (SqlCommand command = new SqlCommand(query, connection)) 
     { 
      GetExample(command, p); 
      command.ExecuteNonQuery(); 
      connection.Close(); 
      command.Parameters.Clear(); 
     } 
    } 

public void GetExample(SqlCommand command, params SqlParameter[] p) 
{ 
    if (p != null) 
     if (p.Any()) 
      command.Parameters.Add(p);//error is pointing here 
} 
+0

つ以上SqlParameter'が 'null'です – VDWWD

+0

[NullReferenceExceptionとは何ですか?それを修正するにはどうすればいいですか?](https://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do -i-fix-it) – VDWWD

+0

あなたのクエリは '@'のパラメータを必要とし、 '@'のパラメータを送信しています – Ravi

答えて

0

私はあなたが本当にAddRangeを意味推測:

以外にも
if (p != null && p.Any()) 
    command.Parameters.AddRange(p); 

、コメンターが指摘したように、あなたのパラメータ名は、クエリに対応していることを確認してください。

あなたの `の
p[0] = new SqlParameter("@type", newType.Text); 
+0

最後の点で同意します – Ravi

+0

@Andrei - このソリューションは正しいです。また、私のコードに基づいて、このコードを改善するためにどのようなテクニックをお勧めしますか? – RockStar

+0

@RockStar、まあ、私が示唆することができるいくつかの愚痴があります。しかし、実際これはコードレビュー – Andrei

0

問題はアドオンであります。代わりにAddRangeを使用する必要があります。

command.Parameters.AddRange(p); 
関連する問題