开发者

checking for duplicate values before attempting insert (ASP.NET)

开发者 https://www.devze.com 2023-02-25 03:32 出处:网络
I have a fo开发者_开发技巧rm where two fields on the first page of the form make up the primary key. I want to check for duplicate values before attempting to insert the record, since I don\'t want th

I have a fo开发者_开发技巧rm where two fields on the first page of the form make up the primary key. I want to check for duplicate values before attempting to insert the record, since I don't want the user to go all the way through the form only to find out they can't submit it. So I'm trying to check for duplicate values when the user tries to go to the next page of the form. I wasn't quite sure how to do it, and sure enough I'm getting an error. ("Object reference not set to an instance of an object.") The problem is apparently in my if statement, "If myValue.Length > 0 Then", but I'm not sure what needs to be in place of that.

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

    'get values
    Dim checkPrefix = txtCoursePrefix.Text
    Dim checkNum = txtCourseNum.Text

    'db connectivity
    Dim myConn As New OleDbConnection
    myConn.ConnectionString = AccessDataSource1.ConnectionString
    myConn.Open()

    'select records
    Dim mySelect As New OleDbCommand("SELECT prefix, course_number FROM tableCourse WHERE prefix='checkPrefix' AND course_number='checkNum'", myConn)

    'execute(Command)
    Dim myValue As String = mySelect.ExecuteScalar()
    'check if record exists
    If myValue.Length > 0 Then
        CustomValidator1.ErrorMessage = "some exp text"
        CustomValidator1.SetFocusOnError = "true"
        CustomValidator1.IsValid = "false"
    End If

End Sub

Thought I'd post the final solution:

'select records
    Dim mySelect As New OleDbCommand("SELECT 1 FROM tableCourse WHERE prefix=? AND course_number=?", myConn)
    mySelect.Parameters.AddWithValue("@checkPrefix", checkPrefix)
    mySelect.Parameters.AddWithValue("@checkNum", checkNum) 

    'execute(Command)
    Dim myValue = mySelect.ExecuteScalar()

    'check if record exists
    If myValue IsNot Nothing Then
        CustomValidator1.SetFocusOnError = True
        args.IsValid = False
    End If


This error indicates that the content of myValue variable is null. If it's null you can't use Length property (or any other property for that matter) on it. You have to check for null explicitly:

If myValue IsNot Nothing Then

EDIT 1 Your sql query is wrong. I don't know what would be the right query, as I don't know your database, but I think you intender to write this:

Dim mySelect As New OleDbCommand("SELECT prefix, course_number FROM tableCourse WHERE prefix=" + checfkPreix + " AND course_number=" + checkNum, myConn)

or something to that effect. You might want to consider using string.Format function for forming the string. And you also need to make sure that there is some kind of protection against SQL Injection, since you form your query from user input. In your case using of OleDbParameter might be appropriate.

Edit 2

You also right to mention that there might be a problem with ExecuteScalar. ExecuteScalar is supposed to return a single value and your select query are returning two (prefix and course_number). Change it so that it returns a single parameter SELECT prefix FROM or simply SELECT 1 FROM and then the rest of the query:

Dim mySelect As New OleDbCommand("SELECT 1 FROM tableCourse WHERE prefix=? AND course_number=?", myConn)
    mySelect.Parameters.AddWithValue("@checkPrefix", checkPrefix)
    mySelect.Parameters.AddWithValue("@checkNum", checkNum)

Edit 3 You are not setting failed validation properly in your validator. Add

 args.IsValid = False 

inside your if statement.


First ExecuteScalar will only return a single value, so in this case you are only going to get the column prefix from the result. Second if there is no match with your query it will return null, so your next length check should account for that scenario:

if String.IsNullOrEmpty(myValue) Then
...

Reference: http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlcommand.executescalar.aspx


myValue is null if there is no duplicate, so you have to apply .Length only if myValue is not null (which means checking for null only is enough; without .Length)

If Not string.IsNullOrEmpty(myValue) Then


try something like this instead (you will have to adapt it to VB.Net) DBNull is different from Null or Nothing so you have to compare it to both

If myValue <> DBNull and not myvalue is nothing Then
0

精彩评论

暂无评论...
验证码 换一张
取 消