ここにいる多くの人がエラーメッセージとDB_connectionのコードを再投稿するようにアドバイスしてくれたので、この質問を再投稿しています。エラーはこのコードのDB_Accessクラスに表示されますconn = DB_conection.GetConnection() `;、私は作業中ですcシャープとmssqlサーバー2008の単純なデータベースプロジェクトですが、プログラムのコンパイル時にエラーが発生し、以下のメッセージがポップアップ表示されます
System.TypeInitializationException was unhandled
Message=The type initializer for 'StudentsInformationSystem.DB_conection' threw an exception.
Source=StudentsInformationSystem
TypeName=StudentsInformationSystem.DB_conection
StackTrace:
at StudentsInformationSystem.DB_conection.GetConnection()
at StudentsInformationSystem.DB_Access..ctor() in C:\Users\soft\Documents\Visual Studio 2010\Projects\StudentsInformationSystem\StudentsInformationSystem\DB_Access.cs:line 16
at StudentsInformationSystem.frmNewStudent..ctor() in C:\Users\soft\Documents\Visual Studio 2010\Projects\StudentsInformationSystem\StudentsInformationSystem\frmNewStudent.cs:line 14
at StudentsInformationSystem.Form1.btnAddNewStudent_Click(Object sender, EventArgs e) in C:\Users\soft\Documents\Visual Studio 2010\Projects\StudentsInformationSystem\StudentsInformationSystem\Form1.cs:line 31
at System.Windows.Forms.Control.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ButtonBase.WndProc(Message& m)
at System.Windows.Forms.Button.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.Run(Form mainForm)
at StudentsInformationSystem.Program.Main() in C:\Users\soft\Documents\Visual Studio 2010\Projects\StudentsInformationSystem\StudentsInformationSystem\Program.cs:line 18
at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
InnerException: System.NullReferenceException
Message=Object reference not set to an instance of an object.
Source=StudentsInformationSystem
StackTrace:
at StudentsInformationSystem.DB_conection..cctor() in C:\Users\soft\Documents\Visual Studio 2010\Projects\StudentsInformationSystem\StudentsInformationSystem\DB_conection.cs:line 15
InnerException:
そしてこれは私のDB_connectionクラスです
class DB_conection
{
public static SqlConnection NewCon;
public static string constr = ConfigurationManager.ConnectionStrings["ConString"].ConnectionString;
public static SqlConnection GetConnection() {
NewCon = new SqlConnection(constr);
return NewCon;
}
}
これはDB_Accessの私のコードです
namespace StudentsInformationSystem
{
class DB_Access
{
SqlConnection conn;
public DB_Access() {
conn = DB_conection.GetConnection();
}
public void add_student(string regNo,string fname, string lname, string phoneNo){
if (conn.State == ConnectionState.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();
}
}
}