Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C# 1.0 > C#
|
C# Programming questions specific to the Microsoft C# language. See also the forum Beginning Visual C# to discuss that specific Wrox book and code.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C# section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old October 17th, 2007, 12:02 AM
Registered User
 
Join Date: May 2006
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default Problem when SqlConnection.Open()

When I try to open a connection to the database I hit an error
"Object reference not set to an instance of an object."

I have make a simple test connection function same error coming out when open connection as the line in red. Here my function:

Code:
private void butConnect_Click(object sender, System.EventArgs e)
{
    SqlConnection connect = null;
    try
    {
        connect = new SqlConnection();
        connect.ConnectionString = "Data Source=(local);Initial Catalog=DB;User ID=sa;Password=password";

        connect.Open();
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message);
    }
}

My program is in .NET 1.1. Dunno why the error occur, it work fine previously. Thank you for someone help.
 
Old October 17th, 2007, 01:08 AM
Authorized User
 
Join Date: Apr 2007
Posts: 23
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Try by giving an if condition to test the connection's state....
Code:
private void butConnect_Click(object sender, System.EventArgs e) {
    SqlConnection connect = null;
    try
    {
        connect = new SqlConnection();
        connect.ConnectionString = "Data Source=(local);Initial Catalog=DB;User ID=sa;Password=password";
if(connect.Connectionstate == Connectionstate.Close)
        connect.Open();
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message);
    }
}

Gunjan
 
Old October 17th, 2007, 02:18 AM
joefawcett's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

If connect is not set to an instance then testing its state won't work either. The error message you're getting doesn't seem possible with the code you have shown, are you sure you haven't misssed anything out?

--

Joe (Microsoft MVP - XML)
 
Old October 17th, 2007, 01:02 PM
Registered User
 
Join Date: May 2006
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Really that happen. You can remote into my pc and see the problem.

Regarding the State, my original code is doing that by checking the Close state. Because of the connection is closed so when come to the connect.Open(), it return exception.
 
Old October 18th, 2007, 02:38 AM
joefawcett's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

Okay, how about you zip the project and email it to me? You can remove any passwords from the connection string as I'll use my own database. All I'm saying is that if connect was not set to an instance then the code would fail when you tried to set the ConnectionString property, not on the line you show.


--

Joe (Microsoft MVP - XML)
 
Old October 19th, 2007, 01:11 PM
Registered User
 
Join Date: May 2006
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi Joe, I dont mind to send you the code, actually it just as simple as I show you on this topic, just the few lines.

Since the problem still there, I had reinstall my .NET Framework 1.1 and test the program connection again, it work.
Anyway dunno why after test few times the connection throw the "Object reference..." error again.

Then I reinstall framework 1.1 again, it works again. SIGH...

As a result, I think I will need to hardworking on reinstall framework 1.1 if error occur again.......:(
 
Old June 27th, 2008, 12:36 AM
Registered User
 
Join Date: Jul 2007
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I also get the same error.
Please check

http://forums.msdn.microsoft.com/de-...-3deeac83e7e8/

 
Old July 6th, 2008, 07:43 PM
Registered User
 
Join Date: Jul 2008
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default

It seems to me that your connection string must be wrong. Can you connect to the database with that string using Access for instance?






Similar Threads
Thread Thread Starter Forum Replies Last Post
open connection problem MunishBhatia SQL Server 2005 0 October 5th, 2007 04:24 AM
SqlConnection angelboy C# 2005 2 March 19th, 2007 09:05 AM
SqlConnection Y.Hashmi ADO.NET 3 September 23rd, 2006 05:22 AM
about sqlConnection LeoG BOOK: Professional C#, 2nd and 3rd Editions 0 April 5th, 2006 02:01 PM
window.open() problem liorlankri ASP.NET 1.x and 2.0 Application Design 3 December 22nd, 2004 09:38 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.