输入字符串格式错误不正确

输入字符串格式错误不正确

本文介绍了输入字符串格式错误不正确的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



执行INSERT查询时出现上述错误。我已经给出了以下代码。



Hi everyone,

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

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.

推荐答案


serialNo = Convert.ToInt32(textBoxSerialNo.Text);

意味着文本框中的数据错误,或者数据库中的某个列期望基于数字或日期的值,并且您传递的是字符串。



使用调试器确切地找到引发错误的行然后仔细查看相关值 - 如果不确切知道你要传递给哪里,我们就无法为你做任何事情。还要检查你的组合框:如果它们包含的类没有实现ToString覆盖,那么返回的值可能是完全限定的类名而不是你期望的值 - 这也可能导致你的问题但是因为我说,没有实际价值,我们无法分辨。



请看看你的代码。想想你正在做什么。

将TextBox的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?


这篇关于输入字符串格式错误不正确的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-21 13:33