4 Replies - 1040 Views - Last Post: 16 March 2011 - 12:33 PM Rate Topic: -----

#1 privatejarhead  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 16-March 11

problem creating an Excel sheet from a VB.NET application...

Posted 16 March 2011 - 06:07 AM

hello everyone, first post here as a new member. I have a pet project of mine that I'm working on; basically, it's for scouts during FIRST FRC matches and allows them to enter data about teams into an Excel sheet. I'm having a problem creating said sheet, however. I want the program to create a new sheet, with the filename being whatever the user puts in a textbox I have. the interface takes data about teams one team at a time; when user wants to add a new team, they press "Enter Data" (which puts whatever is in the interface on Row x, then the user will be able to add new data to Row x+1 each time they press said button, etc). The main issue is that when I have the code as follows:

Public Class ScoutFormMain

    Dim oXL As Excel.Application
    Dim oWB As Excel.Workbook
    Dim oSheet As Excel.Worksheet
    Dim oRng As Excel.Range

    'THIS CAUSES THE ERRORS
    'define the filename of the Excel sheet, as defined by the user.
    Dim filename As String = txtFile.Text
    Dim filepath As String = "C:\Users\%username%\Desktop\" & txtFile.Text & ".xls"
...........



As you can see, I have the Excel stuff and the two variables as global variables, whereas the rest of the Excel-related code is under the "Private Sub btnEnterData" procedure that I have later on in the code. When i run the program from the IDE, it gives me this error:

System.InvalidOperationException was unhandled
  Message=An error occurred creating the form. See Exception.InnerException for details.  The error is: Object reference not set to an instance of an object.
  Source=ScoutForm
  StackTrace:
       at ScoutForm.My.MyProject.MyForms.Create__Instance__[T](T Instance) in 17d14f5c-a337-4978-8281-53493378c1071.vb:line 190
       at ScoutForm.My.MyProject.MyForms.get_ScoutFormMain()
       at ScoutForm.My.MyApplication.OnCreateMainForm() in C:\Users\Robert\Documents\My Dropbox\ScoutForm\ScoutForm\My Project\Application.Designer.vb:line 35
       at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.OnRun()
       at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.DoApplicationModel()
       at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.Run(String[] commandLine)
       at ScoutForm.My.MyApplication.Main(String[] Args) in 17d14f5c-a337-4978-8281-53493378c1071.vb:line 81
       at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
       at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
       at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
       at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Threading.ThreadHelper.ThreadStart()
  InnerException: System.NullReferenceException
       Message=Object reference not set to an instance of an object.
       Source=ScoutForm
       StackTrace:
            at ScoutForm.ScoutFormMain..ctor() in C:\Users\Robert\Documents\My Dropbox\ScoutForm\ScoutForm\ScoutFormMain.vb:line 72
       InnerException: 



I've narrowed the problem to the "filename" and "filepath" variables. I've Googled around for answers, but the articles that I found about the error above are too vague to help me. Does anyone know why VB.NET is giving me this error? Also, how can I reuse the same Excel sheet for the entire duration that my VB.NET application is running? As in, I want to be able to add ~50 or so teams to the same Excel document (for example, all of it going into "MyRegional.xls").

Sorry if this is an obvious problem to solve or anything; I'm fairly new to programming in general, so I'm clueless as to what's going on with this. Up until this point, my program runs fine and when I comment out "Dim filename..." and "Dim filepath...", the program will run normally. If it helps, I'm running VB.NET Express 2010 in Windows 7 Home. Thank you SO much in advance =)

Is This A Good Question/Topic? 0
  • +

Replies To: problem creating an Excel sheet from a VB.NET application...

#2 raziel_  Icon User is offline

  • Like a lollipop
  • member icon

Reputation: 465
  • View blog
  • Posts: 4,255
  • Joined: 25-March 09

Re: problem creating an Excel sheet from a VB.NET application...

Posted 16 March 2011 - 06:43 AM

Perhaps the problem is in the textbox. Are you sure that this textbox exist in this form?

This post has been edited by NoBrain: 16 March 2011 - 06:43 AM

Was This Post Helpful? 0
  • +
  • -

#3 Cyclopses  Icon User is offline

  • New D.I.C Head

Reputation: 5
  • View blog
  • Posts: 38
  • Joined: 20-January 11

Re: problem creating an Excel sheet from a VB.NET application...

Posted 16 March 2011 - 07:17 AM

Dim filepath As String = "C:\Users\%username%\Desktop\" & txtFile.Text & ".xls"

Gets executed at run-time, this means that the txtFile.text property you're calling is empty, because there is nothing there yet.

Before the Form starts initializing it's objects (Like txtFile) it runs down your code path you just showed us, this means there is no textbox yet, and you're calling the text from it, that's gonna give you an error.

You have to assign the value to the string at a later date. For example, if you had a button, and opened the 'click' event, put the same line in there, it should work.

If I give a bad explanation excuse me for that, am a fairly new member of DIC.

Good luck! If you had any questions, ask them!
Was This Post Helpful? 1
  • +
  • -

#4 raziel_  Icon User is offline

  • Like a lollipop
  • member icon

Reputation: 465
  • View blog
  • Posts: 4,255
  • Joined: 25-March 09

Re: problem creating an Excel sheet from a VB.NET application...

Posted 16 March 2011 - 12:11 PM

hmmm perhaps your right. in form load the controls are still not loaded. If Cyclopses is true then put your code in Form_Shown() event
Was This Post Helpful? 0
  • +
  • -

#5 privatejarhead  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 16-March 11

Re: problem creating an Excel sheet from a VB.NET application...

Posted 16 March 2011 - 12:33 PM

View PostCyclopses, on 16 March 2011 - 07:17 AM, said:

Dim filepath As String = "C:\Users\%username%\Desktop\" & txtFile.Text & ".xls"

Gets executed at run-time, this means that the txtFile.text property you're calling is empty, because there is nothing there yet.

Before the Form starts initializing it's objects (Like txtFile) it runs down your code path you just showed us, this means there is no textbox yet, and you're calling the text from it, that's gonna give you an error.

You have to assign the value to the string at a later date. For example, if you had a button, and opened the 'click' event, put the same line in there, it should work.

If I give a bad explanation excuse me for that, am a fairly new member of DIC.

Good luck! If you had any questions, ask them!



Thank you so much. I forgot that "filename/path" was ran before the actual code...It all works now. I brought the code to my programming teacher before with the problem and we couldn't figure it out (since apparently this sort of error code could mean anything).
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1