Results 1 to 2 of 2

Thread: system.nullreferenceexception:object reference not set to an instance at program.main

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Mar 2007
    Posts
    208

    system.nullreferenceexception:object reference not set to an instance at program.main

    i am using C# desktop application and ms sql server database.
    i can run my project in the local pc when i run it from my project or from the .exe file in the debug folder. it runs fine.
    but when i copy the .exe file from debug or release folder and put it in other folder even in the same pc.. it show me error

    Code:
    system.nullreferenceexception:object reference not set to an instance of an object.
    at Form1..ctor()
     at program.main()

    where form1 is the main form of my application.

    please help me
    Code:
     public partial class Form1 : Form
        {
            
             SqlConnection dbcon = new SqlConnection(ConfigurationManager.ConnectionStrings["rentaldbcon"].ConnectionString);
          
            public Form1()
            {
              
                InitializeComponent();
             
               
            }
    Code:
    static class Program
        {
            /// Program.cs
            [STAThread]
            static void Main()
            {
               
                try
                {
                    Application.EnableVisualStyles();
                    Application.SetCompatibleTextRenderingDefault(false);
                    Application.Run(new Form1());
                }
                catch (Exception ex)
                {
                  MessageBox.Show(ex.ToString());
                }
            }
    help me please

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: system.nullreferenceexception:object reference not set to an instance at program.

    Are you copying the config file with the EXE? That connection string your loading is stored in the config file so, if there's not config file, there's no connection string.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width