Why am I getting an error?

Hi Experts,

Why am I getting this error?

System.IndexOutOfRangeException was unhandled by user code
  Message="An SqlParameter with ParameterName 'CostCategory' is not contained by this SqlParameterCollection."
  Source="System.Data"
  StackTrace:
       at System.Data.SqlClient.SqlParameterCollection.GetParameter(String parameterName)
       at System.Data.SqlClient.SqlParameterCollection.get_Item(String parameterName)
       at ASP.managecostcategory_aspx.CostCategoryFormView_ItemInserting(Object sender, FormViewInsertEventArgs e) in C:\Documents and Settings\RXNg\My Documents\Visual Studio 2005\WebSites\Logistics_Budget_Manager\ManageCostCategory.aspx:line 21
       at System.Web.UI.WebControls.FormView.OnItemInserting(FormViewInsertEventArgs e)
       at System.Web.UI.WebControls.FormView.HandleInsert(String commandArg, Boolean causesValidation)
       at System.Web.UI.WebControls.FormView.HandleEvent(EventArgs e, Boolean causesValidation, String validationGroup)
       at System.Web.UI.WebControls.FormView.OnBubbleEvent(Object source, EventArgs e)
       at System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args)
       at System.Web.UI.WebControls.FormViewRow.OnBubbleEvent(Object source, EventArgs e)
       at System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args)
       at System.Web.UI.WebControls.LinkButton.OnCommand(CommandEventArgs e)
       at System.Web.UI.WebControls.LinkButton.RaisePostBackEvent(String eventArgument)
       at System.Web.UI.WebControls.LinkButton.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument)
       at System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument)
       at System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData)
       at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)


THANKS

Protected Sub CostCategoryFormView_ItemInserting(ByVal sender As Object, ByVal e As FormViewInsertEventArgs)
        
        Dim strCostCategory As String
        
        strCostCategory = e.Values("CostCategory")
        
        Dim conn As New System.Data.SqlClient.SqlConnection("Data Source=.\SQLEXPRESS;AttachDbFilename='C:\Documents and Settings\RXNg\My Documents\Visual Studio 2005\WebSites\Logistics_Budget_Manager\App_Data\Logistics_Budget_Manager.mdf';Integrated Security=True;User Instance=True")
        Dim da As New System.Data.SqlClient.SqlDataAdapter("SELECT * FROM [Cost Category] WHERE [Cost Category].CostCategory=@CostCategory", conn)
        da.SelectCommand.Parameters("CostCategory").Value = strCostCategory
        Dim ds As New Data.DataSet()
        
        da.Fill(ds)
        If ds.Tables(0).Rows.Count > 0 Then
            e.Cancel = True
        End If
        
        MsgBox(e.Values("CostCategory"))
        
    End Sub

Open in new window

LVL 1
indyngAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Guy Hengel [angelIII / a3]Billing EngineerCommented:
does this work any better:
da.SelectCommand.Parameters("@CostCategory").Value = strCostCategory 
or this: 
da.SelectCommand.Parameters.AddWithValue("@CostCategory", strCostCategory)

Open in new window

0
indyngAuthor Commented:
Now I have this but still getting the same errror:

An SqlParameter with ParameterName '@CostCategory' is not contained by this SqlParameterCollection.
    Protected Sub CostCategoryFormView_ItemInserting(ByVal sender As Object, ByVal e As FormViewInsertEventArgs)
        
        Dim strCostCategory As String
        
        strCostCategory = e.Values("CostCategory")
        
        Dim conn As New System.Data.SqlClient.SqlConnection("Data Source=.\SQLEXPRESS;AttachDbFilename='C:\Documents and Settings\RXNg\My Documents\Visual Studio 2005\WebSites\Logistics_Budget_Manager\App_Data\Logistics_Budget_Manager.mdf';Integrated Security=True;User Instance=True")
        Dim da As New System.Data.SqlClient.SqlDataAdapter("SELECT * FROM [Cost Category] WHERE [Cost Category].CostCategory=@CostCategory", conn)
        da.SelectCommand.Parameters("@CostCategory").Value = strCostCategory
        Dim ds As New Data.DataSet()
        
        da.Fill(ds)
        If ds.Tables(0).Rows.Count > 0 Then
            e.Cancel = True
        End If
        
        MsgBox(e.Values("CostCategory"))
        
    End Sub

Open in new window

0
indyngAuthor Commented:
When I have the script below I get this error:

The data types text and nvarchar are incompatible in the equal to operator.

THANKS
    Protected Sub CostCategoryFormView_ItemInserting(ByVal sender As Object, ByVal e As FormViewInsertEventArgs)
        
        Dim strCostCategory As String
        
        strCostCategory = e.Values("CostCategory")
        
        Dim conn As New System.Data.SqlClient.SqlConnection("Data Source=.\SQLEXPRESS;AttachDbFilename='C:\Documents and Settings\RXNg\My Documents\Visual Studio 2005\WebSites\Logistics_Budget_Manager\App_Data\Logistics_Budget_Manager.mdf';Integrated Security=True;User Instance=True")
        Dim da As New System.Data.SqlClient.SqlDataAdapter("SELECT * FROM [Cost Category] WHERE [Cost Category].CostCategory=@CostCategory", conn)
        
        da.SelectCommand.Parameters.AddWithValue("@CostCategory", strCostCategory)
 
        Dim ds As New Data.DataSet()
        
        da.Fill(ds)
        If ds.Tables(0).Rows.Count > 0 Then
            e.Cancel = True
        End If
        
        MsgBox(e.Values("CostCategory"))
        
    End Sub

Open in new window

0
Guy Hengel [angelIII / a3]Billing EngineerCommented:
ok, 1 step further.

now, your column CostCategory is of data type TEXT.
if you are in sql 2005 (which I assume as you refer to sqlexpress), change that column to VARCHAR(MAX)  or lower, I doubt that you need 2GB place for a CostCategory information.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
indyngAuthor Commented:
angelIII:

It works! Thank you
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.