Note that there are some explanatory texts on larger screens.

plurals
  1. POmy c# code throws an exception on accessing sql server
    primarykey
    data
    text
    <p>i am new to c# and sql I am creating a simple student database I added my database created in sql server 2008. now i have a form where the inputs are given and i have a button insert to insert the data into the database. But when i click the button i get an exception.</p> <p>this is my App.config file</p> <pre><code>`&lt;?xml version="1.0" encoding="utf-8" ?&gt; &lt;configuration&gt; &lt;startup&gt; &lt;supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" /&gt; &lt;/startup&gt; &lt;configSections&gt; &lt;connectionStrings&gt; &lt;appSettings&gt; &lt;add name="ConString" connectionString="Data Source=ARAVIND-HP\SQLEXPRESS; Initial Catalog=test;Integrated Security=True" providerName="System.Data.sqlClient"/&gt; &lt;/appSettings&gt; &lt;/connectionStrings&gt; </code></pre> <p> `</p> <p>The form i used is given below</p> <pre><code>using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace test { public partial class frmNewStudent : Form { public frmNewStudent() { InitializeComponent(); } private void btnClose_Click(object sender, EventArgs e) { this.Close(); } private void frmNewStudent_Load(object sender, EventArgs e) { } private void btnInsert_Click(object sender, EventArgs e) { DB_Access access = new DB_Access(); access.add_student(txtRegNo.Text,txtFName.Text,txtLName.Text,txtPhone.Text); MessageBox.Show("Data added successfully"); } } } </code></pre> <p>now i have two classes 1.DB_access 2.DB_Connections</p> <p>the code of DB_access is below</p> <pre><code>using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Data; using System.Configuration; using System.Data.SqlClient; namespace test { class DB_Access { public SqlConnection conn; public DB_Access() { conn = DB_Connection.GetConnection(); } public void add_student(string regno, string fname, string lname, string phone) { if(conn.State.ToString()=="Closed") { conn.Open(); } SqlCommand newCmd = conn.CreateCommand(); newCmd.Connection = conn; newCmd.CommandType = CommandType.Text; newCmd.CommandText = "insert into student values('"+ regno +"','"+ fname +"','"+ lname +"','"+ phone +"')"; newCmd.ExecuteNonQuery(); } } } </code></pre> <p>the code of DB_Connections is below</p> <pre><code>using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Data; using System.Configuration; using System.Data.SqlClient; namespace test { class DB_Connection { public static SqlConnection NewCon; public static string ConStr = ConfigurationManager.ConnectionStrings["ConString"].ConnectionString; public static SqlConnection GetConnection() { NewCon = new SqlConnection(ConStr); return NewCon; } } } </code></pre> <p>when i run this and when i click the insert button i get the following exception:</p> <p>An unhandled exception of type 'System.TypeInitializationException' occurred in test.exe</p> <p>Additional information: The type initializer for 'test.DB_Connection' threw an exception.</p> <p>and the line 'conn = DB_Connection.GetConnection();' gets highlighted</p> <p>i am not able to find the error.please help me with this</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload