473,795 Members | 3,024 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Problem with SqlConnection.O pen()

5 New Member
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 9 "connect.Open() ;". Here my function:

Expand|Select|Wrap|Line Numbers
  1. private void butConnect_Click(object sender, System.EventArgs e)
  2. {
  3.     SqlConnection connect = null;
  4.     try
  5.     {
  6.         connect = new SqlConnection();
  7.         connect.ConnectionString = "Data Source=(local);Initial Catalog=DB;User ID=sa;Password=password";
  8.  
  9.         connect.Open();
  10.     }
  11.     catch (Exception ex)
  12.     {
  13.         MessageBox.Show(ex.Message);
  14.     }
  15. }
My program is in .NET 1.1. Dunno why the error occur, it work fine previously. Thank you for someone help.
Oct 17 '07 #1
8 1411
Shashi Sadasivan
1,435 Recognized Expert Top Contributor
Hi
I have never used the (local) context for Datasource

Try using the follwing and directing it to the instance name
Expand|Select|Wrap|Line Numbers
  1. connect.ConnectionString = "Data Source=.\<SQL server instance>;Initial Catalog=DB;User ID=sa;Password=password";
The default instance name for sql express is SQLEXPRESS
so make the value of Data Source=.\SQLEXP RESS

change accordingly for your servers instance name

cheers
Oct 17 '07 #2
hoetat
5 New Member
I am using MSSQL Server 2000. I have try so, even how I use in the Data Source neither server name nor IP still come out the same error.
Oct 17 '07 #3
Shashi Sadasivan
1,435 Recognized Expert Top Contributor
1. Are you able to open the database in the management studio?
2. create a new connection string from visual studio and copy that connection string into your current one. (easy way out)
Oct 17 '07 #4
hoetat
5 New Member
Enterprise Manager no problem the database can be access or modify. Query Analyzer run script also ok.

My connection string is generate by Visual Studio and the string I had use for many program.
Oct 17 '07 #5
Shashi Sadasivan
1,435 Recognized Expert Top Contributor
Could you please check which line gives that exception, and which object is staying null !

cheers
Oct 17 '07 #6
hoetat
5 New Member
The line that cause the problem is number 9 "connect.Open() ;" the line in bold.
When run thought the "connect.Open() ;" it will direct to the Exception and return "Object reference ......".

I wonders why just a simple open connection function cause me waste lot of time. SIGH.
Oct 17 '07 #7
Shashi Sadasivan
1,435 Recognized Expert Top Contributor
So you mean to say that the line which sets the value for connect.Connect ionString works perfectly, but when it comes to open, the connect suddenly goes null?
Hmmm.... Hows that possible?
Hope someone can put some light on this one...

cheers
Oct 17 '07 #8
hoetat
5 New Member
Really that happen just when open();
Firewall had been disable.
When test with other computer no problem.
Test open connection in Visual Studio 2005 is ok.
I wonders is it my Visual Studio 2003 problem. If yes what is the problem?

SIGH SIGH....
Oct 17 '07 #9

Sign in to post your reply or Sign up for a free account.

Similar topics

0
5380
by: Bob | last post by:
I have an ASP.NET web application that has been running without any problems for a while. I recently transferred the site to shared hosting and had multiple users start to use the site. The problem I'm experiencing is that when many users are hitting the site at once, occasionaly I will see errors. The 3 most common ones are: "The SqlCommand is currently busy Open, Fetching. ", "Internal Connection Fatal Error", "object reference not set to...
1
2526
by: Joe Bloggs | last post by:
I am trying display the contents of a table in a web page, select certain rows from that table and then display the fields that I have selected (now table columns) as text in a Label object. Amazingly I have managed to display the table - no problem, I can select rows - no problem, BUT I can't work out how to display more than one of the fields (now columns in the table in the web page). I can display one field OK, but not more than one...
1
2229
by: Skip Key | last post by:
While writing a windows service, I've run into a fairly strange problem. I have a worker proc that gets fired every n seconds using System.Threading.Timer. This works fine, unless I open an SqlConnection on it. Just as soon as I do so, the proc fires that time, and never again. The connection is valid, I can do SQL commands on in with no problems, but my timer goes away. Here's a fairly simple console sample that shows the problem....
23
3083
by: VB Programmer | last post by:
Variable scope doesn't make sense to me when it comes to Try Catch Finally. Example: In order to close/dispose a db connection you have to dim the connection outside of the Try Catch Finally block. But, I prefer to dim them "on the fly" only if needed (save as much resources as possible). A little further... I may wish to create a sqlcommand and datareader object ONLY if certain conditions are met. But, if I want to clean these up in the...
1
1703
by: mhnazly | last post by:
i'm trying to read data from SQL Server database using data reader and assigned it to a label in my asp.net web application. but when the button is clicked, nothing appears. please help, thanks. Private Sub btnTesting_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnTesting.Click Dim cn As New SqlClient.SqlConnection Dim cm As New SqlClient.SqlCommand
0
2442
by: Nithin | last post by:
My code as an txt attachment. I have 2 drop down list boxes that on selection populate text boxes from my database table. I am able to display the correct values in these text boxes. I have 2 questions: 1) I would like the user update these text fields that get populated with data from the database. For some reason I get the error when I change the textbox value.
4
2093
by: Bass Pro | last post by:
Hi, I am creating textbox, radiobuttonlist and checkboxlist dynamically depending on data from a table. It is a questionnaire. I add the control on a Panel control during the 1st load_page event. Each question is displayed and answered, then result written to a SQL table. Then the next question is read from a table and displayed using the load_page event again. The questions display and function perfectly. The user anwers the question...
5
3399
by: Usman Jamil | last post by:
Hi I've a class that creates a connection to a database, gets and loop on a dataset given a query and then close the connection. When I use netstat viewer to see if there is any connection open left, I always see that there are 2 connections open and in "ESTABLISHED" state. Here is the piece of code that I'm using, please tell where I'm doing it wrong. Since this class is being used at many placed in my actual web based application that...
2
3338
by: sweatha | last post by:
Hi friends This is sweatha, fresher MCA 2007 batch. Right now I am working in asp.net platform. My dillema is that I have created a registration form with the fields as username & password. And if once the user register the username and password, it will be stored in the database(SQL Server 2000) by showing "Records Inserted". Then I have created the login form just like the fields as username and password.And if the user enters the values...
2
2640
by: sirdavethebrave | last post by:
Hi guys - I have written a form, and a stored procedure to update the said form. It really is as simple as that. A user can go into the form, update some fields and hit the update button to update the information which is stored in a SQL database. In testing we noticed that the form was updating correctly but the update mechanism was also updating the first record of the table in the sql database every time. No error messages are on...
0
10439
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10165
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10001
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9043
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6783
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5437
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5563
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4113
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2920
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.