Thread: SQL Connection (Exception fail)

  1. #1
    Registered User
    Join Date
    Sep 2015
    Posts
    18

    SQL Connection (Exception fail)

    When trying to connect to sql through sqlconnection I get an error, "NullReferenceException was unhandled" "An unhandled exception of type 'System.NullReferenceException' occurred in DBLabControllers.dll
    Additional information: Objektreferensen har inte angetts till en instans av ett objekt."
    Why do I get exception fail and how do I fix it?

    Code:
    
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Data.SqlClient;
    using System.Data;
    using System.Windows.Forms;
    
    namespace DBLabs
    {
    
    
        public class DBConnection : DBLabsDLL.DBConnectionBase
        {
    
    
            public DBConnection()
            {
                string connectionString = null;
                SqlConnection connection;
                connectionString = "Data Source=(local);Initial Catalog=www3.idt.mdh.se;User ID=ezi15001;Password=********";
    
                connection = new SqlConnection(connectionString);
            }
    
    
            public override bool login(string username, string password)
            {
    
    
                string constring = "Data Source = www3.idt.mdh.se;" + "Database=" + username + "_db;" + "User ID=" + username + ";" + "Password=" + password;
                SqlConnection myConnection = new SqlConnection(constring);
                try
                {
                    myConnection.Open();
                }
                catch (Exception)
                {
                    myConnection.Close();
                    return false;
                }
                finally
                {
                    myConnection.Close();
                }
                return true;
    
            }
        }

  2. #2
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    What's DBLabControllers.dll? What line in the code you pasted is in the stack trace for the exception?
    If you understand what you're doing, you're not learning anything.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,176
    I suppose the first thing to do is run the code in the debugger.

    The exception will be caught at the moment it is raised, giving you the opportunity to look around and get more information.

    More information than "it doesn't work".
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 04-15-2015, 09:29 AM
  2. Exception errors OR exception handling :S
    By cruiser in forum C++ Programming
    Replies: 4
    Last Post: 09-02-2011, 05:30 AM
  3. fail to count digit of an integer (fail at 9)
    By azsquall in forum C++ Programming
    Replies: 3
    Last Post: 05-02-2008, 09:42 AM
  4. Handle C++ exception and structured exception together
    By George2 in forum C++ Programming
    Replies: 2
    Last Post: 01-24-2008, 09:21 PM
  5. cin.fail
    By bj31t in forum C++ Programming
    Replies: 3
    Last Post: 04-01-2004, 03:26 PM

Tags for this Thread