Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: ASP.NET
error is "The ConnectionString property has not been initialized" cannot find what type of error is this and the error is coming on this connection.Open();
 

 
public class filedownload
    {
        private static string GetConnectionString()
        {
            return ConfigurationManager.AppSettings[@"Data Source=LENOVO-74FE9906\SQLEXPRESS;AttachDbFilename=D:\Farrukh\Orignal Project\Inventory Management System\IMS.mdf;Integrated Security=True"];
        }
 
        private static void OpenConnection(SqlConnection connection)
        {
            connection.ConnectionString = GetConnectionString();
            connection.Open(); // error in this line
            
        }
 
        public static DataTable GetFileList()
        {
            DataTable dt=new DataTable();
            using (SqlConnection con = new SqlConnection())
            {
                OpenConnection(con);
                SqlCommand com = new SqlCommand();
                com.Connection = con;
                com.CommandTimeout = 0;
                com.CommandText = "SELECT ID, Name, [Content], size FROM Connectivity";
                com.CommandType = CommandType.Text;
                SqlDataAdapter da = new SqlDataAdapter();
                da.SelectCommand = com;
                da.Fill(dt);
                con.Close();
 

            }
            return dt;
Posted 7-May-13 23:14pm
Edited 7-May-13 23:27pm
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Try this:
private static string GetConnectionString()
{
    return "data source=.\\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|\\IMS.mdf;User Instance=true";
}
private static void OpenConnection(SqlConnection connection)
{
    connection.ConnectionString = GetConnectionString();
    connection.Open(); 
}
Check the function here:
using (SqlConnection con = new SqlConnection())
{
    OpenConnection(con);
    SqlCommand com = new SqlCommand("SELECT ID, Name, [Content], size FROM Connectivity", con);
    SqlDataAdapter da = new SqlDataAdapter();
    da.SelectCommand = com;
    da.Fill(dt);
    con.Close();
}
 
Hope it helps!
--Amit
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

return ConfigurationManager.AppSettings[@"Data Source=LENOVO-74FE9906\SQLEXPRESS;AttachDbFilename=D:\Farrukh\Orignal Project\Inventory Management System\IMS.mdf;Integrated Security=True"];
 

in above statement you have used ConfigurationManager.AppSettings which is used to take values for any key defined in config file and takes key name as parameter in square bracket.
 
now you have provided whole connection string in square brackets but when this line of code will get executed, it will search for a key with name as provided in config file and as there is no any key with this name, it will return null and hence the error.
 
if you want to hardcode connection string here then don't use ConfigurationManager.Appsettings just use like below
 
return @"Data Source=LENOVO-74FE9906\SQLEXPRESS;AttachDbFilename=D:\Farrukh\Orignal Project\Inventory Management System\IMS.mdf;Integrated Security=True";
 
as a second case you can define a key in config file and put its name here in your code file. like below
 
return ConfigurationManager.AppSettings["ConfigKeyName"]
 

Hope it helps you.
  Permalink  

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

  Print Answers RSS
0 Maciej Los 295
1 OriginalGriff 249
2 Sergey Alexandrovich Kryukov 205
3 Aajmot Sk 197
4 Sinisa Hajnal 176
0 OriginalGriff 7,800
1 Sergey Alexandrovich Kryukov 7,072
2 DamithSL 5,604
3 Manas Bhardwaj 4,986
4 Maciej Los 4,790


Advertise | Privacy | Mobile
Web02 | 2.8.1411023.1 | Last Updated 8 May 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100