DropDownList帮助中的DBNull [英] DBNull on DropDownList Help

查看:63
本文介绍了DropDownList帮助中的DBNull的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下午好



我希望有人可以提供协助,因为我在这里明显遗漏了一些东西。



基本上我在网页中查看时有一个类别的下拉列表,并选择一个类别的作业,它会出现错误



例外类型'System.ArgumentOutOfRangeException'在System.Web.dll中发生但未在用户代码中处理



我的代码如下,它指的是DDCat行/ br / >


Good Afternoon

I'm hoping someone can assist as I'm obviously missing something here.

Basically I have a drop down list of a category when viewing in the webpage and selecting a job with a category its coming up with an error

An exception of type 'System.ArgumentOutOfRangeException' occurred in System.Web.dll but was not handled in user code

My code is below and it refers to the line DDCat

Protected Sub DDOFIREF_SelectedIndexChanged(sender As Object, e As EventArgs) Handles DDOFIREF.SelectedIndexChanged

      'LblRef.Text = DDOFIREF.Text

      Dim dv As New Data.DataView 'use the id of your SqlDataSource below'
      dv = AccessDataSource3.Select(DataSourceSelectArguments.Empty)
      LblUser.Text = dv.Table.Rows(0)("UserN")
      LblDept.Text = dv.Table.Rows(0)("Dept")
      lblroot.Text = dv.Table.Rows(0)("Root")
      lblassign.Text = dv.Table.Rows(0)("AssignTo")
      txtdetails.Text = dv.Table.Rows(0)("Details")
      txtctc.Text = dv.Table.Rows(0)("CostCompany")
      lblref2.Text = dv.Table.Rows(0)("ref")
      DDCat.Text = If(IsDBNull(dv.Table.Rows(0)("Category")), DDCat.SelectedItem.ToString = "Select one", dv.Table.Rows(0)("Category"))
      txtact.Text = If(IsDBNull(dv.Table.Rows(0)("Action")), String.Empty, dv.Table.Rows(0)("action"))
      txtofi.Text = If(IsDBNull(dv.Table.Rows(0)("OfiNotes")), String.Empty, dv.Table.Rows(0)("OfiNotes"))





我们非常感谢任何帮助。



谢谢



Any help would be greatly appreciated.

Thanks

推荐答案

每当从数据库获取数据时,在尝试使用它之前,应始终检查查询是否返回了某些内容。例如
Whenever you are getting data from a database you should always check that the query has returned something before attempting to use it. For example
Protected Sub DDOFIREF_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) Handles DDOFIREF.SelectedIndexChanged

    'LblRef.Text = DDOFIREF.Text

    Dim dv As New Data.DataView 'use the id of your SqlDataSource below'
    dv = AccessDataSource3.Select(DataSourceSelectArguments.Empty)

    If dv.Table.Rows.Count > 0 Then
        LblUser.Text = dv.Table.Rows(0)("UserN")
        LblDept.Text = dv.Table.Rows(0)("Dept")
        lblroot.Text = dv.Table.Rows(0)("Root")
        lblassign.Text = dv.Table.Rows(0)("AssignTo")
        txtdetails.Text = dv.Table.Rows(0)("Details")
        txtctc.Text = dv.Table.Rows(0)("CostCompany")
        lblref2.Text = dv.Table.Rows(0)("ref")
        DDCat.Text = If(IsDBNull(dv.Table.Rows(0)("Category")), DDCat.SelectedItem.ToString = "Select one", dv.Table.Rows(0)("Category"))
        txtact.Text = If(IsDBNull(dv.Table.Rows(0)("Action")), String.Empty, dv.Table.Rows(0)("action"))
        txtofi.Text = If(IsDBNull(dv.Table.Rows(0)("OfiNotes")), String.Empty, dv.Table.Rows(0)("OfiNotes"))
    End If

End Sub



简单地说,如果没有返回任何行,那么行( 0)不存在(也不是行(1),行(2)等) - 并且有异常被抛出。



值得检查一下如何配置AccessDataSource控件 - 尝试直接对数据库运行查询,看看你得到了什么。


Simply put, if there are no rows returned then Rows(0) does not exist (nor Rows(1), Rows(2) etc) - and there is your exception being thrown.

It's worth checking how you have configured the AccessDataSource control - try running the query directly against the database to see what you get back.


这篇关于DropDownList帮助中的DBNull的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆