Reputation: 87
i had this exception problem when i tried out implementing an sqlparameter collection. the exception says:
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.
Here is also the source error:
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: 199Stack 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
And here a part of my code where the sqlparameter originated:
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
}
Upvotes: 0
Views: 1115
Reputation: 56688
I guess you really meant AddRange:
if (p != null && p.Any())
command.Parameters.AddRange(p);
Besides, as commenters pointed out, make sure your parameter names correspond to your query:
p[0] = new SqlParameter("@type", newType.Text);
Upvotes: 0
Reputation: 2896
The problem is in the add. You must use AddRange instead.
command.Parameters.AddRange(p);
Upvotes: 0