C# – Insert Listbox Items to Database – Programming, Pseudocode Example, C# Programming Example
Database Windows Form

C# – Insert Listbox Items to Database

Here in this article, I will show you How to insert listbox items to MS Access Database.

 

Step 1:

Open MS Access, click on a Blank Desktop Database. Give the database name “dbTest.accdb“. and then click Create.

Step 2: Now create a Table in database, You can name a table anything you want, here I named it “Category” . There are two columns in the table ID, CategoryName like the following,

Table Design

Id >> AutoNumber

CategoryName >> Text

 

Step 3: Now open Visual Studio, start a new Windows Form  Application and give any name you want.

Step 4: Now Drag and Drop database file from the Documents to the Project Directory folder.

Step 5: Form Design:

 

Step 6: Write a Namespace for connectivity as in the following code snippet.

Step 7: Define global variables.

 

Step 8: Create source code for Insert to ListBox Button

 

Step 9: Create source code for Insert to Database Button

 

Finally, finished  the project.

Here are the all codes:

 

Output :

 

 

 

 

 

1 Comment

  • I am trying to like this But show error
    System.Data.OleDb.OleDbException
    HResult=0x80040E37
    Message=Could not find output table ‘Category’.
    Source=System.Data
    StackTrace:
    at System.Data.OleDb.OleDbCommand.ExecuteCommandTextErrorHandling(OleDbHResult hr)
    at System.Data.OleDb.OleDbCommand.ExecuteCommandTextForSingleResult(tagDBPARAMS dbParams, Object& executeResult)
    at System.Data.OleDb.OleDbCommand.ExecuteCommandText(Object& executeResult)
    at System.Data.OleDb.OleDbCommand.ExecuteCommand(CommandBehavior behavior, Object& executeResult)
    at System.Data.OleDb.OleDbCommand.ExecuteReaderInternal(CommandBehavior behavior, String method)
    at System.Data.OleDb.OleDbCommand.ExecuteNonQuery()
    at DB.Form1.button2_Click(Object sender, EventArgs e) in C:\Users\arun\source\repos\DB\DB\Form1.cs:line 45
    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 DB.Program.Main() in C:\Users\arun\source\repos\DB\DB\Program.cs:line 19

    This exception was originally thrown at this call stack:
    [External Code]
    DB.Form1.button2_Click(object, System.EventArgs) in Form1.cs
    [External Code]
    DB.Program.Main() in Program.cs

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: