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 Professional
Password Reminder
Register
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
ASP.NET 2.0 Professional If you are an experienced ASP.NET programmer, this is the forum for your 2.0 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 Professional 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 16th, 2007, 08:23 AM
Registered User
 
Join Date: Sep 2004
Location: Gary, Indiana, USA.
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Default Custom Validator against a database

Hi,
Could someone please elaborate on this line
"The custom validation logic loops through the rows in a table that is part of the dataset available to the page."
From the MSDN article:
http://msdn2.microsoft.com/en-us/library/s5z00s5e(vs.80).aspx
??? Please.
I've been all over the place trying to figure out how to make that sentence work, "the rows in a table..part of the dataset available to the page." I may be way out in left field, but this is not working...there is no "Object reference set to an instance of an object." when I post back:
>>>>>
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
        If Page.IsPostBack Then
            GetData()
        End If

    End Sub

    Public Function GetData() As DataSet
        Dim nameData As New DataSet
        Dim connString As String = ConfigurationManager.ConnectionStrings("Connection String").ConnectionString
        Dim dbConnection As New SqlConnection(connString) ' = New SqlClient.SqlConnection(dsn)
        Dim Query As String
        Query = "Select [Name] FROM tbl1"
        Dim dbCommand As New SqlCommand(Query, dbConnection)
        Dim sqlDataAdapter As New SqlDataAdapter(dbCommand)
        Try
            sqlDataAdapter.Fill(nameData)
        Catch ex As Exception

        End Try
        Return nameData

    End Function

    'Code from http://msdn2.microsoft.com/en-us/library/s5z00s5e(vs.80).aspx

    Private Sub CustomValidator1_ServerValidate(ByVal _
source As System.Object, ByVal args As _
System.Web.UI.WebControls.ServerValidateEven tArgs) _
Handles CustomValidator1.ServerValidate

        Dim dv As New DataView
        Dim nameData As New Data.DataSet

        dv = nameData.Tables("Name").DefaultView

        Dim datarow As DataRowView
        Dim txtName As String
        args.IsValid = False ' Assume False
        ' Loop through table and compare each record against user's entry
        For Each datarow In dv
            ' Extract name from the current row
            txtName = datarow.Item("Name").ToString()
            ' Compare name against user's entry
            If txtName = args.Value Then
                args.IsValid = True
                Exit For
            End If

        Next
    End Sub
<<<
And on the form:
>>>>
Name &nbsp;<asp:TextBox ID="txtName" runat="server"></asp:TextBox>
        <asp:CustomValidator ID="CustomValidator1" runat="server"
        ErrorMessage="Not valid" ControlToValidate="txtName" Width="161px"></asp:CustomValidator>

<<<
Thanks...I'm roadblocked. Or brain blocked. I don't know which.
I'm using VWD, SqlServerExpress and .Net 2.0 Framework
Steve
  #2 (permalink)  
Old November 16th, 2007, 08:48 AM
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: 100%
Activity: 100% Activity: 100% Activity: 100%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,089
Thanks: 80
Thanked 1,587 Times in 1,563 Posts
Default

Shouldn't it be something like this:

<s>Dim nameData As New Data.DataSet</s>
Dim nameData As Data.DataSet = GetData()

Otherwise, nameData is just an new, empty DataSet with no rows or columns.

Just a guess, as I am not exactly sure what you are trying to accomplish.

Imar
---------------------------------------
Imar Spaanjaars
http://Imar.Spaanjaars.Com
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.
  #3 (permalink)  
Old November 16th, 2007, 06:37 PM
Registered User
 
Join Date: Sep 2004
Location: Gary, Indiana, USA.
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks Imar,
But that doesn't work. You're right..I just have an empty dataset evidently. I have a text box (txtName) that I want to validate against the data in rows in a column in a SqlDatabase. Column name is "Name".

The code for doing that in
http://msdn2.microsoft.com/en-us/library/s5z00s5e.aspx
is confusing the heck out of me. In that code it calls

Dim dv As DataView
    Dim dataset11 As New Data.DataSet

    dv = dataset11.Tables(0).DefaultView

But evidently I don't know how to get "dataset1". I've renamed "dataset1" to "nameData" and am calling it from the function "GetData".
Please take a look at the msdn article and maybe that will tell you what I'm trying to accomplish and why I'm not accomplishing it! It's driving me batty.

  #4 (permalink)  
Old November 16th, 2007, 07:40 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: 100%
Activity: 100% Activity: 100% Activity: 100%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,089
Thanks: 80
Thanked 1,587 Times in 1,563 Posts
Default

I don't think the MSDN example is right. Take a look at this:

Dim dv As DataView
Dim dataset11 As New Data.DataSet

dv = dataset11.Tables(0).DefaultView

It news up dataset11 and then uses the DefaultView of the first table in the DataSet. That doesn't make sense, as dataset11 will always be empty.

Can you show you current code, and explain where it breaks with what exception?

Imar
---------------------------------------
Imar Spaanjaars
http://Imar.Spaanjaars.Com
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.
  #5 (permalink)  
Old November 16th, 2007, 08:03 PM
Registered User
 
Join Date: Sep 2004
Location: Gary, Indiana, USA.
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Well Imar, I got it working:

Private Sub CustomValidator1_ServerValidate(ByVal _
   source As System.Object, ByVal args As _
   System.Web.UI.WebControls.ServerValidateEventArgs) _
   Handles CustomValidator1.ServerValidate

        Dim nameData As New Data.DataSet
        Dim connString As String = ConfigurationManager.ConnectionStrings("Connection String").ConnectionString
        Dim dbConnection As New SqlConnection(connString) ' = New SqlClient.SqlConnection(dsn)
        Dim Query As String
        Query = "Select [Name] FROM tbl1"
        Dim dbCommand As New SqlCommand(Query, dbConnection)
        Dim sqlDataAdapter As New SqlDataAdapter(dbCommand)
        sqlDataAdapter.Fill(nameData)

        Dim dv As New DataView
        dv = nameData.Tables(0).DefaultView


        Dim datarow As DataRowView
        Dim txtName As String
        args.IsValid = False ' Assume False
        ' Loop through table and compare each record against user's entry
        For Each datarow In dv
            ' Extract name from the current row
            txtName = datarow.Item("Name").ToString()
            ' Compare name against user's entry
            If txtName = args.Value Then
                args.IsValid = True
                Exit For
            End If


        Next
    End Sub

I put the dataadapter in with the validator. and changed the named table("Name") to (0):
dv = nameData.Tables(0).DefaultView

Now I can play with it, but any suggestions as to how I can make
If txtName LIKE args.Value
??

Thanks for your helping on this.
Steve

  #6 (permalink)  
Old November 16th, 2007, 09:26 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: 100%
Activity: 100% Activity: 100% Activity: 100%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,089
Thanks: 80
Thanked 1,587 Times in 1,563 Posts
Default

Two ways:

1) Use the current, somewhat extensive and slow code. Once you have the DataSet from the database, use the Select method of the DataTable which you can access using nameData.Tables(0)

2) Skip all the DataSet stuff, set up a connection and execute a SqlDataReader for something like (pun unintended)

Select [Name] FROM tbl1 WHERE [Name] LIKE '%whatever you are searching for %'

If the SqlDataReader's HasRows property is true, you found what you're looking for.

HtH,

Imar
---------------------------------------
Imar Spaanjaars
http://Imar.Spaanjaars.Com
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.
  #7 (permalink)  
Old November 17th, 2007, 05:53 PM
Registered User
 
Join Date: Sep 2004
Location: Gary, Indiana, USA.
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks Imar,
Not a problem. I was a bit obsessed with getting that customvalidator code working. To accomplish what I want to do I guess it would be a lot easier to use the WHERE filter in the SELECT statement.
Thanks again!
Steve

  #8 (permalink)  
Old November 17th, 2007, 06:07 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: 100%
Activity: 100% Activity: 100% Activity: 100%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,089
Thanks: 80
Thanked 1,587 Times in 1,563 Posts
Default

You can still use the custom validator. There's just no need to do all this data set stuff. You can set up a SqlDataReader, fire the necessary SQL statement and then determine IsValid based in the reader:

 args.IsValid = Not mySqlDataReader.HasRows

This way, when a row is found, the validator is not valid...

Imar
---------------------------------------
Imar Spaanjaars
http://Imar.Spaanjaars.Com
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.
  #9 (permalink)  
Old January 8th, 2008, 09:15 PM
Registered User
 
Join Date: Jan 2008
Location: , , .
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I was able to use this code as a model for adding the custom validator to already existing code. I'm not a programmer, the code was generated by a module builder for Dotnetnuke and the code generated uses the same file/form for both adding and editing a record. I'm not sure separate files/forms can be created, but I don't think so without doing a lot of programming that goes over my head.

Anyway, this works when adding a record. But, when editing a record, since it exists in the database the duplicate entry will return a "true, the entry exists already" message and won't let the user resave the record without changing the the field to something altogether unique again. Is there a way to handle this using the same form for adding and editing records? A way to exclude the current record from the lookup for duplicate entries? I can't have the validator turned off because it still needs to check for duplicates if the field entry is changed, it needs to just exclude the current records current underlying value.

Thanks,
Rick
  #10 (permalink)  
Old January 9th, 2008, 02:10 AM
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: 100%
Activity: 100% Activity: 100% Activity: 100%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,089
Thanks: 80
Thanked 1,587 Times in 1,563 Posts
Default

Hi there,

You could exclude the primary key from the check. Let's say your table has a unique Id column. Your pseudo lookup code would then look like this:

Select [Name] FROM tbl1 WHERE [Name] LIKE '%whatever you are searching for %' AND Id <> currentId

Instead of currentId you need to embed the ID of the current record.

Hope this helps,

Imar
---------------------------------------
Imar Spaanjaars
http://Imar.Spaanjaars.Com
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.
 


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
DDL Custom Validator AspNetGuy BOOK: Beginning ASP.NET 3.5 : in C# and VB BOOK ISBN: 978-0-470-18759-3 2 June 6th, 2008 05:17 PM
custom validator question kscdave Classic ASP Professional 3 April 24th, 2005 07:11 AM
Custom Validator for Two text box. vickyhere ASP.NET 1.0 and 1.1 Basics 2 October 15th, 2004 06:41 AM
Custom Validator bmains ASP.NET 1.0 and 1.1 Basics 4 December 18th, 2003 11:45 AM



All times are GMT -4. The time now is 09:19 AM.


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