-1

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.

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
Uwe Keim
  • 36,867
  • 50
  • 163
  • 268
Neqia
  • 1
  • 1
  • 5
  • help me to fix this..i'm new in asp.net. by the way i'm have search n search and make try.. but all not working – Neqia May 21 '19 at 00:51
  • Do you understand what a NullReferenceException is? The first step is to understand the exception you are receiving. https://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it. – Andrew May 21 '19 at 00:55
  • Please show all relevant code. – Mr. Tripodi May 21 '19 at 01:16
  • Now that you have identified the line in error. let's set a breakpoint and debug it. I guess, Request.QueryString("ID") is Nothing. – manabu May 21 '19 at 01:51
  • 1
    Possible duplicate of [What is a NullReferenceException, and how do I fix it?](https://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – Blackwood May 21 '19 at 03:35

1 Answers1

1

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

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.

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.

    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
Mary
  • 12,341
  • 3
  • 17
  • 26
  • what if nothing was returned, would `cmd.ExecuteScalar()` not be nothing? causing the same NullRefEx? In your code I may declare `Format` (using a different name) As Object, initialize it as nothing, set it to `cmd.ExecuteScalar()` (without ToString.Trim) then test it for nothing before trying to call the .ToString method – Mr. Tripodi May 21 '19 at 03:08
  • So if `cmd.ExecuteScalar()` was nothing you would not get an exception using the .ToString method? `cmd.ExecuteScalar().ToString` – Mr. Tripodi May 21 '19 at 03:19
  • @Mr.Tripodi Saw that right after I posted a correction. Do you think I have it now? – Mary May 21 '19 at 03:25
  • yup, thats how I would do it. – Mr. Tripodi May 21 '19 at 03:28
  • i need all data called by id shown, but i got error – Neqia May 21 '19 at 03:51
  • Now that you have identified the line in error. let's set a breakpoint and debug it. I guess, Request.QueryString("ID") is Nothing. its true..nothing but in the database already have a data..how to i call it by ID – Neqia May 21 '19 at 03:52
  • @Neqia "i need all data called by id shown" If you need all the data why are you only using one field from one row? Is that second comment directed at my answer? If you got an error with my answer, please tell me what the error is and what line caused it. – Mary May 21 '19 at 04:01
  • @Neqia I have not the slightest idea what you are talking about. – Mary May 21 '19 at 04:17
  • @Mary sorry my mistake.. actually i using textbox and call from database to fill in the textbox based on ID.. but i got a null error in this line "format_type_id = Request.QueryString("ID").ToString.Trim" – Neqia May 21 '19 at 04:43
  • I am sorry I couldn't help more but I do not know why Request.QueryString is failing. – Mary May 21 '19 at 05:07
  • @Mary its ok Mary..bytheway Thanks for helping – Neqia May 21 '19 at 05:26