Click here to Skip to main content
15,879,096 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi everyone,

The above error is showing while executing the INSERT query. I have given the code below.

C#
try
            {
                //Declaration
                string desc, aType, loc, manuf, bay, model, condtn, vendor, notes, status;
                int serialNo, aCode;
                DateTime warrantyExpiry, dateOfPurchase;

                //Assigning the values with variables
                desc = Convert.ToString(textBoxDesc.Text);
                aType = Convert.ToString(comboBoxAType.SelectedItem);
                loc = Convert.ToString(comboBoxLocation.SelectedItem);
                manuf = Convert.ToString(textBoxManuf.Text);
                bay = Convert.ToString(textBoxBay.Text);
                model = Convert.ToString(textBoxModel.Text);
                condtn = Convert.ToString(comboBoxCondition.SelectedItem);
                vendor = Convert.ToString(comboBoxVendor.SelectedItem);
                notes = Convert.ToString(textBoxNotes.Text);
                status = Convert.ToString(comboBoxStatus.SelectedItem);
                //Date time Variables
                warrantyExpiry = dateTimePickerWarranty.Value.Date;
                dateOfPurchase = dateTimePickerDOP.Value.Date;

                serialNo = Convert.ToInt32(textBoxSerialNo.Text);
                
                if (rdBtnAutomatic.Checked == true)
                {
                    aCode = 123;
                    textBoxAcode.ReadOnly=true;
                }
                else if (rdBtnManual.Checked == true)
                {
                    textBoxAcode.ReadOnly = false;
                    textBoxAcode.BackColor = Color.White;
                    aCode = Convert.ToInt32(textBoxAcode.Text);
                }
                else { aCode = 1; }

                con.Open();
                OleDbCommand cmd = new OleDbCommand("INSERT INTO Assets(Description,AssetType,Location,Manufacturer,Bay,Model,SerialNumber,Status,Condition,WarrantyExpiry,AssetCode,Dateofpurchase,Vendor,Notes,Picture) VALUES(@Description,@AssetType,@Location,@Manufacturer,@Bay,@Model,@SerialNumber,@Status,@Condition,@WarrantyExpiry,@AssetCode,@Dateofpurchase,@Vendor,@Notes,@Picture)", con);

                //Saving the image                
                MemoryStream memoryStream = new MemoryStream();
                pictureBoxAssets.Image.Save(memoryStream, System.Drawing.Imaging.ImageFormat.Jpeg);
                byte[] imgArray = new byte[memoryStream.Length];
                memoryStream.Read(imgArray, 0, imgArray.Length);

                cmd.Parameters.AddWithValue("@Description", desc);
                cmd.Parameters.AddWithValue("@AssetsType", aType);
                cmd.Parameters.AddWithValue("@Location", loc);
                cmd.Parameters.AddWithValue("@Manufacturer", manuf);
                cmd.Parameters.AddWithValue("@Bay", bay);
                cmd.Parameters.AddWithValue("@Model", model);
                cmd.Parameters.AddWithValue("@SerialNumber", serialNo);
                cmd.Parameters.AddWithValue("@Status", status);
                cmd.Parameters.AddWithValue("@Condition", condtn);
                cmd.Parameters.AddWithValue("@WarrantyExpiry", warrantyExpiry);
                cmd.Parameters.AddWithValue("@AssetCode",aCode);
                cmd.Parameters.AddWithValue("@Dateofpurchase", dateOfPurchase);
                cmd.Parameters.AddWithValue("@Vendor", vendor);
                cmd.Parameters.AddWithValue("@Notes", notes);
                cmd.Parameters.AddWithValue("@Picture", imgArray);


                int result = cmd.ExecuteNonQuery();

                if (result == 1)
                {
                    MessageBox.Show("Successfull");
                }
                else
                    MessageBox.Show("Notsuccessfull");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }


Pls help me with this error. Thanks.
Posted
Comments
[no name] 18-May-15 1:25am    
Put a break point then check it ?i think u have entered the wrong input.
Sarath kumar.N 18-May-15 1:33am    
Dear Rajeesh,
I have checked using the break points. It's coming upto cmd.ExcuteNonQuery(); After that it automatically throws the exception.
[no name] 18-May-15 1:45am    
Hi sarath,
First you check each section carry the value or not ? may be the int value saved into the string area or vice versa..
Sarath kumar.N 18-May-15 1:54am    
Hi Rajeesh,
That also I checked every variable holding the correct value only. When it comes to the executing the query throws the exception.
King Fisher 18-May-15 1:59am    
if you can't ,show us what the exact values you holding on SerialNo

MS Access database engine does not know what @Description means.

To be able to use named parameters, please see this article[^]. In a short: you have to declare them.

In other cases, you have to use no-named parameters, which is defined as: ?. Please, see my past answer[^] for further details.
 
Share this answer
 
v2
Comments
Sarath kumar.N 18-May-15 2:00am    
Dear Maciej Los,
I used the same query insert the details in different form, but that's working fine. I don't know why it's throwing error.
Maciej Los 18-May-15 2:02am    
What is your exact insert statement after changes?
Sarath kumar.N 18-May-15 2:14am    
Dear Maciej Los,

There is one picture box there. If i select any image from computer for saving it in DB. It throwing "An INSERT INTO query cannot contain a multi-valued field."

Data type for the picture in DB is Attachment.
If i select the image, the value comes imgArray = {byte[879361]}
Maciej Los 18-May-15 2:49am    
If you want to save picture, please follow this link: Datatype is used in MS Access for storing JPG or gif files.
Um.
Without knowing exactly where the error occurs, we can't be specific - but I'd bet it's either here:
C#
serialNo = Convert.ToInt32(textBoxSerialNo.Text);
Meaning that the data in your textbox is wrong, or that one of the columns in your database is expecting a numeric or date based value, and you are passing a string.

Use the debugger to find exactly which line throws the error and then look closely are the relevant values - without knowing exactly what you are passing to where, we can't do anything for you. Also check your comboboxes: if the class they contain does not implement the ToString override, then the value returned is likely to be the fully qualified class name rather than anything like the value you expect - this may also be contributing to your problem but as I said, without the actual values, we can't tell.

And please, look at your code. Think about what you are doing.
Where is the point in converting a Text property of a TextBox to string, when it is a string to start with?
 
Share this answer
 
Comments
Sarath kumar.N 18-May-15 1:52am    
I have checked using the break points. It's coming upto cmd.ExcuteNonQuery(); After that it automatically throws the exception. I can't find the exact error line.
OriginalGriff 18-May-15 3:42am    
So use the debugger to look at exactly what values you are passing as each parameter.
Then look at the SQL column definition: somewhere what you are passing is valid fro the column type, but it's data dependant - and I can't see your data! :laugh:

If you make a note of (or copy'n'paste) each parameter value, and show the corresponding column definition I might be able to help. But without the data my hands are tied.
Sarath kumar.N 18-May-15 3:56am    
Hi OriginalGriff,

The line of error is "serialNo = Convert.ToInt32(textBoxSerialNo.Text);"

SerialNumber data type in Access DB is Number.
OriginalGriff 18-May-15 4:31am    
So...your user entered a non-numeric value...I can't fix your users! :laugh:
Check input, and report problems instead of going ahead and trying to send bad data to a database...
Sarath kumar.N 18-May-15 5:32am    
Hi OriginalGriff,

While testing I'm inserting only the numeric value.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900