473,411 Members | 2,093 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,411 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 2053

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: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

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.