0
其實我試圖插入以及更新數據到datagridview在運行時,我寫了一個代碼,但執行它時給我數據類型沒有重載的方法'的TryParse」拍攝‘1’參數
我ACCDB表結構是像下面沒有超載的方法'TryParse'需要'1'參數
Field Datatype
Account-- Memo
AccountNumber--Number
Date--Date/Time
Description--Memo
Post_Ref--Memo
Debit--Number
Credit--Number
Balance--Number
**
private void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e)
{
string connectionString = null;
connectionString = ConfigurationManager.ConnectionStrings["AccessConnectionString"].ConnectionString;
con.ConnectionString = connectionString;
string cmd1 = "insert into Ledger([AccountNumber],[Account],[Date],[Description],[Post_Ref],[Debit],[Credit],[Balance])values(?,?,?,?,?,?,?,?)";
OleDbCommand cmd = new OleDbCommand(cmd1, con);
con.Open();
cmd.CommandType = CommandType.Text;
int accountNumber;
bool accountHasValue = int.TryParse(dataGridView1.Rows[e.RowIndex].Cells["AccountNumber"].Value.ToString(), out accountNumber);
if (accountHasValue)
{
cmd.Parameters.AddWithValue("@AccountNumber", accountNumber);
}
string accounts = dataGridView1.Rows[e.RowIndex].Cells["Account"].Value.ToString();
cmd.Parameters.AddWithValue("@Account", accounts);
DateTime datetime;
bool dateTimeHasValue = DateTime.TryParse(dataGridView1.Rows[e.RowIndex].Cells["Date"].Value.ToString(), out datetime);
if (dateTimeHasValue)
{
cmd.Parameters.AddWithValue("@Date", datetime);
}
string Description = dataGridView1.Rows[e.RowIndex].Cells["Description"].Value.ToString();
cmd.Parameters.AddWithValue("@Description", Description);
string Post_Ref = dataGridView1.Rows[e.RowIndex].Cells["Post_Ref"].Value.ToString();
cmd.Parameters.AddWithValue("@Post_Ref", Post_Ref);
int debit;
bool debitHasValue = Int32.TryParse(dataGridView1.Rows[e.RowIndex].Cells["Debit"].Value.ToString(), out debit);
if (debitHasValue)
{
cmd.Parameters.AddWithValue("@Debit", debit);
}
int Credits;
bool CreditsHasValue = Int32.TryParse(dataGridView1.Rows[e.RowIndex].Cells["Credit"].Value.ToString(), out Credits);
if (CreditsHasValue)
{
cmd.Parameters.AddWithValue("@Credit", Credits);
}
int Balances;
bool BalancesHasValue = Int32.TryParse(dataGridView1.Rows[e.RowIndex].Cells["Balance"].Value.ToString(), out Balances);
if (BalancesHasValue)
{
cmd.Parameters.AddWithValue("@Balance", Balances);
}
cmd.ExecuteNonQuery();
con.Close();
Load_data();
}
我寫了你的代碼,但它給了我沒有給出一個或多個所需參數的值。錯誤爲cmd.ExecuteNonQuery();我看到通過使用調試器,調試器不會進入cmd.Parameters.AddWithValue的日期,信用,借記和餘額 – Atul
調試器不進入'if'語句的原因是因爲'TryParse'不能解析值。或者沒有價值可言。確保你的dataGridView1.Rows [e.RowIndex] .Cells [「AccountNumber」]。Value從Grid返回一個值 –
沒有給出一個或多個必需參數的值現在,execption正在發佈先生iam在問題中更新我的代碼, cmd.ExecuteNonQuery();線 – Atul