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

November 16th, 2007, 08:23 AM
|
Registered User
|
|
Join Date: Sep 2004
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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 <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
|

November 16th, 2007, 08:48 AM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
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.
|

November 16th, 2007, 06:37 PM
|
Registered User
|
|
Join Date: Sep 2004
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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.
|

November 16th, 2007, 07:40 PM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
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.
|

November 16th, 2007, 08:03 PM
|
Registered User
|
|
Join Date: Sep 2004
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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
|

November 16th, 2007, 09:26 PM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
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.
|

November 17th, 2007, 05:53 PM
|
Registered User
|
|
Join Date: Sep 2004
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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
|

November 17th, 2007, 06:07 PM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
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.
|

January 8th, 2008, 09:15 PM
|
Registered User
|
|
Join Date: Jan 2008
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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
|

January 9th, 2008, 02:10 AM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
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.
|
|
 |