Solved

Object reference not set to an instance of an object. - ASP.NET using C# codebehind

Posted on 2006-07-20
6
325 Views
Last Modified: 2008-01-09
Hi, I have got this code: (runs fine in vb.net)

I have written a web app in asp.net, here is the code:
        SQLAdapter.Fill(SQLTable)

        Dim cb As New SqlCommandBuilder(SQLAdapter)
        For Each dr As DataRow In dt.Rows
            Dim ndr As DataRow = SQLTable.NewRow
            ndr.ItemArray = dr.ItemArray
            SQLTable.Rows.Add(ndr)
        Next

        SQLAdapter.Update(SQLTable)

        SQLAdapter.Fill(SQLDataSet)

        DatabaseGrid.DataSource = SQLDataSet.Tables(0)
        DatabaseGrid.DataBind()


Its gives me error on this line (LINE NO 37), i can't understand how to fix this:

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

Source Error:


Line 35:
Line 36:         Dim cb As New SqlCommandBuilder(SQLAdapter)
Line 37:         For Each dr As DataRow In dt.Rows (error is coming on this line)
Line 38:             Dim ndr As DataRow = SQLTable.NewRow
Line 39:             ndr.ItemArray = dr.ItemArray
 

Source File: C:\My Work\MyDotNetProjects\PlanningTool_Ver1\TimeTracker\UploadBudgetFile.aspx.vb    Line: 37

Stack Trace:


[NullReferenceException: Object reference not set to an instance of an object.]
   TimeTracker_UploadBudgetFile.btnExport_Click(Object sender, EventArgs e) in C:\My Work\MyDotNetProjects\PlanningTool_Ver1\TimeTracker\UploadBudgetFile.aspx.vb:37
   System.Web.UI.WebControls.Button.OnClick(EventArgs e) +75
   System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +98
   System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +7
   System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +11
   System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +33
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +4920

Can anyone pls help.

Regards

 

0
Comment
Question by:radhakrishan
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 24

Accepted Solution

by:
Jeff Certain earned 500 total points
ID: 17147118
dt has not been set to a value

You might want to replace dt with SQLTable
0
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 17147133
What is "dt" ? Its not defined anywhere in the code you posted.
0
 

Author Comment

by:radhakrishan
ID: 17147167
I have got dt defined in a private function like this:

    Private Sub btnImport_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnImport.Click
        dt = Excel.Import.Query(FileUpload1.PostedFile.FileName, "C5:BE79")
        ExcelDataSet.Tables.Add(dt)
        ExcelGrid.DataSource = ExcelDataSet.Tables(0)
        ExcelGrid.DataBind()
        lblExcel.Text = "Excel file - No. of Rows/Columns : " & ExcelDataSet.Tables(0).Rows.Count() & "-" & ExcelDataSet.Tables(0).Columns.Count()
    End Sub

And I have got EXPORT buttons which has got this code:
    Private Sub btnExport_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnExport.Click
        dt = Excel.Import.Query("C:\New Budget.xls", "C5:BE79")
        If dt.Rows.Count > 0 Then
            SQLAdapter.Fill(SQLTable)

            Dim dr As DataRow

            Dim cb As New SqlCommandBuilder(SQLAdapter)
            For Each dr In dt.Rows
                Dim ndr As DataRow = SQLTable.NewRow
                ndr.ItemArray = dr.ItemArray
                SQLTable.Rows.Add(ndr)
            Next

            SQLAdapter.Update(SQLTable)

            SQLAdapter.Fill(SQLDataSet)

            DatabaseGrid.DataSource = SQLDataSet.Tables(0)
            DatabaseGrid.DataBind()
        End If

    End Sub



Any suggestions, do I need mention "Dt" somewhere else ?

Regards
0
Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

 
LVL 52

Expert Comment

by:Carl Tawn
ID: 17147214
Not to familiar with interop with Excel. What type of object does Excel.Import.Query return ?
0
 

Author Comment

by:radhakrishan
ID: 17147315
Whole of this codes works in vb.net application where I import data from Excel file.

Its also working fine here, but when im export data from excel to DATABASE on our server that's where Im having problem.

Regards
0
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 17147355
In that case it suggests that there is a problem with the Excel file and the "Query" method is returning NULL.

Maybe there is a problem locating the file or a permissions error, or one of a multitude of other problems. If you can, add code to check if "dt" is null after the call to Excel.Import.Query.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question