Wrox Programmer Forums
|
ASP.NET 3.5 Basics If you are new to ASP or ASP.NET programming with version 3.5, this is the forum to begin asking questions. Please also see the Visual Web Developer 2008 forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 3.5 Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old July 4th, 2009, 01:58 AM
Authorized User
 
Join Date: Jun 2009
Posts: 30
Thanks: 13
Thanked 0 Times in 0 Posts
Default update info

HI.....now i am trying to update the contact name and phone in the Customers table by not changing the customer ID(primary key)....so here is my code...look for the highlighted error()

protectedvoid Button1_Click(object sender, EventArgs e)
{
SqlConnection conCust;
SqlCommand cmdUpdate,cmdSelect;
SqlDataReader dtrCust;
string conStr = ConfigurationManager.ConnectionStrings["ConnectionCust"].ConnectionString;
conCust = newSqlConnection(conStr);

cmdSelect = newSqlCommand("Select Customerid From Customers",conCust);
cmdUpdate = newSqlCommand("Update Customers set ContactName=@contName,phone=@con_phone Where CustomerID=@id)",conCust);
conCust.Open();
dtrCust = cmdSelect.ExecuteReader();

if (dtrCust.HasRows)
{
cmdUpdate.Parameters.AddWithValue("@contName", TextBox2.Text);
cmdUpdate.Parameters.AddWithValue("@con_phone", TextBox3.Text);

}
else
{
Label1.Text = "Customer ID cannot be updated...</br> Enter id that already exist";
}
cmdUpdate.ExecuteNonQuery();//error appear here(invalidOperationException)


conCust.Close();
Label1.Text = "Record is updated";

}
it says... there is already an open DataReader associated with this Command which must be closed first.....how am i going to close it?and i want to clarify something if i want to update the info in the table without changing the customer ID....i must read(sqlDataReader) it right?
 
Old July 4th, 2009, 03:48 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Hi there,

You can close the reader by calling Close() on it.

However, in your situation there's a better alternative. Don't check up front if the record exists. Just perform a regular UPDATE immediately without executing a SELECT first, and then check the return value of ExecuteNonQuery to see if any records were affected.

Code:
 
if (cmdUpdate.ExecuteNonQuery() > 0)
{
  // found
}
else
{
  // not found
}
More info:
http://msdn.microsoft.com/en-us/libr...enonquery.aspx


Hope this helps,

Imar
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!





Similar Threads
Thread Thread Starter Forum Replies Last Post
update multiple columns in an update statement debbiecoates SQL Server 2000 1 August 17th, 2008 04:01 AM
Opening new window to update db info... Danielav21 Classic ASP Professional 3 April 19th, 2007 06:14 PM
Update info from Access Wall st Guru Excel VBA 0 February 27th, 2006 09:46 AM
Update info on home page via FTP/other levinho Classic ASP Databases 1 November 11th, 2003 09:40 PM
Where would I get info on how to download info aidanc ASP.NET 1.x and 2.0 Application Design 1 September 29th, 2003 02:45 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.