簡體   English   中英

將記錄插入到c#中的access db異常時

[英]while inserting the record into access db exception throw in c#

iam嘗試將記錄插入到ms-access數據庫中,以供以下代碼和方法使用

string SqlString = "Insert Into RegistrationForm (ClientCount,Name,Address,Contact,Documents,Money_Taking_Date,Muddat,Money_Return_date,Account_status,Taking_Amout,Interest_per_month,Pending_interest_month,Pending_interst_Amount,Total_Amount,Client_image,Document_image1,Document_image2) Values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
            conv_photo();
            using (OleDbConnection conn = new OleDbConnection(SqlString))
            {

                using (OleDbCommand cmd = new OleDbCommand(SqlString, conn))
                {

                    cmd.CommandType = CommandType.Text;
                    cmd.Parameters.AddWithValue("ClientCount", lblcount.Text);
                    cmd.Parameters.AddWithValue("Name", textBox20.Text);
                    cmd.Parameters.AddWithValue("Address", textBox21.Text);
                    cmd.Parameters.AddWithValue("Contact", textBox19.Text);
                    cmd.Parameters.AddWithValue("Documents", textBox18.Text);
                    cmd.Parameters.AddWithValue("Money_Taking_Date", maskedTextBox1.Text.ToString());
                    cmd.Parameters.AddWithValue("Muddat", textBox22.Text);
                    cmd.Parameters.AddWithValue("Money_Return_date", maskedTextBox2.Text.ToString());
                    cmd.Parameters.AddWithValue("Account_status", textBox23.Text);
                    cmd.Parameters.AddWithValue("Taking_Amout", textBox17.Text);
                    cmd.Parameters.AddWithValue("Interest_per_month", textBox16.Text);
                    cmd.Parameters.AddWithValue("Pending_interest_month", textBox15.Text);
                    cmd.Parameters.AddWithValue("Pending_interst_Amount", Convert.ToDouble(textBox13.Text));
                    cmd.Parameters.AddWithValue("Total_Amount", Convert.ToDouble(textBox14.Text));
                    cmd.Parameters.AddWithValue("@Client_image", pictureBox6);
                    cmd.Parameters.AddWithValue("Document_image1", pictureBox4);
                    cmd.Parameters.AddWithValue("Document_image2", pictureBox5);

                    conn.Open();
                    int n=cmd.ExecuteNonQuery();
                    conn.Close();
                    if (n > 0)
                    {
                        MessageBox.Show("record inserted");
                        loaddata();
                       // rno++;
                    }
                    else
                        MessageBox.Show("insertion failed");
                }
            }

方法conv_photo:

public void conv_photo()
         {
             //converting photo to binary data


             if (pictureBox6.Image != null)
             {
                 //using MemoryStream:
                 ms = new MemoryStream();
                 pictureBox6.Image.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
                 byte[] photo_aray = new byte[ms.Length];
                 ms.Position = 0;
                 ms.Read(photo_aray, 0, photo_aray.Length);
                 cmd.Parameters.AddWithValue("@pictureBox6", photo_aray);
             }

             if (pictureBox4.Image != null)
             {
                 //using MemoryStream:
                 ms = new MemoryStream();
                 pictureBox4.Image.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
                 byte[] photo_aray = new byte[ms.Length];
                 ms.Position = 0;
                 ms.Read(photo_aray, 0, photo_aray.Length);
                 cmd.Parameters.AddWithValue("@pictureBox4", photo_aray);
             }
             if (pictureBox5.Image != null)
             {
                 //using MemoryStream:
                 ms = new MemoryStream();
                 pictureBox5.Image.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
                 byte[] photo_aray = new byte[ms.Length];
                 ms.Position = 0;
                 ms.Read(photo_aray, 0, photo_aray.Length);
                 cmd.Parameters.AddWithValue("@pictureBox5", photo_aray);
             }

現在的問題是當我運行應用程序時

對象引用未設置為在cmd.Parameters.AddWithValue("@pictureBox6", photo_aray);拋出的對象執行實例cmd.Parameters.AddWithValue("@pictureBox6", photo_aray);

如果我之后使用conv_photo方法

using (OleDbConnection conn = new OleDbConnection(SqlString))
            {

}

循環給我初始化字符串的格式不符合從索引0開始的規范。執行(未處理參數異常)

我不知道該怎么辦。

這里有兩個問題:

  1. 您似乎正在使用cmd兩個版本。 一個似乎是全局的,因為它可在conv_photo內部使用,而另一個可在您的top方法所在的位置實例化。 根本不會實例化全局變量(根據您顯示的內容)。

您的using語句將創建一個全新的cmd對象,該對象與您嘗試在conv_photo內部使用的cmd對象無關。 擺脫您的全局cmd對象,並將對conv_photo的調用放在您的內部

using (OleDbCommand cmd = new OleDbCommand(SqlString, conn))

並將cmd對象作為參數傳遞給它。

  1. 您的SqlString不是連接字符串。 這是一個SQL語句。 如果您不熟悉,則需要研究什么是連接字符串

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM