TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
Atul Rokade
NA
141
43.8k
how to use update statement for datagridview
Mar 25 2016 4:27 PM
hi all , my data now inserted very well into database but now problem is when im trying to update particular cell(its not updated but create new one duplicate insert row) im not getting where is exactly problem, update code im written on dataGridView1_CellValueChanged event and insert code written on dataGridView1_RowLeave event here iam pasting Update code please help me to solve this issue (if i not write AccountNumber in cmd.Parameters.AddWithValue its giving me one or more reference missing)
string cmd1 = "update Ledger set [Account]=@Account,[Date]=@Date,[Description]=@Description,[Post_Ref]=@Post_Ref,[Debit]=@Debit,[Credit]=@Credit,[Balance]=@Balance where [AccountNumber]=@AccountNumber";
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);
}
else
{
cmd.Parameters.AddWithValue("@AccountNumber", DBNull.Value);
}
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);
}
else
{
cmd.Parameters.AddWithValue("@Date", DBNull.Value);
}
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);
}
else
{
cmd.Parameters.AddWithValue("@Debit", DBNull.Value);
}
int Credits;
bool CreditsHasValue = Int32.TryParse(dataGridView1.Rows[e.RowIndex].Cells["Credit"].Value.ToString(), out Credits);
if (CreditsHasValue)
{
cmd.Parameters.AddWithValue("@Credit", Credits);
}
else
{
cmd.Parameters.AddWithValue("@Credit", DBNull.Value);
}
int Balances;
bool BalancesHasValue = Int32.TryParse(dataGridView1.Rows[e.RowIndex].Cells["Balance"].Value.ToString(), out Balances);
if (BalancesHasValue)
{
cmd.Parameters.AddWithValue("@Balance", Balances);
}
else
{
cmd.Parameters.AddWithValue("@Balance", DBNull.Value);
}
cmd.ExecuteNonQuery();
con.Close();
}
Reply
Answers (
2
)
insert multiple row in table and update multiple row
Reg: Textbox