vb.net 如何修复此错误“未将对象引用设置到对象的实例”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/56229773/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 20:26:29  来源:igfitidea点击:

How to fix this error "Object reference not set to an instance of an object"

asp.netsql-servervb.net

提问by Neqia

Server Error in '/' Application. Object reference not set to an instance of an object. Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

“/”应用程序中的服务器错误。你调用的对象是空的。说明:在执行当前 Web 请求期间发生未处理的异常。请查看堆栈跟踪以获取有关错误及其在代码中的来源的更多信息。

异常详细信息:System.NullReferenceException:未将对象引用设置为对象的实例。

Source Error:

源错误:

Line 844:        Dim dt As New DataTable
Line 845:
Line 846:        format_type_id = Request.QueryString("ID").ToString.Trim
Line 847:
Line 848:        'take the value from db so easy to maintain the format name

Stack Trace:

堆栈跟踪:

[NullReferenceException: Object reference not set to an instance of an object.]
Doccon.ESER_Doc_Form.get_format_type() in C:\Users\naquid9065\Documents\Q SMTT\Doccon\Doccon\ESER-Doc-Form.aspx.vb:846
Doccon.ESER_Doc_Form.Page_Load(Object sender, EventArgs e) in C:\Users\naquid9065\Documents\Q SMTT\Doccon\Doccon\ESER-Doc-Form.aspx.vb:57
System.Web.UI.Control.OnLoad(EventArgs e) +95
System.Web.UI.Control.LoadRecursive() +59
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +678

Code:

代码:

Private Sub get_format_type()
    Dim strSQL As String = ""
    Dim params As New Hashtable
    Dim dt As New DataTable

    format_type_id = Request.QueryString("ID").ToString.Trim (error from here)

    strSQL = "SELECT * FROM tb_setup_format_type WHERE status ='1' and ID = @ID "
    params.Add("@ID", format_type_id)
    Common.OpenConn()
    Common.execReader(strSQL, params, dt, Common.txn)
    If dt.Rows.Count <> 0 Then
        Me.txtformat.Text = dt.Rows(0)("Report_Format").ToString.Trim
    End If

    Common.CloseConn()
End Sub

回答by Mary

Procedures that are named Get... are usually functions. If you are getting something then it is returned.

名为 Get... 的过程通常是函数。如果你得到了一些东西,那么它就会被退回。

If you only need one piece of data don't pull down the whole record. You can use ExecuteScalar which returns the first column of the first row.

如果您只需要一条数据,请不要下拉整个记录。您可以使用 ExecuteScalar 返回第一行的第一列。

It is really easier to use ADO.net directly. Then you can keep track of your data objects local and ensure that they are closed and disposed even if there is an error. The Using...End Using blocks accomplish this.

直接使用ADO.net确实更容易。然后,您可以在本地跟踪您的数据对象,并确保即使出现错误,它们也已关闭和处理。Using...End Using 块实现了这一点。

    Private Sub FillFormatTextBox()
    Dim Format As Object = Nothing
    Dim strSQL = "SELECT Report_Format FROM tb_setup_format_type WHERE status ='1' and ID = @ID "
    Using cn As New SqlConnection("Your connection string")
        Using cmd As New SqlCommand(strSQL, cn)
            'Your code did not provide a value for the Id parameter
            cmd.Parameters.Add("@ID", SqlDbType.Int).Value = 4
            cn.Open()
            Format = cmd.ExecuteScalar()
        End Using
    End Using
    If Format Is Nothing Then
        MessageBox.Show("No format Found")
    Else
        txtformat.Text = Format.ToString.Trim
    End If
End Sub