参数没有默认值,Excel C#

时间:2016-11-25 23:49:59

标签: c# excel datagridview

我的代码存在问题。我想将datagridview中的WSID数据插入到我的excel文件中。当我运行时,我的commanddir2.ExecuteNoQuery()出现问题,并且它表示参数@wsid没有默认值。任何人都可以帮助我吗?

string koneksi = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=D:\Dokumen\Alfon\Kerja\BCA\Program\Program-Pengisian-Uang-ATM-BCA-SOY\Program-Pengisian-Uang-ATM-BCA-SOY\bin\x86\Debug\ATM SLA SURABAYA.xlsx;Extended Properties='Excel 12.0 xml;HDR=YES';";


private void InputScheduleSaldoBtn_Click(object sender, EventArgs e)
            {

                for (int i = 0; i < ViewDataSaldoGV.Rows.Count; i++ )
                {

                OleDbConnection kon2 = new OleDbConnection(konekpengisian);
                OleDbCommand commanddir2 = kon2.CreateCommand();
                commanddir2.CommandText = @"INSERT INTO [Saldo$] ([WSID]) VALUES (@wsid)";
                commanddir2.Parameters.AddWithValue("@wsid", ViewDataSaldoGV.Rows[i].Cells["WSID"].Value);
                textBox1.Text = i.ToString();

                kon2.Open();
                commanddir2.ExecuteNonQuery();
                kon2.Close();

            }

            MessageBox.Show("Data berhasil disimpan");
}

1 个答案:

答案 0 :(得分:1)

当您具有空参数值

时设置DBNull
commanddir2.Parameters.AddWithValue("@wsid", ViewDataSaldoGV.Rows[i].Cells["WSID"].Value)?? DBNull.Value;