473,322 Members | 1,496 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,322 software developers and data experts.

Validating: If the value EXISTS in Db then show error!

Hi,

I am using a compare validator in asp.net application(c# code). This is used
for comparing a value enterd by the user against the primary key in the SQL
database. IF the VALUE ENTERED BY THE USER EXISTS IN THE DB , then THE ERROR
MESSAGE OF THE COMPARE VALIDATOR SHOULD BE DISPLAYED.

For this, I used the reference artiicle
"http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vbcon/html/vbtskvalidatingagainstvaluesindatabase.asp" .

But it doesnt work. My code is::

private void CustomValidator2_ServerValidate(object
source,System.Web.UI.WebControls.ServerValidateEve ntArgs args)
{
DataView dv= new DataView();
dv=dsValidate1.Tables[0].DefaultView;
string asi;
args.IsValid=true;
foreach(DataRowView datarow in dv)
{
// Extract asi from the current row
asi = datarow["Alias"].ToString();

// Compare asi against user's entry
if (asi == args.Value)
{
args.IsValid = false;
}
}
}

--
pmud
Nov 19 '05 #1
5 3574
Please tell me what is the problem with this code?

Any help is appreciated.

"pmud" wrote:
Hi,

I am using a compare validator in asp.net application(c# code). This is used
for comparing a value enterd by the user against the primary key in the SQL
database. IF the VALUE ENTERED BY THE USER EXISTS IN THE DB , then THE ERROR
MESSAGE OF THE COMPARE VALIDATOR SHOULD BE DISPLAYED.

For this, I used the reference artiicle
"http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vbcon/html/vbtskvalidatingagainstvaluesindatabase.asp" .

But it doesnt work. My code is::

private void CustomValidator2_ServerValidate(object
source,System.Web.UI.WebControls.ServerValidateEve ntArgs args)
{
DataView dv= new DataView();
dv=dsValidate1.Tables[0].DefaultView;
string asi;
args.IsValid=true;
foreach(DataRowView datarow in dv)
{
// Extract asi from the current row
asi = datarow["Alias"].ToString();

// Compare asi against user's entry
if (asi == args.Value)
{
args.IsValid = false;
}
}
}

--
pmud

Nov 19 '05 #2
pmud wrote:
Hi,

I am using a compare validator in asp.net application(c# code). This is used
for comparing a value enterd by the user against the primary key in the SQL
database. IF the VALUE ENTERED BY THE USER EXISTS IN THE DB , then THE ERROR
MESSAGE OF THE COMPARE VALIDATOR SHOULD BE DISPLAYED.

For this, I used the reference artiicle
"http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vbcon/html/vbtskvalidatingagainstvaluesindatabase.asp" .

But it doesnt work. My code is::

private void CustomValidator2_ServerValidate(object
source,System.Web.UI.WebControls.ServerValidateEve ntArgs args)
{
DataView dv= new DataView();
dv=dsValidate1.Tables[0].DefaultView;
string asi;
args.IsValid=true;
foreach(DataRowView datarow in dv)
{
// Extract asi from the current row
asi = datarow["Alias"].ToString();

// Compare asi against user's entry
if (asi == args.Value)
{
args.IsValid = false;
}
}
}


Are you using a "compare validator" or a "custom validator"?
This is a task for a custom validator.

What do you mean by "does not work"?
- you never get errormessages
- you always get errormessages
- errorcondition is reversed (allows wrong values, block correct ones)

The test can be done in different ways:
- try and find it directly in the database (no rows -> fine)
- use the Select method on the DataTable to find rows

Note: you test is case-sensitive, is that on purpose?

--
Hans Kesting
Nov 19 '05 #3
Hi Hans,

I am using a Custom Validator only. & By does not work what I mean is that
when i enter a value(which already exists in the database) in the text box &
hit submit, the Validation sumaary doesnt show error message. Instead, I get
the error as::

Server Error in '/NEW' Application.

Violation of PRIMARY KEY constraint 'PK_FormFields'. Cannot insert duplicate
key in object 'FormFields'. The statement has been terminated.

Description: An unhandled exception occurred during the execution of the
current web request. Please review the stack trace for more information about
the error and where it originated in the code.

Rather than this the user been shown this Server error page, I want the
custom validator to check that if the value already exists in the database,
the validation summary should show error.

I have no idea why this is happening. Please Help.

Thanks

"Hans Kesting" wrote:
pmud wrote:
Hi,

I am using a compare validator in asp.net application(c# code). This is used
for comparing a value enterd by the user against the primary key in the SQL
database. IF the VALUE ENTERED BY THE USER EXISTS IN THE DB , then THE ERROR
MESSAGE OF THE COMPARE VALIDATOR SHOULD BE DISPLAYED.

For this, I used the reference artiicle
"http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vbcon/html/vbtskvalidatingagainstvaluesindatabase.asp" .

But it doesnt work. My code is::

private void CustomValidator2_ServerValidate(object
source,System.Web.UI.WebControls.ServerValidateEve ntArgs args)
{
DataView dv= new DataView();
dv=dsValidate1.Tables[0].DefaultView;
string asi;
args.IsValid=true;
foreach(DataRowView datarow in dv)
{
// Extract asi from the current row
asi = datarow["Alias"].ToString();

// Compare asi against user's entry
if (asi == args.Value)
{
args.IsValid = false;
}
}
}


Are you using a "compare validator" or a "custom validator"?
This is a task for a custom validator.

What do you mean by "does not work"?
- you never get errormessages
- you always get errormessages
- errorcondition is reversed (allows wrong values, block correct ones)

The test can be done in different ways:
- try and find it directly in the database (no rows -> fine)
- use the Select method on the DataTable to find rows

Note: you test is case-sensitive, is that on purpose?

--
Hans Kesting

Nov 19 '05 #4
pmud wrote:
Hi Hans,

I am using a Custom Validator only. & By does not work what I mean is that
when i enter a value(which already exists in the database) in the text box &
hit submit, the Validation sumaary doesnt show error message. Instead, I get
the error as::

Server Error in '/NEW' Application.

Violation of PRIMARY KEY constraint 'PK_FormFields'. Cannot insert duplicate
key in object 'FormFields'. The statement has been terminated.

Description: An unhandled exception occurred during the execution of the
current web request. Please review the stack trace for more information about
the error and where it originated in the code.

Rather than this the user been shown this Server error page, I want the
custom validator to check that if the value already exists in the database,
the validation summary should show error.

I have no idea why this is happening. Please Help.

Thanks
The errormessage is because you insert a duplicate value.

As I said before, your test is case-sensitive, did you mean that?
What I didn't add was that SqlServer might use a case-INsensitive
test for your key-field.
example: in the database you have the value 'TEST', you type in
'test', the compare sees no problems, but when you store the
row the value is already present.

If this is the problem, you could convert both the db-value
and args.Value to lowercase (or upper, if you prefer that)
before testing.

Are you using VS.Net? Then you could step through this code
with the debugger to see what really happens.

It could even be that the handler-method is not connected
to the event anymore!

Hans Kesting

"Hans Kesting" wrote:

pmud wrote:
Hi,

I am using a compare validator in asp.net application(c# code). This is used
for comparing a value enterd by the user against the primary key in the SQL
database. IF the VALUE ENTERED BY THE USER EXISTS IN THE DB , then THE ERROR
MESSAGE OF THE COMPARE VALIDATOR SHOULD BE DISPLAYED.

For this, I used the reference artiicle
"http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vbcon/html/vbtskvalidatingagainstvaluesindatabase.asp" .

But it doesnt work. My code is::

private void CustomValidator2_ServerValidate(object
source,System.Web.UI.WebControls.ServerValidate EventArgs args)
{
DataView dv= new DataView();
dv=dsValidate1.Tables[0].DefaultView;
string asi;
args.IsValid=true;
foreach(DataRowView datarow in dv)
{
// Extract asi from the current row
asi = datarow["Alias"].ToString();

// Compare asi against user's entry
if (asi == args.Value)
{
args.IsValid = false;
}
}
}


Are you using a "compare validator" or a "custom validator"?
This is a task for a custom validator.

What do you mean by "does not work"?
- you never get errormessages
- you always get errormessages
- errorcondition is reversed (allows wrong values, block correct ones)

The test can be done in different ways:
- try and find it directly in the database (no rows -> fine)
- use the Select method on the DataTable to find rows

Note: you test is case-sensitive, is that on purpose?

--
Hans Kesting

--
Hans Kesting
Nov 19 '05 #5
Hi Hans,

I dont think the it is case sensitive coz when I entered the Upper case
value & the lower case value existed in the database, it showed up the exact
same error.

Ya, I am using VS.NEt. Could you please tell me how to step through this code
with the debugger to see what really happens. I am kind of new to this whole
process. Thast why. Your hrlp will be appreciated.

Aslo, I think , as you said, that the handler-method is not connected
to the event anymore!

How to get over this?


"Hans Kesting" wrote:
pmud wrote:
Hi Hans,

I am using a Custom Validator only. & By does not work what I mean is that
when i enter a value(which already exists in the database) in the text box &
hit submit, the Validation sumaary doesnt show error message. Instead, I get
the error as::

Server Error in '/NEW' Application.

Violation of PRIMARY KEY constraint 'PK_FormFields'. Cannot insert duplicate
key in object 'FormFields'. The statement has been terminated.

Description: An unhandled exception occurred during the execution of the
current web request. Please review the stack trace for more information about
the error and where it originated in the code.

Rather than this the user been shown this Server error page, I want the
custom validator to check that if the value already exists in the database,
the validation summary should show error.

I have no idea why this is happening. Please Help.

Thanks


The errormessage is because you insert a duplicate value.

As I said before, your test is case-sensitive, did you mean that?
What I didn't add was that SqlServer might use a case-INsensitive
test for your key-field.
example: in the database you have the value 'TEST', you type in
'test', the compare sees no problems, but when you store the
row the value is already present.

If this is the problem, you could convert both the db-value
and args.Value to lowercase (or upper, if you prefer that)
before testing.

Are you using VS.Net? Then you could step through this code
with the debugger to see what really happens.

It could even be that the handler-method is not connected
to the event anymore!

Hans Kesting

"Hans Kesting" wrote:

pmud wrote:

Hi,

I am using a compare validator in asp.net application(c# code). This is used
for comparing a value enterd by the user against the primary key in the SQL
database. IF the VALUE ENTERED BY THE USER EXISTS IN THE DB , then THE ERROR
MESSAGE OF THE COMPARE VALIDATOR SHOULD BE DISPLAYED.

For this, I used the reference artiicle
"http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vbcon/html/vbtskvalidatingagainstvaluesindatabase.asp" .

But it doesnt work. My code is::

private void CustomValidator2_ServerValidate(object
source,System.Web.UI.WebControls.ServerValidate EventArgs args)
{
DataView dv= new DataView();
dv=dsValidate1.Tables[0].DefaultView;
string asi;
args.IsValid=true;
foreach(DataRowView datarow in dv)
{
// Extract asi from the current row
asi = datarow["Alias"].ToString();

// Compare asi against user's entry
if (asi == args.Value)
{
args.IsValid = false;
}
}
}
Are you using a "compare validator" or a "custom validator"?
This is a task for a custom validator.

What do you mean by "does not work"?
- you never get errormessages
- you always get errormessages
- errorcondition is reversed (allows wrong values, block correct ones)

The test can be done in different ways:
- try and find it directly in the database (no rows -> fine)
- use the Select method on the DataTable to find rows

Note: you test is case-sensitive, is that on purpose?

--
Hans Kesting

--
Hans Kesting

Nov 19 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: mike | last post by:
Hello, After trying to validate this page for a couple of days now I was wondering if someone might be able to help me out. Below is a list of snippets where I am having the errors. 1. Line 334,...
1
by: Craig Beuker | last post by:
Hello, I am experimenting with this XmlValidatingReader and have a question about how it is working (or not working as would be the case) The sample documents and code are included at the end...
2
by: Joakim Olesen | last post by:
Hello When validating xml against a schema, the most frequent error I get is something like "The 'FOO' attribute has an invalid value according to its data type. An error occurred at , (25,...
5
by: Stephen | last post by:
Could someone please help me with some validation. I have to write code which checks to see whether a dropdown list is populated with a value or a checkbox is checked. I want the code to run on the...
0
by: enahar | last post by:
I want to validate a cell wheather a value is greater than 0 or not.If value is greater than 0 then I want to show messagebox and focus back the same cell else move to the other cell. I am...
7
by: | last post by:
Hello to all I'm handling the Validating event for one text box. If something is wrong in user input I show a warning message. The problem is that if I add to method the message is displayed...
2
by: Chris Dunaway | last post by:
I have a form with a textbox and numerous panels, buttons and other controls. I have handled the textbox Validating and Validated events. The textbox will hold a filename. In the validating...
232
by: robert maas, see http://tinyurl.com/uh3t | last post by:
I'm working on examples of programming in several languages, all (except PHP) running under CGI so that I can show both the source files and the actually running of the examples online. The first...
24
by: runway27 | last post by:
i have used the following code to validate the username it is working fineif( $username == "" || !preg_match("/^+(?:_+)?$/i", $username) ) { $error.="User name cannot be blank or has special...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.