Click here to Skip to main content
15,908,112 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
I am made the program student information ..
but the some error arise in particular code ,When I am Click the newstudentbutton in that time the error is rise, please solve the problem and give me solution...
code..
1.App Configuration
XML
<configuration>
  <configsections>
    <connectionstrings>
      <add name="ConString" connectionstring="Data Source=ANAND;Initial Catalog=TEST;Integrated Security=True"
           providerName ="System.Data.SqlClient"/>
     </connectionstrings>
  </configsections>
</configuration>

2.DB_ACCESS Class
C#
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
namespace Studentinformation
{
    class DB_Access
    {
        SqlConnection conn;
        public DB_Access()
        {
            conn = DB_Connection.GetConnection();  // in this line the runtime error is rise
         }
        public void add_student(string regNo, string fName, string Lname, string phoneNo)
        {
            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 + "','" + phoneNo + "',)";
            newCmd.ExecuteNonQuery();
        }
    }
}

3.
C#
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;
       }
   }

4.
C#
public partial class Form1 : Form
   {
       public Form1()
       {
           InitializeComponent();
       }

       private void btnAddNew_Click(object sender, EventArgs e)
       {
           frmNewStudent newstudent = new frmNewStudent();
           newstudent.Show();
       }
   }
Posted
Updated 17-Nov-13 1:00am
v5
Comments
Thomas Daniels 17-Nov-13 6:32am    
What's the error you get?
Member 10406767 17-Nov-13 7:13am    
conn=DB_Connection.GetConnection();//type initialisation was unhanded
Member 10406767 17-Nov-13 7:18am    
1.The type initializer for 'Studentinformation.DB_Connection' threw an exception.
2The element 'configSections' has invalid child element 'connectionstrings'. List of possible elements expected: 'section, sectionGroup'.
Maciej Los 17-Nov-13 7:03am    
Please, run your application in debug mode and test where the problem is.
Richard MacCutchan 17-Nov-13 7:09am    
Your ConString element contains the attribute connectionstring, but your code referes to it as ConnectionString. C# is case sensitive, so the spellings must be exactly the same. You could avoid situations like this by adding proper error checks in your code; don't just assume things will work.

You need to look at your DB_Connection class, and the GetConnection method in particular. The chances are that your connection string is incorrect and the web server PC can't find the SQL server, or the database doesn't exist on the server if it can find it.

Check it: Try setting up a connection in VS with the Server Explorer pane.
1) Open Server Explorer.
2) Right click "Data connections" and select "Add connection"
3) In the dialog that follows, select your DataSource, and database, specify the security info, and press the "Test connection" button.
4) When the connection works, press "OK"
5) Highlight your database in the Server Explorer pane, and look at the Properties pane. A working example of the connection string will be shown, which you can copy and paste into your app or config file.

Then, provided you don't publish the website to a different environment (such as a web server instead of your local PC - where the SQL server instance will probably be different) you should be fine.
 
Share this answer
 
go to http://csharp-sqlconnection.blogspot.com/ for easy and shortest way for database connectivity
 
Share this answer
 

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