praveen maurya

praveen maurya

  • NA
  • 0
  • 29.1k

Database not accessible

Apr 23 2020 4:22 AM
Hello There
I am just learning user Control in Visual Studio 2015. Everything was going fine but when i tried to access the database in user control file i am getting null error
 
the same connection string is working fine in winform but not working in usercontrol the error comming when I am trying to drag the user control page to my winform. the image of error is attached
 
 
 
my app.config code is written here
 app.config
  1. <?xml version="1.0" encoding="utf-8" ?>  
  2. <configuration>  
  3.   <configSections>  
  4.       
  5.   </configSections>  
  6.   <connectionStrings>  
  7.     <add name="CC" connectionString="Data Source=DESKTOP-M5IBRH0\sqlexpress;Initial Catalog=RMS;Integrated Security=True" providerName="System.Data.SqlClient"/>  
  8.   </connectionStrings>  
  9.     <startup>   
  10.         <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />  
  11.     </startup>  
  12. </configuration>  
 
 Code of User Control:
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.ComponentModel;  
  4. using System.Drawing;  
  5. using System.Data;  
  6. using System.Linq;  
  7. using System.Text;  
  8. using System.Threading.Tasks;  
  9. using System.Windows.Forms;  
  10. using System.Data.SqlClient;  
  11. using System.Configuration;  
  12.   
  13. namespace usercontrol_with_sqlconnection  
  14. {  
  15.     public partial class user_A : UserControl  
  16.     {          
  17.         public static string constring = ConfigurationManager.ConnectionStrings["CC"].ConnectionString;  
  18.         SqlConnection con = new SqlConnection(constring);        
  19.         public user_A()  
  20.         {  
  21.             InitializeComponent();  
  22.         }        
  23.     }  
  24. }  
 
 

Answers (3)