473,406 Members | 2,867 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Problem with SqlConnection.Open()

5
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 1393
Shashi Sadasivan
1,435 Expert 1GB
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=.\SQLEXPRESS

change accordingly for your servers instance name

cheers
Oct 17 '07 #2
hoetat
5
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 Expert 1GB
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
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 Expert 1GB
Could you please check which line gives that exception, and which object is staying null !

cheers
Oct 17 '07 #6
hoetat
5
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 Expert 1GB
So you mean to say that the line which sets the value for connect.ConnectionString 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
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
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...
1
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....
1
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...
23
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...
1
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. ...
0
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...
4
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....
5
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...
2
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...
2
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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...
0
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,...

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.