p2p.wrox.com Forums

Need to download code?

View our list of code downloads.


Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
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 p2p Programmer to Programmer discussion community. This is a community of more than 40,000 computer programmers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining our free Wrox p2p community you can post your own programming questions and respond to other programmers’ questions. Registered users also don't have to see the ads that are displayed to guests. Registration is fast, simple and absolutely free so please, join today!
Join today and post to win prizes! Post more to increase your chances of being Wrox’s top poster of the month.

Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old July 4th, 2009, 02:58 AM
Authorized User
Points: 141, Level: 2
Points: 141, Level: 2 Points: 141, Level: 2 Points: 141, Level: 2
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
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?
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
  #2 (permalink)  
Old July 4th, 2009, 04:48 AM
Imar's Avatar
Wrox Author
Points: 33,554, Level: 80
Points: 33,554, Level: 80 Points: 33,554, Level: 80 Points: 33,554, Level: 80
Activity: 100%
Activity: 100% Activity: 100% Activity: 100%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 10,228
Thanks: 7
Thanked 203 Times in 201 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
Everyone is unique, except for me.
Author of Beginning ASP.NET 3.5 : in C# and VB, ASP.NET 2.0 Instant Results and Dreamweaver MX 2004

Did this post help you? Click the button to show your appreciation!
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
update multiple columns in an update statement debbiecoates SQL Server 2000 1 August 17th, 2008 05:01 AM
Opening new window to update db info... Danielav21 Classic ASP Professional 3 April 19th, 2007 07: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 03:45 PM



All times are GMT -4. The time now is 11:54 PM.


Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
© 2008 Wiley Publishing, Inc