943,866 Members | Top Members by Rank

Ad:
  • VB.NET Discussion Thread
  • Unsolved
  • Views: 1666
  • VB.NET RSS
You are currently viewing page 2 of this multi-page discussion thread; Jump to the first page
Sep 27th, 2009
-1

Re: error in insert into statement

Click to Expand / Collapse  Quote originally posted by TomW ...
The dataadapter does not automatically create any of these commands.

If you are referring to the use of a commandbuilder attached to your dataadapter to generate the insert, update & delete statements; I would like to see the block of coding that creates this and fills your dataadapter so that I may see where it is causing you problems.

Also does the table have a primary key/unique field, the command builder needs this in order to be able to perform these statements.
this is the code
VB.NET Syntax (Toggle Plain Text)
  1. m_DataAdapter = New OleDb.OleDbDataAdapter("Select * from [Base Colours]", oledbcon)
  2. m_DataAdapter.Fill(m_DataTable)
  3. m_CommandBuilder = New OleDb.OleDbCommandBuilder(m_DataAdapter)
  4. DataGridView1.DataSource = m_DataTable
Reputation Points: -1
Solved Threads: 23
Posting Whiz in Training
babbu is offline Offline
207 posts
since Jun 2009
Sep 27th, 2009
0

Re: error in insert into statement

Changes below. Also I dont know what fields your table has but the CommandBuilder object requires a primary key.

VB.NET Syntax (Toggle Plain Text)
  1. m_DataAdapter = New OleDb.OleDbDataAdapter("Select * from [Base Colours]", oledbcon)
  2. m_DataAdapter.Fill(m_DataTable)
  3. m_CommandBuilder = New OleDb.OleDbCommandBuilder(m_DataAdapter)
  4.  
  5. m_DataAdapter.InsertCommand = m_CommandBuilder.GetInsertCommand
  6. m_DataAdapter.UpdateCommand = m_CommandBuilder.GetUpdateCommand
  7. m_DataAdapter.DeleteCommand = m_CommandBuilder.GetDeleteCommand
  8. DataGridView1.DataSource = m_DataTable
Last edited by TomW; Sep 27th, 2009 at 8:13 am.
Reputation Points: 84
Solved Threads: 48
Posting Whiz
TomW is offline Offline
342 posts
since Sep 2009
Sep 27th, 2009
0

Re: error in insert into statement

hi
my be you can use the following method :

VB.NET Syntax (Toggle Plain Text)
  1. dim b1 , b2 , b3 as string
  2. b1 = "'" textbox1.text & "',"
  3. b2 = "'" textbox2.text & "',"
  4. b3 = "'" textbox3.text & "'"
  5. dim s as string
  6. s = "Insert into table name "
  7. s = s & "(field1,field2,field3) values"
  8. s = s & "(b1,b2,b3)"
  9. cmd.commandtext=s
  10. cmd.excuteNonQuery()
  11.  
  12. msgbox "data saved succesfully"

use this method to execute the insert into

and attention to make the same values and target fields is the same..

_________________________
i hope it'll be useful

sameh senosi,
Reputation Points: 10
Solved Threads: 0
Newbie Poster
samehsenosi is offline Offline
10 posts
since Sep 2009
Sep 30th, 2009
0

Re: error in insert into statement

i am aware of the insert query. but i wanted to add data using command builder.
Reputation Points: -1
Solved Threads: 23
Posting Whiz in Training
babbu is offline Offline
207 posts
since Jun 2009
Sep 30th, 2009
0

Re: error in insert into statement

Click to Expand / Collapse  Quote originally posted by babbu ...
i am aware of the insert query. but i wanted to add data using command builder.
See my above example, creating the command builder an attaching it to your dataadapter, only allows the commandbuilder to get the structure of the tables from your selectcommand. You still have to individually assign each of the new commands to the appropiate commands in the dataadapter.

VB Syntax (Toggle Plain Text)
  1. m_DataAdapter.InsertCommand = m_CommandBuilder.GetInsertCommand
  2. m_DataAdapter.UpdateCommand = m_CommandBuilder.GetUpdateCommand
  3. m_DataAdapter.DeleteCommand = m_CommandBuilder.GetDeleteCommand

On an additional note; I would suggest not using "Select *" in conjunction with a command builder; instead explicitily write out each of the column names you want to work with. Previously I have run into problems doing the former mentioned.

Using the "Select *" method in conjuction with typed datasets causes an error with the command builder if you ever make changes to the database table in the future such as adding additional columns, regardless if you need to use them or not in your program, the typed dataset then no longer matches the command builders table structure. You would then need to go and update the typed datasets and redistribute the program with every change.
Last edited by TomW; Sep 30th, 2009 at 8:41 am.
Reputation Points: 84
Solved Threads: 48
Posting Whiz
TomW is offline Offline
342 posts
since Sep 2009
Sep 30th, 2009
0

Re: error in insert into statement

still the same error
Reputation Points: -1
Solved Threads: 23
Posting Whiz in Training
babbu is offline Offline
207 posts
since Jun 2009
Sep 30th, 2009
0

Re: error in insert into statement

Hmmmm check a couple of things.
01) Have a messagebox display the actual comandbuilder.GetInsertCommand.ToString and let me know exactly what the insert statement looks like.

02) Does your table have a primary key?
Reputation Points: 84
Solved Threads: 48
Posting Whiz
TomW is offline Offline
342 posts
since Sep 2009
Sep 30th, 2009
0

Re: error in insert into statement

Click to Expand / Collapse  Quote originally posted by TomW ...
Hmmmm check a couple of things.
01) Have a messagebox display the actual comandbuilder.GetInsertCommand.ToString and let me know exactly what the insert statement looks like.

02) Does your table have a primary key?
yes my table has a primary key.

if u want the exact exception that is occuring this is it

System.Data.OleDb.OleDbException was unhandled
ErrorCode=-2147217900
Message="Syntax error in INSERT INTO statement."
Source="Microsoft Office Access Database Engine"
StackTrace:
at System.Data.Common.DbDataAdapter.UpdatedRowStatusErrors(RowUpdatedEventArgs rowUpdatedEvent, BatchCommandInfo[] batchCommands, Int32 commandCount)
at System.Data.Common.DbDataAdapter.UpdatedRowStatus(RowUpdatedEventArgs rowUpdatedEvent, BatchCommandInfo[] batchCommands, Int32 commandCount)
at System.Data.Common.DbDataAdapter.Update(DataRow[] dataRows, DataTableMapping tableMapping)
at System.Data.Common.DbDataAdapter.UpdateFromDataTable(DataTable dataTable, DataTableMapping tableMapping)
at System.Data.Common.DbDataAdapter.Update(DataTable dataTable)
at libra_plasticvb.frmBaseColours.btnAdd_Click(Object sender, EventArgs e) in D:\waheebdocs\project1\libra plastic\libra plasticvb\Backup\libra plasticvb\Base Colours.vb:line 45
at System.Windows.Forms.Control.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnClick(EventArgs e)
at System.Windows.Forms.ButtonBase.OnKeyUp(KeyEventArgs kevent)
at System.Windows.Forms.Control.ProcessKeyEventArgs(Message& m)
at System.Windows.Forms.Control.ProcessKeyMessage(Message& m)
at System.Windows.Forms.Control.WmKeyChar(Message& m)
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(ApplicationContext context)
at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.OnRun()
at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.DoApplicationModel()
at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.Run(String[] commandLine)
at libra_plasticvb.My.MyApplication.Main(String[] Args) in 17d14f5c-a337-4978-8281-53493378c1071.vb:line 81
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()
Reputation Points: -1
Solved Threads: 23
Posting Whiz in Training
babbu is offline Offline
207 posts
since Jun 2009
Sep 30th, 2009
0

Re: error in insert into statement

No I want to see exactly what the Insert Into statement that the command builder made looks like.
Reputation Points: 84
Solved Threads: 48
Posting Whiz
TomW is offline Offline
342 posts
since Sep 2009

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in VB.NET Forum Timeline: Small Probs
Next Thread in VB.NET Forum Timeline: Storing Data in Excel Worksheet Invisible to the User





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC