我在一个VB.NET项目上工作,我有一个按钮,我想要插入数据到我的表。因此,这是这个按钮的代码:插入数据到SQL Server Compact数据库
Public Class Form21
Dim connexion As New SqlCeConnection("Data Source=D:\Cours\Projets\Stge\WindowsApplication1\WindowsApplication1\datababse.sdf")
Dim cmd As New SqlCeCommand
Dim libelle As String = TextBox1.Text
Dim prix As Double = Double.Parse(TextBox2.Text)
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
cmd.Connection = connexion
cmd.CommandText = "INSERT into Produit_fini values(@libelle,@prix)"
cmd.Parameters.AddWithValue("@libelle", libelle)
cmd.Parameters.AddWithValue("@prix", prix)
connexion.Open()
cmd.ExecuteNonQuery()
connexion.Close()
End Sub
End Class
现在的问题是,当我想打开该按钮想通窗口,我在这条线得到一个错误:
Form21.ShowDialog()
错误表示我无法打开窗口。我的按钮代码中是否有错误?请帮忙。
谢谢
以下是错误细节:
L'exception System.InvalidOperationException n'a pas été gérée
Message=Une erreur s'est produite lors de la création du formulaire. Pour plus d'informations, consultez Exception.InnerException. L'erreur est : La référence d'objet n'est pas définie à une instance d'un objet.
Source=database
StackTrace:
à database.My.MyProject.MyForms.Create_Instance_[T](T Instance) dans 17d14f5c-a337-4978-8281-53493378c1071.vb:ligne 190
à database.My.MyProject.MyForms.get_Form21()
à database.Form1.إضافةمنتوججديدToolStripMenuItem_Click(Object sender, EventArgs e) dans D:\Cours\Projets\ferran\WindowsApplication1\WindowsApplication1\Form1.vb:ligne 130
à System.Windows.Forms.ToolStripItem.RaiseEvent(Object key, EventArgs e)
à System.Windows.Forms.ToolStripMenuItem.OnClick(EventArgs e)
à System.Windows.Forms.ToolStripItem.HandleClick(EventArgs e)
à System.Windows.Forms.ToolStripItem.HandleMouseUp(MouseEventArgs e)
à System.Windows.Forms.ToolStripItem.FireEventInteractive(EventArgs e, ToolStripItemEventType met)
à System.Windows.Forms.ToolStripItem.FireEvent(EventArgs e, ToolStripItemEventType met)
à System.Windows.Forms.ToolStrip.OnMouseUp(MouseEventArgs mea)
à System.Windows.Forms.ToolStripDropDown.OnMouseUp(MouseEventArgs mea)
à System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
à System.Windows.Forms.Control.WndProc(Message& m)
à System.Windows.Forms.ScrollableControl.WndProc(Message& m)
à System.Windows.Forms.ToolStrip.WndProc(Message& m)
à System.Windows.Forms.ToolStripDropDown.WndProc(Message& m)
à System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
à System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
à System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
à System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
à System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
à System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
à System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
à System.Windows.Forms.Application.Run(ApplicationContext context)
à Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.OnRun()
à Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.DoApplicationModel()
à Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.Run(String[] commandLine)
à database.My.MyApplication.Main(String[] Args) dans 17d14f5c-a337-4978-8281-53493378c1071.vb:ligne 81
à System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
à System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
à Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
à System.Threading.ThreadHelper.ThreadStart_Context(Object state)
à System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)
à System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
à System.Threading.ThreadHelper.ThreadStart()InnerException: System.NullReferenceException
Message=La référence d'objet n'est pas définie à une instance d'un objet.
Source=database
StackTrace:
à database.Form21..ctor() dans D:\Cours\Projets\Stge\WindowsApplication1\WindowsApplication1\Form21.vb:ligne 7
您的实际问题似乎并不有什么做你的文章的标题。您在插入数据或使用对话框时遇到问题吗?而且您发布的代码与您所询问的错误无关。 – Tony
窗口无法显示,因为我的按钮代码有错误。在放置代码之前,窗口显示得非常好 – Somar
您可以逐句通过代码并告诉我们错误发生的位置以及错误消息是什么? – DOK