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.
1 2 3 |
using System.Data.OleDb; |
Step 7: Define global variables.
1 2 3 4 |
OleDbConnection con; OleDbCommand cmd; |
Step 8: Create source code for Insert to ListBox Button
1 2 3 4 5 6 7 8 9 10 |
private void button1_Click(object sender, EventArgs e) { if(textBox1.Text!="") { listBox1.Items.Add(textBox1.Text); textBox1.Text = ""; } } |
Step 9: Create source code for Insert to Database Button
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
private void button2_Click(object sender, EventArgs e) { if(listBox1.Items.Count!=0) { string sql = "INSERT INTO Category (CategoryName) VALUES(@cName)"; con = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=dbTest.accdb"); foreach (string catgry in listBox1.Items) { cmd = new OleDbCommand(sql, con); cmd.Parameters.AddWithValue("@cName", catgry); con.Open(); cmd.ExecuteNonQuery(); con.Close(); } MessageBox.Show("Records inserted succesfully."); } else { MessageBox.Show("Error!!!"); } } |
Finally, finished the project.
Here are the all codes:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Data.OleDb; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace listboxVeriGonder { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { if(textBox1.Text!="") { listBox1.Items.Add(textBox1.Text); textBox1.Text = ""; } } OleDbConnection con; OleDbCommand cmd; private void button2_Click(object sender, EventArgs e) { if(listBox1.Items.Count!=0) { string sql = "INSERT INTO Category (CategoryName) VALUES(@cName)"; con = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=dbTest.accdb"); foreach (string catgry in listBox1.Items) { cmd = new OleDbCommand(sql, con); cmd.Parameters.AddWithValue("@cName", catgry); con.Open(); cmd.ExecuteNonQuery(); con.Close(); } MessageBox.Show("Records inserted succesfully."); } else { MessageBox.Show("Error!!!"); } } } } |
Output :
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