?
Solved

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

Posted on 2006-07-20
6
Medium Priority
?
332 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
Suggested Courses
Course of the Month9 days, 5 hours left to enroll

765 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