实际上我试图在运行时插入以及更新数据到datagridview,因为我已经编写了一个代码,但在执行它时给我数据类型没有重载方法' TryParse'需要' 1'参数
我的accdb表结构如下所示
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();
}
答案 0 :(得分:0)
错误只是意味着TryParse方法没有接受一个参数的重载。如果查看documentation,则需要两个参数。
对于使用TryParse的代码行,首先声明一个变量并将其用作out参数并将其传递给种子数据库。给我看一下代码!!!好的,请看下面的例子。
//For accountNumber
int accountNumber;
bool accountHasValue = int.TryParse(dataGridView1.Rows[e.RowIndex].Cells["AccountNumber"].Value, out accountNumber);
if(accountHasValue)
{
cmd.Parameters.AddWithValue("@AccountNumber", accountNumber);
}
//For Datetime
DateTime datetime;
bool dateTimeHasValue = DateTime.TryParse(dataGridView1.Rows[e.RowIndex].Cells["Date"].Value, out datetime);
if(dateTimeHasValue)
{
cmd.Parameters.AddWithValue("@Date", datetime);
}
//For Debit
int debit;
bool debitHasValue = Int32.TryParse(dataGridView1.Rows[e.RowIndex].Cells["Debit"].Value, debit);
if(debitHasValue )
{
cmd.Parameters.AddWithValue("@Debit", debit);
}
基本上您使用TryParse的所有代码行都按上述方式实现。