Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 2.0 > ASP.NET 2.0 Basics
Password Reminder
| FAQ | Members List | Search | Today's Posts | Mark Forums Read
ASP.NET 2.0 Basics If you are new to ASP or ASP.NET programming with version 2.0, this is the forum to begin asking questions. Please also see the Visual Web Developer 2005 forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 2.0 Basics section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old November 27th, 2006, 07:33 AM
Authorized User
Join Date: Mar 2005
Location: , , .
Posts: 23
Thanks: 0
Thanked 0 Times in 0 Posts
Default Checking for duplicate primary key in Access

Hi folks,

I am working with ASP.NET 2.0 and am having a headache trying to check for an existing primary key in an Access database. I have a web form that will be used to add a new record of staff details. Currently there are several thousand members of staff that are stored in the database, each has a unique primary key entitled staff_id. This is simply a 4 digit number (for example, 2345), if I add a record with an existing staff ID it will post back an asp.net error. I am attempting (and rather badly) to write a script that will check for the existing field staff_id if the user enters an existing ID number in the web form textbox. This is what I have come up with so far but it simply throws back this error.

Error 2 Operator '==' cannot be applied to operands of type 'method group' and 'string' C:\Inetpub\wwwroot\CourseList\Admin\addStaff.aspx 23 12 C:\...\CourseList
I am working with C# and trying to convert a piece of VB.Net code. I have done record checks before in classic asp and have never had a problem but now I am lost. This is the script I have written.

<script runat="server">

protected void bntClick_Click(object sender, EventArgs e)


           Boolean ValidRecord;

           string connStaff = ConfigurationSettings.AppSettings["ConnStaff"];

           OleDbConnection conn = new OleDbConnection(connStaff);


           string sqlString = "SELECT COUNT(*) FROM staff WHERE staff_id = '" + txtstaffID.Text + "'";

           OleDbCommand myCommand = new OleDbCommand(sqlString, conn);

           object qryValue = myCommand.ExecuteScalar();

           if(qryValue.ToString == txtstaffID.Text)


                  Label1.Text = "This is a duplicate record";

                  ValidRecord = false;





                  ValidRecord = true;



 Any help with this would be greatly appreciated.

Reply With Quote
  #2 (permalink)  
Old November 29th, 2006, 12:11 PM
Authorized User
Join Date: Sep 2006
Location: , , India.
Posts: 66
Thanks: 0
Thanked 0 Times in 0 Posts

Hi ,
The line
 object qryValue = myCommand.ExecuteScalar();

is where problem lies.

u must store the result of myCommand.ExecuteScalar(); as string as

string qryvalue = (string)myCommand.ExecuteScalar();
the check it.

Reply With Quote
  #3 (permalink)  
Old November 29th, 2006, 12:19 PM
Imar's Avatar
Wrox Author
Points: 72,073, Level: 100
Points: 72,073, Level: 100 Points: 72,073, Level: 100 Points: 72,073, Level: 100
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,089
Thanks: 80
Thanked 1,587 Times in 1,563 Posts

You forgot the parentheses on the method call ToString

 if(qryValue.ToString == txtstaffID.Text)

should be

 if(qryValue.ToString() == txtstaffID.Text)

A bit confusing error message, I must say... ;)

Imar Spaanjaars
Everyone is unique, except for me.
Author of ASP.NET 2.0 Instant Results and Beginning Dreamweaver MX / MX 2004
Want to be my colleague? Then check out this post.
Reply With Quote

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

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Inserting duplicate values in primary key column, Shuchik SQL Server ASP 1 August 31st, 2007 05:38 AM
Duplicate Primary Key donevco Access 7 January 18th, 2007 01:59 PM
Detect Primary Key in Access TSEROOGY Classic ASP Databases 1 December 13th, 2004 09:12 AM
Get primary Key Columns From Access Table MeSaqi Access VBA 0 July 18th, 2003 08:20 AM

All times are GMT -4. The time now is 04:17 AM.

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