473,406 Members | 2,705 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

InsertCommand???

Hi,

I am running an insert statement into an access database, but i don't want
to add information to every column in my table... when i run my
insertcommand the following exception is thrown...

"Number of query values and Destination fields are not the same"...

How do i stop it from caring wether i insert a record for all my fields or
just one? and the fields that i am missing out are not required so i don't
understand the problem??

Any help would be appreciated.

Regards

Darryn
Nov 16 '05 #1
5 3611
Hi Darryn,

I suspect you aren't specifying which columns you are updating, so it expects you to pass it a value for all columns in the order they are in the table. Or, you are specifying columns, but do not pass the same number of values.

INSERT INTO SomeTable (Column1, Column2, Column3) VALUES ('Value1', 'Value2', 'Value3')

--
Happy coding!
Morten Wennevik [C# MVP]
Nov 16 '05 #2
Darryn Ross <da****@datawave.com.au> wrote:
I am running an insert statement into an access database, but i don't want
to add information to every column in my table... when i run my
insertcommand the following exception is thrown...

"Number of query values and Destination fields are not the same"...

How do i stop it from caring wether i insert a record for all my fields or
just one? and the fields that i am missing out are not required so i don't
understand the problem??


Please post the command you're using.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #3
here is my command...

try {

//Building the insert command for the addition of new records

InsertDtlsCommand.CommandText = "INSERT INTO tblGLBatch VALUES (@TransNum,
@Source, @Type, @TDate," +

"@Reference, @Code, @Note, @GSTMode, @Debit, @Credit, @GST_Debit,
@GST_Credit," +

"@Amount)" ;
//Setting the connnection object for the update command

InsertDtlsCommand.Connection = JournalConnection ;

//For each field that will be getting a value added to it, there will need
to be a parameter

//added and filled in with the appropriate information

InsertDtlsCommand.Parameters.Add("@TransNum", OleDbType.Integer, 9,
"TransNum") ;

InsertDtlsCommand.Parameters["@TransNum"].Value = GetNextTransNum() ;

InsertDtlsCommand.Parameters.Add("@Source", OleDbType.VarChar, 14, "Source")
;

InsertDtlsCommand.Parameters["@Source"].Value = "General Ledger" ;

InsertDtlsCommand.Parameters.Add("@Type", OleDbType.Integer, 9, "Type") ;

InsertDtlsCommand.Parameters["@Type"].Value = 40 ;

InsertDtlsCommand.Parameters.Add("@TDate", OleDbType.Date, 10, "TDate") ;

InsertDtlsCommand.Parameters["@TDate"].Value = txtDate.Value ;

InsertDtlsCommand.Parameters.Add("@Reference", OleDbType.VarChar, 50,
"Reference") ;

InsertDtlsCommand.Parameters["@Reference"].Value = txtReference.Text ;

InsertDtlsCommand.Parameters.Add("@Code", OleDbType.VarChar, 9, "Code") ;

InsertDtlsCommand.Parameters["@Code"].Value = txtCode.Text ;

InsertDtlsCommand.Parameters.Add("@Note", OleDbType.VarChar, 250, "Note") ;

InsertDtlsCommand.Parameters["@Note"].Value = txtNote.Text ;

InsertDtlsCommand.Parameters.Add("@GSTMode", OleDbType.VarChar, 1,
"GSTMode") ;

InsertDtlsCommand.Parameters["@GSTMode"].Value =
txtTaxCode.Text.Substring(0, 1) ;
InsertDtlsCommand.Parameters.Add("@Debit", OleDbType.Double, 10, "Debit") ;

InsertDtlsCommand.Parameters["@Debit"].Value =
Convert.ToDouble(txtDebit.Text) ;

InsertDtlsCommand.Parameters.Add("@Credit", OleDbType.Double, 10, "Credit")
;

InsertDtlsCommand.Parameters["@Credit"].Value =
Convert.ToDouble(txtCredit.Text) ;

InsertDtlsCommand.Parameters.Add("@GST_Debit", OleDbType.Double, 10,
"GST_Debit") ;

InsertDtlsCommand.Parameters["@GST_Debit"].Value =
Convert.ToDouble(txtGSTDebit.Text) ;

InsertDtlsCommand.Parameters.Add("@GST_Credit", OleDbType.Double, 10,
"GST_Credit") ;

InsertDtlsCommand.Parameters["@GST_Credit"].Value =
Convert.ToDouble(txtGSTCredit.Text) ;

InsertDtlsCommand.Parameters.Add("@Amount", OleDbType.Double , 10, "Amount")
;

InsertDtlsCommand.Parameters["@Amount"].Value = 100.00 ;

//Allocating the insert command defined above to the JournalAdapter.

JournalAdapter.InsertCommand = InsertDtlsCommand ;

}

catch(Exception e) {

MessageBox.Show(e.Message, "JournalInsertCommand", MessageBoxButtons.OK,
MessageBoxIcon.Error) ;

}

There are two more fields in the database but i don't want to update them...
if i leave them out it has a shit?? if i put tham all in it works fine??
"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Darryn Ross <da****@datawave.com.au> wrote:
I am running an insert statement into an access database, but i don't want to add information to every column in my table... when i run my
insertcommand the following exception is thrown...

"Number of query values and Destination fields are not the same"...

How do i stop it from caring wether i insert a record for all my fields or just one? and the fields that i am missing out are not required so i don't understand the problem??


Please post the command you're using.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 16 '05 #4
Darryn Ross <da****@datawave.com.au> wrote:
here is my command...

try {

//Building the insert command for the addition of new records

InsertDtlsCommand.CommandText = "INSERT INTO tblGLBatch VALUES (@TransNum,
@Source, @Type, @TDate," +

"@Reference, @Code, @Note, @GSTMode, @Debit, @Credit, @GST_Debit,
@GST_Credit," +

"@Amount)" ;
Right - you haven't specified which columns you're trying to insert
into, which means you *do* have to have all of the columns, in the
order in which they're in the schema. If you want to only specify some
columns, you need to use

INSERT INTO tblGLBATCH (Column1, Column2, ...) VALUES (...)
There are two more fields in the database but i don't want to update them...
if i leave them out it has a shit?? if i put tham all in it works fine??


I don't think I'd characterise "telling you that your SQL is invalid"
as "having a shit".

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #5
hi,
For insert command you need to supply correct no of parameters which
match the database no of feilds.

Suppose if you want to put it as blank use System.DBNull.Value to check
whether it is empty. Then pass this System.DBNull.Value to your command
Nov 16 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: hamid S | last post by:
Hi, I am new to ADO.Net and I am getting system error when I try to issue the System Error and sqlDataAdapter.InsertCommand.ExecuteNonQuery to update the database. What could be wrong. Please...
0
by: Ivaylo | last post by:
I use OleDbAdapter to fill my dataset from Access database and tables with autoincrement id columns.Also i created xsd schema for my dataset The problem is that the dataset automatically add neu...
1
by: Luboą ©lapák | last post by:
Hi, I need define InsertCommand in SQLDataAdapter, but I don't know how. Please Help me. I don't know the format of InsertCommand. Thanks
3
by: Jon S via DotNetMonster.com | last post by:
Hi all, I'm having a problem updating a simple change I've made to a Access 2000 table through databinding. The error I get is : An unhandled exception of type...
1
by: zoneal | last post by:
I retrieved the following function from VB.NET help and added a few statements for updating the datasource. But, it does not actually commence the InsertCommand property of the DataAdapter in order...
3
by: Rich | last post by:
What is the diffeence bewtween a dataAdapter.InsertCommand and dataAdapter.SelectCommand (and dataAdapter.UpdateCommand for that matter)? Dim da As SqlDataAdapter conn.Open da.SelectCommand =...
2
Elnath
by: Elnath | last post by:
im connecting to a DataBase tru ODBC, i need to insert new records (rows) therefore i want the InsertCommand to be generated by my CommbadBuilder. This solution however is aint working, the...
2
by: RP | last post by:
>From the below given codes used to insert a record in a table, which code is well-optimized and must be used. Please also let me know why it is better. ...
2
by: Cirene | last post by:
In my HTML view of my ASPX page how do I use the NOW() function for the InsertCommand? This doesn't seem to work... Code snippet: <asp:SqlDataSource ID="sdsPress" runat="server"...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.