Solved

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

Posted on 2006-07-20
6
324 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
  • 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
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Programmatically signing Word macros 4 72
Extracting last characters in a substring 5 26
VB.Net - Windows UserName index and length error 4 29
VB.NET 2008 Publish Error 2 24
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…

816 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now