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

G

Guest

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/d...ml/vbtskvalidatingagainstvaluesindatabase.asp" .

But it doesnt work. My code is::

private void CustomValidator2_ServerValidate(object
source,System.Web.UI.WebControls.ServerValidateEventArgs 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;
}
}
}
 
H

Hans Kesting

pmud said:
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/d...ml/vbtskvalidatingagainstvaluesindatabase.asp" .

But it doesnt work. My code is::

private void CustomValidator2_ServerValidate(object
source,System.Web.UI.WebControls.ServerValidateEventArgs 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?
 
G

Guest

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 said:
pmud said:
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/d...ml/vbtskvalidatingagainstvaluesindatabase.asp" .

But it doesnt work. My code is::

private void CustomValidator2_ServerValidate(object
source,System.Web.UI.WebControls.ServerValidateEventArgs 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?
 
H

Hans Kesting

pmud said:
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
:

pmud said:
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/d...ml/vbtskvalidatingagainstvaluesindatabase.asp" .

But it doesnt work. My code is::

private void CustomValidator2_ServerValidate(object
source,System.Web.UI.WebControls.ServerValidateEventArgs 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?
 
G

Guest

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 said:
pmud said:
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
:

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/d...ml/vbtskvalidatingagainstvaluesindatabase.asp" .

But it doesnt work. My code is::

private void CustomValidator2_ServerValidate(object
source,System.Web.UI.WebControls.ServerValidateEventArgs 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?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top