2011-04-14 65 views
2

我有一个窗体,窗体的第一页上的两个字段组成主键。我想在尝试插入记录之前检查重复值,因为我不希望用户通过表单完全查找他们无法提交的内容。所以我试图在用户试图去到表单的下一页时检查重复值。我不太确定该怎么做,当然我得到一个错误。 (“对象引用未设置为对象的实例”)。问题显然在我的if语句中,“如果myValue.Length> 0 Then”,但我不确定需要替代的是什么。尝试插入前检查重复值(ASP.NET)

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 

以为我会发布最终的解决方案:

'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 
+0

什么是数据库? – 2011-04-14 23:07:49

+0

@zespri Microsoft Access。 – Sara 2011-04-14 23:19:49

回答

1

此错误表明myvalue的变量的内容为空。如果它为空,则不能在其上使用Length属性(或其他任何属性)。你必须明确地检查空:

If myValue IsNot Nothing Then 

编辑1 您的SQL查询是错误的。我不知道什么是正确的查询,因为我不知道你的数据库,但我认为你购买意向写这篇文章:

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

或诸如此类的话。您可能需要考虑使用string.Format函数来形成字符串。而且您还需要确保有针对SQL Injection的某种保护措施,因为您可以根据用户输入形成查询。在你的情况下使用OleDbParameter可能是合适的。

编辑2

您也有权提,有可能是用的ExecuteScalar一个问题。 ExecuteScalar应该返回一个值,并且您的选择查询返回两个(prefix和course_number)。

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) 

编辑3 您没有设置在验证失败验证正确:以便它返回一个参数SELECT prefix FROM或者干脆SELECT 1 FROM,然后将查询的其余部分更改。 加入

args.IsValid = False 

在你的if语句中。

+0

试过,同样的事情发生在上面的一个,没有错误了,但它没有当我输入重复值时停止我。 – Sara 2011-04-14 22:47:03

+0

这意味着您用于检查重复值的查询是错误的。它返回null,如你所期望的那样返回别的东西。解决这个问题,它会解决你的问题 – 2011-04-14 22:53:45

+0

哦,当然是错的!看看你如何传递参数!你并没有真正传递它们,它们是你写它的方式,它们只是文字,是你的字符串的一部分! – 2011-04-14 22:57:17

0

myValue为null,如果没有重复的,所以你必须申请.Length只有myValue不为空(这意味着检查null只就够了;没有.Length

If Not string.IsNullOrEmpty(myValue) Then 
+0

在String.IsNullOrEmpty中没有隐含的长度> 0吗? – 2011-04-14 22:33:25

+1

是的,我编辑了我的答案......同时你写了你的评论。 – manji 2011-04-14 22:35:57

+0

谢谢,试了一下,我没有得到错误了,但它没有阻止我,当我输入一个重复的值... – Sara 2011-04-14 22:44:36

0

尝试这样的事情,而不是(你将不得不使其适应VB.Net)的DBNull为空或没有什么不同,所以你必须把它比作两个

If myValue <> DBNull and not myvalue is nothing Then