Wrox Programmer Forums
Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 2.0 > ASP.NET 2.0 Basics
| 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 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
  #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.

Code:
<script runat="server">

protected void bntClick_Click(object sender, EventArgs e)

{

           Boolean ValidRecord;

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

           OleDbConnection conn = new OleDbConnection(connStaff);

           conn.Open();

           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;



           }

          else

           {

                  AccessDataSource3.Insert();

                  ValidRecord = true;

           }

}



</script>
 Any help with this would be greatly appreciated.

  #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
Default

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.




  #3 (permalink)  
Old November 29th, 2006, 12:19 PM
Imar's Avatar
Wrox Author
Points: 70,322, Level: 100
Points: 70,322, Level: 100 Points: 70,322, Level: 100 Points: 70,322, Level: 100
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

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
---------------------------------------
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.


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





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