Solved

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

Posted on 2006-07-20
6
321 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
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Article by: Kraeven
Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

747 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

12 Experts available now in Live!

Get 1:1 Help Now