Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 2006-07-20
6
Medium Priority
?
337 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 1500 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
Independent Software Vendors: 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!

 
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

Hire Technology Freelancers with Gigs

Work with 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

This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…

598 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