0

I have a windows forms application which begins with the Login form The Login form has been fine for past few day while i was working on rest of the application I get an error now that I have two database one DB.mdf and one MYD.sdf

NullReferenceException was unhandled

Object reference not set to an instance of an object.

for this particular lines of code --- >

private void button1_Click(object sender, EventArgs e)
    {
        string path=@"C:\Users\Srinath\Documents\Visual Studio 2010\Projects\TESTFEE\TESTFEE\DB.mdf";
SqlConnection con =new SqlConnection(@"Data Source=.\SQLEXPRESS; AttachDbFilename='"+path+"';User Instance=True");

            string constring=ConfigurationManager.ConnectionStrings["ConnectionStringFMS"].ConnectionString;
         //SqlConnection con=new SqlConnection(constring);
       
        SqlDataAdapter sda = new SqlDataAdapter("Select Count(*) from LOGIN where USERNAME='" + textUser.Text + "' and PASSWORD='" + textPass.Text + "'", con);
        DataTable dt = new DataTable();

        try
        {
            sda.Fill(dt);
            if (dt.Rows[0][0].ToString() == "1")
            {
                this.Hide();
                e1.Show();

            }
            else
            {

                HoldButton();
                MessageBox.Show("Please Enter Your Right Credentials");
            }

        }
        catch (SqlException ex)
        {

            MessageBox.Show(ex.Message);
        }
        
      
             
       
        }//![The error i get ][1 -]

I tried using the Configuration File for connection string before i directly used the SqlConnection for connection I am using Sql server 2008 r2 with the Management studio I first recieved the Failed to connect to the default database inititally Doubts - > is it the Problem because of using two different types of db in one application I tried reinstalling sql server 2008 but no use please help

Community
  • 1
  • 1
Srinath Naidu
  • 137
  • 2
  • 11
  • 2
    What line does the exception get thrown on and what reference on that line is null? I'm guessing that you have no connection string in the config file named ConnectionStringFMS but that's just a guess. – jmcilhinney Mar 19 '14 at 04:33
  • @jmcilhinney This is my reference in config file – Srinath Naidu Mar 19 '14 at 04:35
  • 1
    Almost all cases of `NullReferenceException` are the same. Please see "[What is a NullReferenceException in .NET?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-in-net)" for some hints. – John Saunders Mar 19 '14 at 04:36
  • @JohnSaunders So in my case what should i do ? I'm bit confused please guide me ! Thank you – Srinath Naidu Mar 19 '14 at 04:38
  • Learn to use a debugger. Find out what data is null, then find out why it is null, then **fix it**. – John Saunders Mar 19 '14 at 04:40
  • @user3334101 near `private void button1_Click(object sender, EventArgs e)` simply press `f9` and run again..It will tell you where error occurs – Nagaraj S Mar 19 '14 at 04:42
  • @NagarajS S It Throws an error on this line every time string constring=ConfigurationManager.ConnectionStrings["ConnectionStringFMS"].ConnectionString; – Srinath Naidu Mar 19 '14 at 04:47
  • Try removing your connection string from the config file and then add it again via the Settings page of the project properties. That way, you can access it via a typed property of My.Settings instead of having to use a magic string. – jmcilhinney Mar 19 '14 at 05:17
  • @jmcilhinney It is not working !!! I tried to connect with your Method but could not do it – Srinath Naidu Mar 19 '14 at 14:43

1 Answers1

0

Put your connection string in webconfig, If you have the *.mdf placed in App_Data folder, using this format works

<connectionStrings>
  <add name="ConnectionName"
    connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|DatabaseName.mdf;Integrated Security=True;User Instance=True"
    providerName="System.Data.SqlClient" />
</connectionStrings>
Sajeetharan
  • 216,225
  • 63
  • 350
  • 396