【问题标题】:Implement a search engine in ASP.NET and VB.net from .sdf database [duplicate]从 .sdf 数据库在 ASP.NET 和 VB.net 中实现搜索引擎 [重复]
【发布时间】:2015-11-02 08:54:07
【问题描述】:

我正在尝试在 ASP.NET 和 VB.net 中实现搜索引擎。搜索来自 .sdf 数据库。我不断收到:

System.NullReferenceException:对象引用未设置为对象的实例

错误发生就行了

myDA.SelectCommand = cm

但我无法指出错误的来源,因为我的代码看起来很干净。我会很感激一些帮助

Private Sub searchButton_click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles searchButton.Click
    registration.Visible = True
    con.Open()
    cm.Connection = con
    cm.CommandText = "SELECT * FROM records WHERE phone = " & phoneTextBox.Text
    cm.ExecuteNonQuery()
    myDA.SelectCommand = cm
    myDA.Fill(myDataSet, "records")

    If myDataSet.Tables("records").Rows.Count = 0 Then
        TextBox1.Text = ""
        TextBox2.Text = ""
        TextBox3.Text = ""
        TextBox4.Text = ""
        TextBox5.Text = ""
        TextBox6.Text = ""
        Response.Write("Record not Found")
    Else
        TextBox1.Text = myDataSet.Tables("Records").Rows(0).Item("phone")
        TextBox2.Text = myDataSet.Tables("Records").Rows(0).Item("name")
        TextBox3.Text = myDataSet.Tables("Records").Rows(0).Item("id")
        TextBox4.Text = myDataSet.Tables("Records").Rows(0).Item("pin")
        TextBox5.Text = myDataSet.Tables("Records").Rows(0).Item("area")
        TextBox6.Text = myDataSet.Tables("Records").Rows(0).Item("subscription")
    End If
End Sub

【问题讨论】:

  • 首先调试并找出发生在哪一行
  • 你的代码中声明的变量myDA在哪里?您正在将其设置为某些东西,但您没有事先声明其数据类型

标签: asp.net vb.net search sdf


【解决方案1】:

你的错误在这里

          cm.CommandText = "SELECT * FROM records WHERE phone = " +& phoneTextBox.Text

电话号码是字符串或 varchar 字段,这意味着它们必须放在“引号”中。因为您还没有执行查询并返回空结果(0 行)。当您尝试访问第一行时,即 row[0],因为没有行,您会收到 nullrefrence 异常

像这样修改你的代码

     cm.CommandText = "SELECT * FROM records WHERE phone = '" +phoneTextBox.Text+"'"

【讨论】:

  • 我已修改,错误依旧
  • 它说这一行是源 myDA.SelectCommand = cm
  • 可以使用 executenonquery 或 dataadapter.fill 方法。不要同时使用。尝试评论 cm.executenonquery()
  • 它仍然出现错误..问题可能出在我的导入上吗?
猜你喜欢
  • 1970-01-01
  • 2011-11-02
  • 2017-07-23
  • 2012-09-07
  • 2018-10-09
  • 2012-01-31
  • 1970-01-01
  • 2011-06-06
  • 1970-01-01
相关资源
最近更新 更多