Click here to Skip to main content
15,888,610 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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();



C#
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
Updated 7-May-13 22:27pm
v2

Try this:
C#
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:
C#
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
 
Share this answer
 
C#
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

C#
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

C#
return ConfigurationManager.AppSettings["ConfigKeyName"]



Hope it helps you.
 
Share this answer
 

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


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