Click here to Skip to main content
15,166,728 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
Hello guys, can somebody help.

I am trying to select all from a table in a database (LOCALDB) of visual studio 2013.
But it gives me the above error. The error also points to the connection string. see my code below:
public void viewAllStaffMethod(object sender, RoutedEventArgs e)
           {
               var dGrid = new DataGrid { };


               DockPanel dockPanelmain = new DockPanel();
               dockPanelmain.Children.Add(dGrid);
               string conString=ConfigurationManager.ConnectionStrings[@"Data Source=(LocalDB)\v11.0;AttachDbFilename=G:\Applications\MOICIDB\MOICIDB\MOICIDB.mdf;Integrated Security=True"].ConnectionString;
               string cmdString=string.Empty;
               using( SqlConnection con=new SqlConnection(conString))
               {
                   cmdString=" select * from PersonnelDetails";
                   SqlCommand com= new SqlCommand(cmdString, con);
                   SqlDataAdapter sda=new SqlDataAdapter(com);
                   DataTable dt=new DataTable("PersonnelDetails");
                   sda.Fill(dt);
                   dGrid.ItemsSource=dt.DefaultView;

               }
           }


I am sorry i am a novice.. 1st is it the right way to connect to the localDB.
Posted
Comments
Sergey Alexandrovich Kryukov 10-Mar-14 10:05am
   
In what line?
—SA
Member 10645876 11-Mar-14 6:25am
   
in the connection string line

I am not i am doin the right thing. i am very novice in this. shouldnt i take a different approach

Usually a NullReferenceException[^] is pretty easy to find. Just run your program from Visual Studio, and step through each line and check what variable return null.

As for this code, this line probably returns null:
C#
ConfigurationManager.ConnectionStrings[@"Data Source=(LocalDB)\v11.0;AttachDbFilename=G:\Applications\MOICIDB\MOICIDB\MOICIDB.mdf;Integrated Security=True"]

The ConnectionStrings[^] property will allow you to get a connnection string based on a name. Now, unless the name you used in the configuration is Data Source=(LocalDB)\v11.0;AttachDbFilename=G:\Applications\MOICIDB\MOICIDB\MOICIDB.mdf;Integrated Security=True this is going to return null. What you should pass is the name as used in the configuration (the app.config or web.config file).
   
Comments
Sergey Alexandrovich Kryukov 10-Mar-14 10:11am
   
This is possible and likely, my 5. I added general recommendation for dealing with this kind of problem, please see my answer.
OP cannot ask questions every time such situations happen, it's important to develop the adequate debugging skills.
—SA
Member 10645876 11-Mar-14 8:00am
   
Maarten kools

Ok. then connectionString in App.config is
<pre>
Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\MOICIDB.mdf;Integrated Security=True"]
</pre>
but the one given in the database properties is
<pre> Data Source=(LocalDB)\v11.0;AttachDbFilename=G:\Applications\MOICIDB\MOICIDB\MOICIDB.mdf;Integrated Security=True
</pre>
but i think both are the same. Any way i hav used the one in the App.config but to no avail.
Plz note my database is localDB.

MAJ
Maarten Kools 11-Mar-14 8:23am
   
You can define your connection strings in the App.config file, and assign each a name. The idea of the ConfigurationManager.ConnectionStrings[string] property is that you can use that name to retrieve the actual connection string. So you either hardwire the connection string in your code (not a good idea), then you do either string conString = @"Data Source=(LocalDB)\v11.0;AttachDbFilename=G:\Applications\MOICIDB\MOICIDB\MOICIDB.mdf;Integrated Security=True" or you use the configuration, string conString = ConfigurationManager.ConnectionStrings["thenamethatisusedinappconfig"]. This example[^] explains step by step how it works and what to do.
Member 10645876 11-Mar-14 8:37am
   
thanks....
i used the first option u gave.

MAJ
You did not show where the exception with the message "Object reference not set to an instance of an object" is thrown.

Not to worry. This is one of the very easiest cases to detect and fix. It simply means that some member/variable of some reference type is dereferenced by using and of its instance (non-static) members, which requires this member/variable to be non-null, but in fact it appears to be null. Simply execute it under debugger, it will stop the execution where the exception is thrown. Put a break point on that line, restart the application and come to this point again. Evaluate all references involved in next line and see which one is null while it needs to be not null. After you figure this out, fix the code: either make sure the member/variable is properly initialized to a non-null reference, or check it for null and, in case of null, do something else.

Please see also: want to display next record on button click. but got an error in if condition of next record function "object reference not set to an instance of an object"[^].

Sometimes, you cannot do it under debugger, by one or another reason. One really nasty case is when the problem is only manifested if software is built when debug information is not available. In this case, you have to use the harder way. First, you need to make sure that you never block propagation of exceptions by handling them silently (this is a crime of developers against themselves, yet very usual). The you need to catch absolutely all exceptions on the very top stack frame of each thread. You can do it if you handle the exceptions of the type System.Exception. In the handler, you need to log all the exception information, especially the System.Exception.StackTrace:
http://msdn.microsoft.com/en-us/library/system.exception.aspx[^],
http://msdn.microsoft.com/en-us/library/system.exception.stacktrace.aspx[^].

The stack trace is just a string showing the full path of exception propagation from the throw statement to the handler. By reading it, you can always find ends. For logging, it's the best (in most cases) to use the class System.Diagnostics.EventLog:
http://msdn.microsoft.com/en-us/library/system.diagnostics.eventlog.aspx[^].

Good luck,
—SA
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900