[Webinar] Streamline your web hosting managementRegister Today

x
?
Solved

Why am I getting an error?

Posted on 2008-11-05
5
Medium Priority
?
244 Views
Last Modified: 2008-11-05
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

0
Comment
Question by:indyng
  • 3
  • 2
5 Comments
 
LVL 143

Expert Comment

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

Open in new window

0
 
LVL 1

Author Comment

by:indyng
ID: 22889863
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
 
LVL 1

Author Comment

by:indyng
ID: 22889889
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
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 2000 total points
ID: 22890127
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
 
LVL 1

Author Comment

by:indyng
ID: 22890335
angelIII:

It works! Thank you
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Is your OST file inaccessible, Need to transfer OST file from one computer to another? Want to convert OST file to PST? If the answer to any of the above question is yes, then look no further. With the help of Stellar OST to PST Converter, you can e…
Planning to migrate your EDB file(s) to a new or an existing Outlook PST file? This video will guide you how to convert EDB file(s) to PST. Besides this, it also describes, how one can easily search any item(s) from multiple folders or mailboxes…
Suggested Courses

591 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question