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
anna john
NA
47
9k
pls help on datagrid view insert /update operation
Oct 4 2019 3:58 PM
Hi all,
i have a table employee;[MSACESS database] it has an ID which is auto generated.
in datagrid view, the id field is hidden. name and department values user should enter.
so for current row , if there is no ID value, then it should be insert.
else , if there is an id value is present it should be update. my code below not looking into that.
can you please help me
private
void
DgvWizard_CellValueChanged(
object
sender, DataGridViewCellEventArgs e)
{
//insert or update into database the values from the datagridview
if
(dgvWizard.CurrentRow !=
null
)
{
using
(OleDbConnection Connection =
new
OleDbConnection(connectionString))
{
Connection.Open();
DataGridViewRow dgvRow = dgvWizard.CurrentRow;
try
{
if
(dgvRow.Cells[
"txtEmpID"
].Value==DBNull.Value)
cmd =
new
OleDbCommand(cmdText:
"insert into employees(name,contact) values (@name,@contact)"
, Connection);
//if employeeid is null, then it should insert the current row
else
cmd =
new
OleDbCommand(cmdText:
"update employees set name =@name, contact=@contact where empid=@empid"
, Connection);
//if employee id is not null, it means its an existing record and use update query
cmd.Parameters.AddWithValue(
"@name"
, dgvRow.Cells[
"txtEmployeeName"
].Value == DBNull.Value ?
""
: dgvRow.Cells[
"txtEmployeeName "
].Value.ToString());
cmd.Parameters.AddWithValue(
"@empid "
, Convert.ToInt32(dgvRow.Cells[
"txtEmpID "
].Value == DBNull.Value ?
""
: dgvRow.Cells[
"txtEmpID "
].Value));
cmd.Parameters.AddWithValue(
"@contact"
, dgvRow.Cells[
"txtContactName"
].Value == DBNull.Value ?
""
: dgvRow.Cells[
"txtContactName"
].Value.ToString());
cmd.ExecuteNonQuery();
MessageBox.Show(
"Success!"
);
}
catch
(Exception ee) { }
}
}
}
Reply
Answers (
2
)
How send data from .cs file to .ascx.vb file in vb.net
allow combobox to add user defined values at runtime