Iv'e been searching this website forums for the right answer but i didn't find. Im using VS2008 and want to build some application that working with microsoft access database. in the database i have one table call "customers" with the coulmns = "firstName", "lastName", "address" etc.. I want to make form with the same textbox that will send the data to the database. I have some background in ASP.NET and i build some websites with asp.net but i guess its not the same syntax cause in Windows Applications this is not working. PLEASE HELP ME WITH THE RIGHT CODE..... just for sample here is the code that im writting:

private void addNewCustomer_Click(object sender, EventArgs e)
    {
        OleDbConnection reemConnection = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=E:\\WindowsFormsApplication2\forumArt.accdb");
        OleDbCommand objCmd = new OleDbCommand("Insert into customers (firstName, lastName, address, city) values (@firstName,@lastName,@address,@city)", reemConnection);
        objCmd.Parameters.Add("@firstName", OleDbType.VarChar).Value = firstName.Text;
        objCmd.Parameters.Add("@lastName", OleDbType.VarChar).Value = lastName.Text;
        objCmd.Parameters.Add("@address", OleDbType.VarChar).Value = address.Text;
        objCmd.Parameters.Add("@city", OleDbType.VarChar).Value = city.Text;

        if (firstName.Text == string.Empty || address.Text == string.Empty)
        {
            MessageBox.Show("Please Fillup the first name and the address fields");
            return;
        }
        reemConnection.Open();
        reemConnection.Close();

        Close();
    }

Thanks a lot

Recommended Answers

All 6 Replies

after you open the connection you should say this

objCmd.ExecuteNonQuery();

regards.

it still show an error. after tryin to send the data the compiler stop and mark the "reemConnection.Open();" in yellow saying its unlegal.

System.Data.OleDb.OleDbException was unhandled
Message="‏‏שם קובץ לא חוקי."
Source="Microsoft JET Database Engine"
ErrorCode=-2147467259
StackTrace:
at System.Data.OleDb.OleDbConnectionInternal..ctor(OleDbConnectionString constr, OleDbConnection connection)
at System.Data.OleDb.OleDbConnectionFactory.CreateConnection(DbConnectionOptions options, Object poolGroupProviderInfo, DbConnectionPool pool, DbConnection owningObject)
at System.Data.ProviderBase.DbConnectionFactory.CreateNonPooledConnection(DbConnection owningConnection, DbConnectionPoolGroup poolGroup)
at System.Data.ProviderBase.DbConnectionFactory.GetConnection(DbConnection owningConnection)
at System.Data.ProviderBase.DbConnectionClosed.OpenConnection(DbConnection outerConnection, DbConnectionFactory connectionFactory)
at System.Data.OleDb.OleDbConnection.Open()
at WindowsFormsApplication2.Form1.addNewCustomer_Click(Object sender, EventArgs e) in E:\WindowsFormsApplication2\Form1.cs:line 34
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(Int32 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 WindowsFormsApplication2.Program.Main() in E:\WindowsFormsApplication2\Program.cs:line 18
at System.AppDomain._nExecuteAssembly(Assembly 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)
at System.Threading.ThreadHelper.ThreadStart()
InnerException:

make sure you have permissions to access and write into the database, also double check the path of the connection string, maybe you should test the connection first without trying the insert to see what happen.

here some connection string exampleshttp://www.sqlstrings.com/MS-Access-connection-strings.htm

regards.

what is the path of your database?

post it and i wil write u the code with the textboxes u mentioned to connect to the tables in your database

WillemP

the Database is forumArt.mdb and he is placed near the C# files (not in a special dir)
<code>
E:\\WindowsFormsApplication2\forumArt.mdb
</code>
Thanks a lot

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.