我在一个VB.NET项目上工作,我有一个按钮,我希望将数据插入到我的表中。所以这是这个按钮的代码:
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'apasétégérée
消息= Une erreur s'est produite lors delacréationduformulaire。倒入信息,咨询Exception.InnerException。 L'erreur est:Laréférenced'objetn'estdeséfinieàuneinstance d'un objet。
来源=数据库
堆栈跟踪:
à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符合)
àSystem.Windows.Forms.ToolStripItem.FireEvent(EventArgs e,ToolStripItemEventType符合)
àSystem.Windows.Forms.ToolStrip.OnMouseUp(MouseEventArgs mea)
àSystem.Windows.Forms.ToolStripDropDown.OnMouseUp(MouseEventArgs mea)
àSystem.Windows.Forms.Control.WmMouseUp(消息& m,MouseButtons按钮,Int32点击)
à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(消息& m)
àSystem.Windows.Forms.Control.ControlNativeWindow.OnMessage(消息& m)
àSystem.Windows.Forms.Control.ControlNativeWindow.WndProc(消息& 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(对象状态)
àSystem.Threading.ExecutionContext.Run(ExecutionContext executionContext,ContextCallback回调,对象状态,布尔值ignoreSyncCtx)
àSystem.Threading.ExecutionContext.Run(ExecutionContext executionContext,ContextCallback回调,对象状态)
àSystem.Threading.ThreadHelper.ThreadStart()InnerException:System.NullReferenceException
消息=Laréférenced'objetn'estpasdéfinieàuneinstance d'un objet 来源=数据库
堆栈跟踪:
àdatabase.Form21..ctor()dans D:\ Cours \ Projets \ Stge \ WindowsApplication1 \ WindowsApplication1 \ Form21.vb:ligne 7
答案 0 :(得分:0)
Public Class Form21
Dim connexion As New SqlCeConnection("Data Source...")
Dim cmd As New SqlCeCommand
Dim libelle As String = TextBox1.Text
Dim prix As Double = Double.Parse(TextBox2.Text)
如果这些是表单级别声明,因为它们似乎是在初始化之前无法引用控件(TextBox1
和TextBox2
)。改为:
Dim libelle As String = ""
Dim prix As Double =
然后在Form Load中(当存在TB时):
libelle = TextBox1.Text
prix = Double.Parse(TextBox2.Text)