Click here to Skip to main content
15,898,020 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
private void button1_Click(object sender, EventArgs e)
        {
            string str = ConfigurationSettings.AppSettings["ConnectionString"];


            using (SqlConnection connection = new SqlConnection(str))
            {     try 
            {

                connection.Open();   ---throwing error here saying instance failure
            }   
            catch (System.Data.SqlClient.SqlException ex)
            {   
                  
                return;
            } 
                string selectCommandText = "SELECT * FROM tmptblUserProfile";   
                using (SqlDataAdapter adapter = new SqlDataAdapter(selectCommandText, connection))
                {
                    using (DataTable table = new DataTable("tmptblUserProfile")) 
                {             adapter.Fill(table);           
                    
                    StringBuilder commaDelimitedText = new StringBuilder();
                    commaDelimitedText.AppendLine("col1|col2|col3|col4|col5|col6col7|col8|col9|col10|col11|col12|col13|col14|col15");         
                    foreach (DataRow row in table.Rows) {
                        string value = string.Format("{0}|{1}|{2}|{3}|{4}|{5}{6}|{7}|{8}{9}|{10}|{11}{12}|{13}|{14}", row[0], row[1], row[2], row[3], row[4], row[5], row[6], row[7], row[8], row[9], row[10], row[11], row[12], row[13], row[14]);                 
                        commaDelimitedText.AppendLine(value);         
                    }        
                    File.WriteAllText(@"C:\Documents and Settings\aj99823\Desktop\amal101.txt", commaDelimitedText.ToString());    
                }  
            }

            } 
        }


in the app.config i specified as


HTML
<configuration>
  <appsettings>
 <add key="ConnectionString">
 value="Data Source=CSZ-PCS43132\\SQLEXPRESS;Initial Catalog=test;Integrated Security=True"
 />
 </add></appsettings>
  </configuration>

it is throwing one error saying instance failure while executing this statement.can anyone plss help me.............
Posted
Updated 27-Feb-12 0:45am
v2

The problem is not with your config file, the problem is with your SQL Server instance name. Check if CSZ-PCS43132\\SQLEXPRESS is a valid SQL Server instance. (Here CSZ-PCS43132 is your PC/Server name and SQLEXPRESS is your instance name)
 
Share this answer
 
connection string is specified just like how you did in appconfig.ex
XML
<connectionStrings>
   <clear />
   <add name="Local" connectionString="Data Source=MyPC;Database=Northwind;user=sa;password=pass;" />
   </connectionStrings>


It seems like your connection string is not correct.You can try to open SSMS using same credential.
 
Share this answer
 
Comments
amaljosep 27-Feb-12 5:12am    
if i give like this in app.config...how should i call it in the button click event?
Rishikesh_Singh 27-Feb-12 6:39am    
public static string GetConnectionString(string name)
{
try
{
return ConfigurationManager.ConnectionStrings[name].ConnectionString;
}

catch (ConfigurationException oex)
{
throw oex;
}
catch (Exception oex)
{
throw oex;
}
}
Your problem is because of in "\\" your connection string should be
CSZ-PCS43132\SQLEXPRESS;Initial Catalog=test;Integrated Security=True

Mark it as answered if it helps
 
Share this answer
 
v2
Comments
amaljosep 30-Mar-12 8:19am    
thanks a lot for your help.......

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