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
Oladotun Obileye
NA
98
19.4k
how to automatical update datagrid after insert in c#winform
May 27 2019 5:42 PM
Good day everyone i am working on an application in c# windows form that has two usercontrol place on a form.
the first one is for registration i.e to register the second one is to view the registered users, to crud. and also has a datagriedview. whenever i register a new it does not show in the second usercontrol except i exit and re-run the code before it will get updated in the datagridview. please i want whenever i insert a new record it should show in the second usercontrol. not only datagridview even label thanks to get updated.
this is the usercontrol for registration
private
void
btnreg_Click(
object
sender, EventArgs e)
{
userprofile up =
new
userprofile();
if
(txtempno.Text !=
null
&& txtfullname.Text !=
null
&& dob.Value !=
null
&& txtdept.Text !=
null
&& txtoccupation.Text !=
null
&& txtmaritalstatus.SelectedItem !=
null
&& txtemail.Text !=
null
&& txtpost.Text !=
null
&& txthaddress.Text !=
null
&& txtpno.Text !=
null
&& txtnation.Text !=
null
&& txtorigin.Text !=
null
&& pictureBox1.Image !=
null
)
{
MemoryStream ms =
new
MemoryStream();
pictureBox1.Image.Save(ms, pictureBox1.Image.RawFormat);
byte
[] img = ms.ToArray();
if
(rfemale.Checked ==
true
)
{
gender =
"Female"
;
}
else
if
(rmale.Checked ==
true
)
{
gender =
"Male"
;
}
SqlDataAdapter da =
new
SqlDataAdapter(
"Select * FROM Registrationform where Employementnumber='"
+ txtempno.Text +
"' or Email='"
+ txtemail.Text +
"' "
, con);
DataTable dt =
new
DataTable();
da.Fill(dt);
if
(dt.Rows.Count >= 1)
{
MessageBox.Show(
"already exist"
);
}
else
{
con.Open();
SqlCommand cmd =
new
SqlCommand();
cmd.Connection = con;
cmd.CommandText =
"INSERT INTO Registrationform([Employementnumber],[FullName],[Dateofbirth],[Gender],[Occupation],[Maritalstatus],[Email],[Department],[Position],[HomeAddress],[Phonenumber],[Nationality],[Stateoforigin],[image]) VALUES (@empno,@fullname,@dob,@gen,@occ,@ms,@email,@dept,@post,@haddress,@pno,@nation,@soo,@img)"
;
cmd.Parameters.AddWithValue(
"@empno"
, txtempno.Text);
cmd.Parameters.AddWithValue(
"@fullname"
, txtfullname.Text);
cmd.Parameters.AddWithValue(
"@dob"
, dob.Value);
cmd.Parameters.AddWithValue(
"@gen"
, gender);
cmd.Parameters.AddWithValue(
"@occ"
, txtoccupation.Text);
cmd.Parameters.AddWithValue(
"@ms"
, txtmaritalstatus.GetItemText(txtmaritalstatus.SelectedItem));
cmd.Parameters.AddWithValue(
"@email"
, txtemail.Text);
cmd.Parameters.AddWithValue(
"@dept"
, txtdept.Text);
cmd.Parameters.AddWithValue(
"@post"
, txtpost.Text);
cmd.Parameters.AddWithValue(
"@haddress"
, txthaddress.Text);
cmd.Parameters.AddWithValue(
"@pno"
, txtpno.Text);
cmd.Parameters.AddWithValue(
"@nation"
, txtnation.Text);
cmd.Parameters.AddWithValue(
"@soo"
, txtorigin.Text);
cmd.Parameters.AddWithValue(
"@img"
, img);
cmd.ExecuteNonQuery();
con.Close();
MessageBox.Show(
"data entered"
);
da.Fill(dt);
BindingSource bd =
new
BindingSource();
up.dtuserpro.DataSource = dt;
up.dtuserpro.DataSource = bd;
con.Close();
}
}
else
{
MessageBox.Show(
"Please Fill In All The Necessary Informations"
);
}
}
Reply
Answers (
2
)
state machine workflow (.net frame work 4.6.1)
Getting Cell value from Gridview