473,327 Members | 2,016 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,327 software developers and data experts.

DataGrid ParameterName not contained by SqlParameterCollection

Hi. I'm just getting started with vb.NET development in VS 2003. I have a
datagrid that lists a bunch of customers (hospitals). Next to the customer
number and name is a bunch of checkboxes that I want to use to check whether
it's a key account, defensive account, ambulatory surgery center, academic
hospital, or MD office. The grid opens with the checkboxes in edit mode and
shows whether a box is checked or not. This initial binding is working fine.
The problem is when I try to use the edits to update the SQL database (for
all rows at once). I don't think my unique row identifier (AccountID) is
working. I'm getting the error "An SqlParameter with ParameterName
'@AccountID' is not contained by this SqlParameterCollection...Line 52:
myCommand.Parameters("@AccountID").Value = AccountID"

Here's some of my HTML:
<asp:BoundColumn DataField="AccountID" SortExpression="AccountID"
HeaderText="ACCOUNT #"></asp:BoundColumn>
<asp:TemplateColumn HeaderText="KEY">
<ItemTemplate>
<asp:CheckBox ID="cbKey" Enabled="True" Checked='<%#
Databinder.Eval(Container.DataItem, "KeyAccount") %>' Runat="server" />
</ItemTemplate>
</asp:TemplateColumn>
....etc. for the other checkboxes.

Here's my code behind:
Imports System.Data
Imports System.Data.SqlClient
Imports System.Text

Public Class AccountAttributes
Inherits System.Web.UI.Page
Protected myComponent As New Component1
Protected WithEvents DemoGrid As System.Web.UI.WebControls.DataGrid
Protected WithEvents btnUpdate As System.Web.UI.WebControls.Button
Dim strConnNewProd As String =
ConfigurationSettings.AppSettings("strConnNewProd" )

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
If Not IsPostBack Then
'Create a SqlConnection object.
'Modify the connection string as necessary for your environment.
Dim cn As SqlConnection = New
SqlConnection("Server=NATWWW04DEV\INET_DEV;UID=End oscopySqlUser;PWD=2er@9b76m;Database=EndoNewProduc t")
Dim cmd As SqlCommand = New SqlCommand("Select AccountID,
CUSTOMER_N, CITY, STATE_OR_R, KeyAccount, DefensiveAccount, [ASC], Academic,
Office FROM AccountAttributes INNER JOIN dbo.tblAccountList ON
tblAccountList.CUSTOMER__ = AccountAttributes.AccountID WHERE SALES_TERR =
90011326 ORDER BY CUSTOMER_N ASC", cn)
'Dim cmd As SqlCommand = New SqlCommand("Select AccountID,
CUSTOMER_N, CITY, STATE_OR_R, SALES_TERR, KeyAccount, DefensiveAccount,
Academic, [ASC], Office FROM AccountAttributes INNER JOIN dbo.tblAccountList
ON tblAccountList.CUSTOMER__ = AccountAttributes.AccountID WHERE SALES_TERR =
" & _ID, cn)
cn.Open()
Dim reader As SqlDataReader = cmd.ExecuteReader()
DemoGrid.DataSource = reader
DataBind()
reader.Close()
cn.Close()
End If
End Sub

'Use this to update all records at once
Private Sub btnUpdate_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnUpdate.Click

'Declarations
Dim cn As SqlConnection = New
SqlConnection("Server=xxx;UID=xxx;PWD=xxx;Database =xxx")
Dim strSQL As String = "Select AccountID, CUSTOMER_N, CITY,
STATE_OR_R, KeyAccount, DefensiveAccount, [ASC], Academic, Office FROM
AccountAttributes INNER JOIN dbo.tblAccountList ON tblAccountList.CUSTOMER__
= AccountAttributes.AccountID WHERE SALES_TERR = 90011326"
Dim myCommand As New SqlCommand(strSQL, cn)
Dim i As Integer
Dim dgi As DataGridItem
Dim AccountID As Integer
Dim cbKey, cbDef, cbASC, cbAca, cbMD As CheckBox

'Loop through the grid
For i = 0 To DemoGrid.Items.Count - 1
'Read in the values from the TemplateColumns
dgi = DemoGrid.Items(i)
AccountID = CType(AccountID, Integer)
cbKey = CType(dgi.FindControl("cbKey"), CheckBox)
cbDef = CType(dgi.FindControl("cbDef"), CheckBox)
cbASC = CType(dgi.FindControl("cbASC"), CheckBox)
cbAca = CType(dgi.FindControl("cbAca"), CheckBox)
cbMD = CType(dgi.FindControl("cbMD"), CheckBox)

'Issue an UPDATE statement to the database
myCommand.Parameters("@AccountID").Value = AccountID
myCommand.Parameters("@KeyAccount").Value = cbKey.Checked
myCommand.Parameters("@DefensiveAccount").Value = cbDef.Checked
myCommand.Parameters("@ASC").Value = cbASC.Checked
myCommand.Parameters("@Academic").Value = cbAca.Checked
myCommand.Parameters("@Office").Value = cbMD.Checked

Dim updateSQL As String = "UPDATE AccountAttributes SET
KeyAccount = @KeyAccount, DefensiveAccount = @DefensiveAccount WHERE
AccountID = @AccountID"

'Open the connection, update the data, close the connection
cn.Open()
myCommand.ExecuteNonQuery()
cn.Close()
Next
End Sub
I'M REALLY FIRED UP ABOUT LEARNING HOW TO DO THIS AND I'VE SPENT AT LEAST 3
DAYS TRYING TO FIGURE OUT HOW TO DO IT TO NO AVAIL :( THANKS FOR ANY HELP!!

Nov 7 '05 #1
0 2048

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Stephan | last post by:
Hi, I'm experiencing the problem mentioned in the title above. Briefly, I build a page in which I include a class. This class contains a procedure to add a value to an ArrayList. The value is...
4
by: René Kabis | last post by:
People, I am at my wit's end. I am using the exact code from http://aspnet.4guysfromrolla.com/articles/071002-1.aspx And yet, the code does not manage to update the database. When I go to...
0
by: Mike C | last post by:
Hi. I'm just getting started with vb.NET development in VS 2003. I have a datagrid that lists a bunch of customers (hospitals). Next to the customer number and name is a bunch of checkboxes that I...
3
by: Jason Huang | last post by:
Hi, In my C# Windows form project. I am wondering can we manually define the width of a cell in a DataGrid? Thanks for help. Jason
2
by: DC | last post by:
The Code <%@ import namespace="System" %> <%@ import namespace="System.Web" %> <%@ import namespace="System.Web.UI" %> <%@ import namespace="System.Web.UI.HtmlControls" %> <%@ import...
0
by: DC | last post by:
The problem I'm using the .NET GridView and FormView objects for the first time and im getting the error "An OleDbParameter with ParameterName '@ID' is not contained by this...
1
by: Sharon | last post by:
Hello All, Is it possible to update Sql Table through DataGrid. I have a DataGrid which is being populated through a stored procedure, all i wanted to do is to update one field...
2
by: Brad Pears | last post by:
I have the following code that adds SQLParameter objects to an SQLParameterCollection object (well at least that's what I am trying to do!!) When the line of code runs that adds the parameter...
1
by: Dhananjay | last post by:
Hi all I have got this error An SqlParameter with ParameterName '@employment' is not contained by this SqlParameterCollection in asp.net
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.